Merge pull request 'fix: file content image ordering' (#32) from refactor-architecture into main

Reviewed-on: #32
This commit is contained in:
rov 2026-03-19 17:09:02 -03:00
commit 0bb2e0cacc
2 changed files with 26 additions and 17 deletions

View File

@ -124,6 +124,12 @@
<version>3.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.grey-panther</groupId>
<artifactId>natural-comparator</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -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<String, byte[]> 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<String, byte[]> 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);