@@ -2,9 +2,11 @@ package ru.soune.nocopy.service.file;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.file.moderation.ModerationPassportFile;
|
||||
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
||||
import ru.soune.nocopy.service.file.moderation.ModerationFileService;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -25,12 +27,17 @@ public class ZipService {
|
||||
|
||||
private final ModerationFileService moderationFileService;
|
||||
|
||||
private final CloudStorageService cloudStorageService;
|
||||
|
||||
private static final int MAX_SIZE_MB = 20;
|
||||
|
||||
private static final int FIRST_PART = 1;
|
||||
|
||||
private static final int SECOND_PART = 2;
|
||||
|
||||
@Value("${yandex.cloud.bucket-cold}")
|
||||
private String bucketCold;
|
||||
|
||||
public void createAndSplitZip(Long userId, List<FileEntity> files) throws IOException {
|
||||
byte[] zipBytes = createZip(files);
|
||||
String base64String = Base64.getEncoder().encodeToString(zipBytes);
|
||||
@@ -39,29 +46,29 @@ public class ZipService {
|
||||
}
|
||||
|
||||
public byte[] assembleZipFromSplitFiles(Long userId) throws IOException {
|
||||
List<ModerationPassportFile> activeFiles = moderationFileService.getActiveFiles();
|
||||
List<ModerationPassportFile> activeFiles = moderationFileService.getActiveFilesByUserId(userId);
|
||||
|
||||
String part1Content = null;
|
||||
String part2Content = null;
|
||||
String part1Key = null;
|
||||
String part2Key = null;
|
||||
|
||||
for (ModerationPassportFile file : activeFiles) {
|
||||
Path filePath = Paths.get(file.getPath());
|
||||
String content = Files.readString(filePath);
|
||||
|
||||
if (file.getPart() == FIRST_PART) {
|
||||
part1Content = content;
|
||||
part1Key = file.getPath();
|
||||
} else if (file.getPart() == SECOND_PART) {
|
||||
part2Content = content;
|
||||
part2Key = file.getPath();
|
||||
}
|
||||
}
|
||||
|
||||
if (part1Content == null || part2Content == null) {
|
||||
if (part1Key == null || part2Key == null) {
|
||||
throw new IOException("Missing part 1 or part 2 for user: " + userId);
|
||||
}
|
||||
|
||||
String base64String = mergeBase64(part1Content, part2Content);
|
||||
String part1Content = cloudStorageService.downloadPartFromBucket(bucketCold, part1Key);
|
||||
String part2Content = cloudStorageService.downloadPartFromBucket(bucketCold, part2Key);
|
||||
|
||||
return Base64.getDecoder().decode(base64String);
|
||||
String fullBase64 = part1Content + part2Content;
|
||||
|
||||
return Base64.getDecoder().decode(fullBase64);
|
||||
}
|
||||
|
||||
private byte[] createZip(List<FileEntity> files) throws IOException {
|
||||
@@ -120,11 +127,16 @@ public class ZipService {
|
||||
Files.writeString(file1, part1);
|
||||
Path file2 = userDir.resolve(userId + "___2.txt");
|
||||
Files.writeString(file2, part2);
|
||||
Map<Integer, String> files = Map.of(FIRST_PART, file1.toString(), SECOND_PART, file2.toString());
|
||||
|
||||
Map<String, String> cloudKeys = cloudStorageService.saveSplitPartsToColdStorage(userId, file1, file2);
|
||||
|
||||
Map<Integer, String> files = Map.of(
|
||||
FIRST_PART, cloudKeys.get("part1"),
|
||||
SECOND_PART, cloudKeys.get("part2"));
|
||||
|
||||
moderationFileService.addFiles(userId, files);
|
||||
|
||||
log.info("Saved split files: {} and {}", file1, file2);
|
||||
log.info("Saved split files to cold storage for userId: {}", userId);
|
||||
}
|
||||
|
||||
private String mergeBase64(String part1, String part2) {
|
||||
|
||||
Reference in New Issue
Block a user