@@ -26,13 +26,10 @@ import java.util.zip.ZipOutputStream;
|
|||||||
public class ZipService {
|
public class ZipService {
|
||||||
|
|
||||||
private final ModerationFileService moderationFileService;
|
private final ModerationFileService moderationFileService;
|
||||||
|
|
||||||
private final CloudStorageService cloudStorageService;
|
private final CloudStorageService cloudStorageService;
|
||||||
|
|
||||||
private static final int MAX_SIZE_MB = 20;
|
private static final int MAX_SIZE_MB = 20;
|
||||||
|
|
||||||
private static final int FIRST_PART = 1;
|
private static final int FIRST_PART = 1;
|
||||||
|
|
||||||
private static final int SECOND_PART = 2;
|
private static final int SECOND_PART = 2;
|
||||||
|
|
||||||
@Value("${yandex.cloud.bucket-cold}")
|
@Value("${yandex.cloud.bucket-cold}")
|
||||||
@@ -63,10 +60,13 @@ public class ZipService {
|
|||||||
throw new IOException("Missing part 1 or part 2 for user: " + userId);
|
throw new IOException("Missing part 1 or part 2 for user: " + userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
String part1Content = cloudStorageService.downloadPartFromBucket(bucketCold, part1Key);
|
String part1Base64 = cloudStorageService.downloadPartFromBucket(bucketCold, part1Key);
|
||||||
String part2Content = cloudStorageService.downloadPartFromBucket(bucketCold, part2Key);
|
String part2Base64 = cloudStorageService.downloadPartFromBucket(bucketCold, part2Key);
|
||||||
|
|
||||||
String fullBase64 = part1Content + part2Content;
|
String fullBase64 = part1Base64 + part2Base64;
|
||||||
|
|
||||||
|
log.debug("Part1 length: {}, Part2 length: {}, Combined length: {}",
|
||||||
|
part1Base64.length(), part2Base64.length(), fullBase64.length());
|
||||||
|
|
||||||
return Base64.getDecoder().decode(fullBase64);
|
return Base64.getDecoder().decode(fullBase64);
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,6 @@ public class ZipService {
|
|||||||
.map(FileEntity::getFileSize)
|
.map(FileEntity::getFileSize)
|
||||||
.reduce(0L, Long::sum);
|
.reduce(0L, Long::sum);
|
||||||
|
|
||||||
|
|
||||||
if (totalSize > MAX_SIZE_MB * 1024 * 1024) {
|
if (totalSize > MAX_SIZE_MB * 1024 * 1024) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
String.format("Total size %.2f MB exceeds %d MB limit",
|
String.format("Total size %.2f MB exceeds %d MB limit",
|
||||||
@@ -86,8 +85,7 @@ public class ZipService {
|
|||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||||
|
for (FileEntity file : files) {
|
||||||
for (FileEntity file: files) {
|
|
||||||
String filePath = file.getFilePath();
|
String filePath = file.getFilePath();
|
||||||
String fileName = file.getStoredFileName();
|
String fileName = file.getStoredFileName();
|
||||||
|
|
||||||
@@ -105,18 +103,11 @@ public class ZipService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String[] splitBase64(String base64) {
|
private String[] splitBase64(String base64) {
|
||||||
StringBuilder part1 = new StringBuilder();
|
int half = base64.length() / 2;
|
||||||
StringBuilder part2 = new StringBuilder();
|
return new String[]{
|
||||||
|
base64.substring(0, half),
|
||||||
for (int i = 0; i < base64.length(); i++) {
|
base64.substring(half)
|
||||||
if (i % 2 == 0) {
|
};
|
||||||
part1.append(base64.charAt(i));
|
|
||||||
} else {
|
|
||||||
part2.append(base64.charAt(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new String[]{part1.toString(), part2.toString()};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveSplitFiles(Long userId, String part1, String part2) throws IOException {
|
private void saveSplitFiles(Long userId, String part1, String part2) throws IOException {
|
||||||
@@ -136,23 +127,7 @@ public class ZipService {
|
|||||||
|
|
||||||
moderationFileService.addFiles(userId, files);
|
moderationFileService.addFiles(userId, files);
|
||||||
|
|
||||||
log.info("Saved split files to cold storage for userId: {}", userId);
|
log.info("Saved split files to cold storage for userId: {}, Base64 length: {}",
|
||||||
}
|
userId, part1.length() + part2.length());
|
||||||
|
|
||||||
private String mergeBase64(String part1, String part2) {
|
|
||||||
StringBuilder merged = new StringBuilder();
|
|
||||||
|
|
||||||
int maxLength = Math.max(part1.length(), part2.length());
|
|
||||||
|
|
||||||
for (int i = 0; i < maxLength; i++) {
|
|
||||||
if (i < part1.length()) {
|
|
||||||
merged.append(part1.charAt(i));
|
|
||||||
}
|
|
||||||
if (i < part2.length()) {
|
|
||||||
merged.append(part2.charAt(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return merged.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user