Merge pull request 'fix: file content image ordering' (#32) from refactor-architecture into main
Reviewed-on: #32
This commit is contained in:
commit
0bb2e0cacc
6
pom.xml
6
pom.xml
@ -124,6 +124,12 @@
|
|||||||
<version>3.2.3</version>
|
<version>3.2.3</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.grey-panther</groupId>
|
||||||
|
<artifactId>natural-comparator</artifactId>
|
||||||
|
<version>1.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -18,13 +18,12 @@ import com.magamochi.ingestion.service.ContentProviderService;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Comparator;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@ -90,21 +89,27 @@ public class ContentImportService {
|
|||||||
null,
|
null,
|
||||||
"en-US");
|
"en-US");
|
||||||
|
|
||||||
try {
|
try (var is = s3Service.getFileStream(filename);
|
||||||
var is = s3Service.getFileStream(filename);
|
var zis = new ZipInputStream(is)) {
|
||||||
var zis = new ZipInputStream(is);
|
|
||||||
|
Map<String, byte[]> entryMap =
|
||||||
|
new TreeMap<>(
|
||||||
|
CaseInsensitiveSimpleNaturalComparator
|
||||||
|
.getInstance()); // TreeMap keeps keys sorted naturally
|
||||||
|
|
||||||
ZipEntry entry;
|
ZipEntry entry;
|
||||||
var position = 0;
|
|
||||||
|
|
||||||
while ((entry = zis.getNextEntry()) != null) {
|
while ((entry = zis.getNextEntry()) != null) {
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) continue;
|
||||||
continue;
|
|
||||||
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
zis.transferTo(os);
|
||||||
|
entryMap.put(entry.getName(), os.toByteArray());
|
||||||
|
zis.closeEntry();
|
||||||
}
|
}
|
||||||
|
|
||||||
var os = new ByteArrayOutputStream();
|
int position = 0;
|
||||||
zis.transferTo(os);
|
for (Map.Entry<String, byte[]> sortedEntry : entryMap.entrySet()) {
|
||||||
var bytes = os.toByteArray();
|
byte[] bytes = sortedEntry.getValue();
|
||||||
|
|
||||||
var imageId = imageFetchService.uploadImage(bytes, null, ContentType.CONTENT_IMAGE);
|
var imageId = imageFetchService.uploadImage(bytes, null, ContentType.CONTENT_IMAGE);
|
||||||
var image = imageService.find(imageId);
|
var image = imageService.find(imageId);
|
||||||
@ -113,13 +118,11 @@ public class ContentImportService {
|
|||||||
MangaContentImage.builder()
|
MangaContentImage.builder()
|
||||||
.image(image)
|
.image(image)
|
||||||
.mangaContent(mangaContent)
|
.mangaContent(mangaContent)
|
||||||
.position(position)
|
.position(position++)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
zis.closeEntry();
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException("Failed to process zip: " + filename, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
mangaContent.setDownloaded(true);
|
mangaContent.setDownloaded(true);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user