cleanup
This commit is contained in:
parent
b5423f3434
commit
03a8f45128
3 changed files with 50 additions and 55 deletions
|
@ -70,7 +70,7 @@ public class SplitPdfBySectionsController {
|
||||||
for (int i = 0; i < splitDocumentsBoas.size(); i++) {
|
for (int i = 0; i < splitDocumentsBoas.size(); i++) {
|
||||||
ByteArrayOutputStream baos = splitDocumentsBoas.get(i);
|
ByteArrayOutputStream baos = splitDocumentsBoas.get(i);
|
||||||
int sectionNum = (i % (horiz * verti)) + 1;
|
int sectionNum = (i % (horiz * verti)) + 1;
|
||||||
String fileName = filename + "_" + pageNum + "_" + sectionNum + ".pdf";
|
String fileName = filename + "_" + pageNum + "_" + sectionNum + ".pdf";
|
||||||
byte[] pdf = baos.toByteArray();
|
byte[] pdf = baos.toByteArray();
|
||||||
ZipEntry pdfEntry = new ZipEntry(fileName);
|
ZipEntry pdfEntry = new ZipEntry(fileName);
|
||||||
zipOut.putNextEntry(pdfEntry);
|
zipOut.putNextEntry(pdfEntry);
|
||||||
|
@ -137,7 +137,4 @@ public class SplitPdfBySectionsController {
|
||||||
|
|
||||||
return splitDocuments;
|
return splitDocuments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,11 @@ package stirling.software.SPDF.controller.api.pipeline;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -82,46 +82,45 @@ public class PipelineController {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a ByteArrayOutputStream to hold the zip
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
ZipOutputStream zipOut = new ZipOutputStream(baos);
|
||||||
|
|
||||||
// Create a ByteArrayOutputStream to hold the zip
|
// A map to keep track of filenames and their counts
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
Map<String, Integer> filenameCount = new HashMap<>();
|
||||||
ZipOutputStream zipOut = new ZipOutputStream(baos);
|
|
||||||
|
|
||||||
// A map to keep track of filenames and their counts
|
// Loop through each file and add it to the zip
|
||||||
Map<String, Integer> filenameCount = new HashMap<>();
|
for (Resource file : outputFiles) {
|
||||||
|
String originalFilename = file.getFilename();
|
||||||
|
String filename = originalFilename;
|
||||||
|
|
||||||
// Loop through each file and add it to the zip
|
// Check if the filename already exists, and modify it if necessary
|
||||||
for (Resource file : outputFiles) {
|
if (filenameCount.containsKey(originalFilename)) {
|
||||||
String originalFilename = file.getFilename();
|
int count = filenameCount.get(originalFilename);
|
||||||
String filename = originalFilename;
|
String baseName = originalFilename.replaceAll("\\.[^.]*$", "");
|
||||||
|
String extension = originalFilename.replaceAll("^.*\\.", "");
|
||||||
|
filename = baseName + "(" + count + ")." + extension;
|
||||||
|
filenameCount.put(originalFilename, count + 1);
|
||||||
|
} else {
|
||||||
|
filenameCount.put(originalFilename, 1);
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the filename already exists, and modify it if necessary
|
ZipEntry zipEntry = new ZipEntry(filename);
|
||||||
if (filenameCount.containsKey(originalFilename)) {
|
zipOut.putNextEntry(zipEntry);
|
||||||
int count = filenameCount.get(originalFilename);
|
|
||||||
String baseName = originalFilename.replaceAll("\\.[^.]*$", "");
|
|
||||||
String extension = originalFilename.replaceAll("^.*\\.", "");
|
|
||||||
filename = baseName + "(" + count + ")." + extension;
|
|
||||||
filenameCount.put(originalFilename, count + 1);
|
|
||||||
} else {
|
|
||||||
filenameCount.put(originalFilename, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
ZipEntry zipEntry = new ZipEntry(filename);
|
// Read the file into a byte array
|
||||||
zipOut.putNextEntry(zipEntry);
|
InputStream is = file.getInputStream();
|
||||||
|
byte[] bytes = new byte[(int) file.contentLength()];
|
||||||
|
is.read(bytes);
|
||||||
|
|
||||||
// Read the file into a byte array
|
// Write the bytes of the file to the zip
|
||||||
InputStream is = file.getInputStream();
|
zipOut.write(bytes, 0, bytes.length);
|
||||||
byte[] bytes = new byte[(int) file.contentLength()];
|
zipOut.closeEntry();
|
||||||
is.read(bytes);
|
|
||||||
|
|
||||||
// Write the bytes of the file to the zip
|
is.close();
|
||||||
zipOut.write(bytes, 0, bytes.length);
|
}
|
||||||
zipOut.closeEntry();
|
|
||||||
|
|
||||||
is.close();
|
zipOut.close();
|
||||||
}
|
|
||||||
|
|
||||||
zipOut.close();
|
|
||||||
|
|
||||||
logger.info("Returning zipped file response...");
|
logger.info("Returning zipped file response...");
|
||||||
return WebResponseUtils.boasToWebResponse(
|
return WebResponseUtils.boasToWebResponse(
|
||||||
|
|
|
@ -223,7 +223,7 @@ public class PipelineProcessor {
|
||||||
// If the operation is "auto-rename", generate a new filename.
|
// If the operation is "auto-rename", generate a new filename.
|
||||||
// This is a simple example of generating a filename using current timestamp.
|
// This is a simple example of generating a filename using current timestamp.
|
||||||
// Modify as per your needs.
|
// Modify as per your needs.
|
||||||
|
|
||||||
newFilename = extractFilename(response);
|
newFilename = extractFilename(response);
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, keep the original filename.
|
// Otherwise, keep the original filename.
|
||||||
|
@ -248,28 +248,27 @@ public class PipelineProcessor {
|
||||||
return newOutputFiles;
|
return newOutputFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String extractFilename(ResponseEntity<byte[]> response) {
|
||||||
|
String filename = "default-filename.ext"; // Default filename if not found
|
||||||
|
|
||||||
public String extractFilename(ResponseEntity<byte[]> response) {
|
HttpHeaders headers = response.getHeaders();
|
||||||
String filename = "default-filename.ext"; // Default filename if not found
|
String contentDisposition = headers.getFirst(HttpHeaders.CONTENT_DISPOSITION);
|
||||||
|
|
||||||
HttpHeaders headers = response.getHeaders();
|
if (contentDisposition != null && !contentDisposition.isEmpty()) {
|
||||||
String contentDisposition = headers.getFirst(HttpHeaders.CONTENT_DISPOSITION);
|
String[] parts = contentDisposition.split(";");
|
||||||
|
for (String part : parts) {
|
||||||
|
if (part.trim().startsWith("filename")) {
|
||||||
|
// Extracts filename and removes quotes if present
|
||||||
|
filename = part.split("=")[1].trim().replace("\"", "");
|
||||||
|
filename = URLDecoder.decode(filename, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
if (contentDisposition != null && !contentDisposition.isEmpty()) {
|
break;
|
||||||
String[] parts = contentDisposition.split(";");
|
}
|
||||||
for (String part : parts) {
|
|
||||||
if (part.trim().startsWith("filename")) {
|
|
||||||
// Extracts filename and removes quotes if present
|
|
||||||
filename = part.split("=")[1].trim().replace("\"", "");
|
|
||||||
filename = URLDecoder.decode(filename, StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Resource> generateInputFiles(File[] files) throws Exception {
|
List<Resource> generateInputFiles(File[] files) throws Exception {
|
||||||
if (files == null || files.length == 0) {
|
if (files == null || files.length == 0) {
|
||||||
|
|
Loading…
Reference in a new issue