refactor: improve PDF splitting logic with bounds checking and fix task status response parsing
Build and Push Multi-Platform Images / build-and-push (push) Successful in 30s
Build and Push Multi-Platform Images / build-and-push (push) Successful in 30s
This commit is contained in:
@@ -301,11 +301,6 @@ export class EmailImportService {
|
||||
}[];
|
||||
emailDate: string;
|
||||
}): Promise<{ success: boolean; results: any[] }> {
|
||||
const fs = require('fs/promises');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'paperless-mail-import-'));
|
||||
const results = [];
|
||||
|
||||
@@ -331,21 +326,31 @@ export class EmailImportService {
|
||||
if (att.splitRanges && att.splitRanges.length > 0) {
|
||||
// SPLIT PDF
|
||||
const pdfDoc = await PDFDocument.load(originalPdfBytes, { ignoreEncryption: true });
|
||||
const totalPages = pdfDoc.getPageCount();
|
||||
|
||||
for (const range of att.splitRanges) {
|
||||
const start = Math.max(1, range.start);
|
||||
const end = Math.min(range.end, totalPages);
|
||||
|
||||
if (start > end) {
|
||||
this.logger.warn(`Ungültiger Bereich für Splitting: ${start}-${end} (Seiten gesamt: ${totalPages})`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const newPdf = await PDFDocument.create();
|
||||
// Pages are 0-indexed in pdf-lib
|
||||
const pageIndices = Array.from({ length: range.end - range.start + 1 }, (_, i) => range.start - 1 + i);
|
||||
const pageIndices = Array.from({ length: end - start + 1 }, (_, i) => start - 1 + i);
|
||||
const copiedPages = await newPdf.copyPages(pdfDoc, pageIndices);
|
||||
copiedPages.forEach((p) => newPdf.addPage(p));
|
||||
|
||||
const splitPdfBytes = await newPdf.save();
|
||||
const tempFilePath = path.join(tempDir, `${baseFilename}_${range.start}-${range.end}.pdf`);
|
||||
const tempFilePath = path.join(tempDir, `${baseFilename}_${start}-${end}.pdf`);
|
||||
await fs.writeFile(tempFilePath, Buffer.from(splitPdfBytes));
|
||||
|
||||
uploadPromises.push({
|
||||
path: tempFilePath,
|
||||
filename: `${baseFilename}_${range.start}-${range.end}`,
|
||||
rangeKey: `${range.start}-${range.end}`,
|
||||
filename: `${baseFilename}_${start}-${end}`,
|
||||
rangeKey: `${start}-${end}`,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@@ -395,10 +400,11 @@ export class EmailImportService {
|
||||
let docId = null;
|
||||
for (let i = 0; i < 30; i++) {
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
try {
|
||||
const taskStatus = await this.paperlessService.getTask(paperlessTaskId);
|
||||
const statusObj = Array.isArray(taskStatus) ? taskStatus[0] : taskStatus;
|
||||
if (statusObj && statusObj.related_document) {
|
||||
try {
|
||||
const taskStatus = await this.paperlessService.getTask(paperlessTaskId);
|
||||
// Paperless returns { results: [ ... ] } for filtered tasks
|
||||
const statusObj = taskStatus.results ? taskStatus.results[0] : (Array.isArray(taskStatus) ? taskStatus[0] : taskStatus);
|
||||
if (statusObj && statusObj.related_document) {
|
||||
docId = statusObj.related_document;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user