diff --git a/pom.xml b/pom.xml index ed7118c..f29e8e7 100644 --- a/pom.xml +++ b/pom.xml @@ -124,6 +124,12 @@ 3.2.3 compile + + net.grey-panther + natural-comparator + 1.1 + compile + diff --git a/src/main/java/com/magamochi/content/service/ContentImportService.java b/src/main/java/com/magamochi/content/service/ContentImportService.java index 0877bb0..9adafef 100644 --- a/src/main/java/com/magamochi/content/service/ContentImportService.java +++ b/src/main/java/com/magamochi/content/service/ContentImportService.java @@ -18,13 +18,12 @@ import com.magamochi.ingestion.service.ContentProviderService; import jakarta.validation.constraints.NotNull; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.Comparator; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; +import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -90,21 +89,27 @@ public class ContentImportService { null, "en-US"); - try { - var is = s3Service.getFileStream(filename); - var zis = new ZipInputStream(is); + try (var is = s3Service.getFileStream(filename); + var zis = new ZipInputStream(is)) { + + Map entryMap = + new TreeMap<>( + CaseInsensitiveSimpleNaturalComparator + .getInstance()); // TreeMap keeps keys sorted naturally ZipEntry entry; - var position = 0; - while ((entry = zis.getNextEntry()) != null) { - if (entry.isDirectory()) { - continue; - } + if (entry.isDirectory()) continue; - var os = new ByteArrayOutputStream(); + ByteArrayOutputStream os = new ByteArrayOutputStream(); zis.transferTo(os); - var bytes = os.toByteArray(); + entryMap.put(entry.getName(), os.toByteArray()); + zis.closeEntry(); + } + + int position = 0; + for (Map.Entry sortedEntry : entryMap.entrySet()) { + byte[] bytes = sortedEntry.getValue(); var imageId = imageFetchService.uploadImage(bytes, null, ContentType.CONTENT_IMAGE); var image = imageService.find(imageId); @@ -113,13 +118,11 @@ public class ContentImportService { MangaContentImage.builder() .image(image) .mangaContent(mangaContent) - .position(position) + .position(position++) .build()); - - zis.closeEntry(); } } catch (Exception e) { - throw new RuntimeException(e); + throw new RuntimeException("Failed to process zip: " + filename, e); } mangaContent.setDownloaded(true);