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

This commit is contained in:
2026-05-04 23:19:55 +02:00
parent 9a1095ad6e
commit 84a349fd35
@@ -301,11 +301,6 @@ export class EmailImportService {
}[]; }[];
emailDate: string; emailDate: string;
}): Promise<{ success: boolean; results: any[] }> { }): 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 tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'paperless-mail-import-'));
const results = []; const results = [];
@@ -331,21 +326,31 @@ export class EmailImportService {
if (att.splitRanges && att.splitRanges.length > 0) { if (att.splitRanges && att.splitRanges.length > 0) {
// SPLIT PDF // SPLIT PDF
const pdfDoc = await PDFDocument.load(originalPdfBytes, { ignoreEncryption: true }); const pdfDoc = await PDFDocument.load(originalPdfBytes, { ignoreEncryption: true });
const totalPages = pdfDoc.getPageCount();
for (const range of att.splitRanges) { 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(); const newPdf = await PDFDocument.create();
// Pages are 0-indexed in pdf-lib // 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); const copiedPages = await newPdf.copyPages(pdfDoc, pageIndices);
copiedPages.forEach((p) => newPdf.addPage(p)); copiedPages.forEach((p) => newPdf.addPage(p));
const splitPdfBytes = await newPdf.save(); 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)); await fs.writeFile(tempFilePath, Buffer.from(splitPdfBytes));
uploadPromises.push({ uploadPromises.push({
path: tempFilePath, path: tempFilePath,
filename: `${baseFilename}_${range.start}-${range.end}`, filename: `${baseFilename}_${start}-${end}`,
rangeKey: `${range.start}-${range.end}`, rangeKey: `${start}-${end}`,
}); });
} }
} else { } else {
@@ -397,7 +402,8 @@ export class EmailImportService {
await new Promise(resolve => setTimeout(resolve, 2000)); await new Promise(resolve => setTimeout(resolve, 2000));
try { try {
const taskStatus = await this.paperlessService.getTask(paperlessTaskId); const taskStatus = await this.paperlessService.getTask(paperlessTaskId);
const statusObj = Array.isArray(taskStatus) ? taskStatus[0] : taskStatus; // Paperless returns { results: [ ... ] } for filtered tasks
const statusObj = taskStatus.results ? taskStatus.results[0] : (Array.isArray(taskStatus) ? taskStatus[0] : taskStatus);
if (statusObj && statusObj.related_document) { if (statusObj && statusObj.related_document) {
docId = statusObj.related_document; docId = statusObj.related_document;
break; break;