Add password prompt functionality and tests while uploading encrypted PDF

This commit is contained in:
alam00000 committed 2026-03-26 12:11:12 +05:30
1 parent f88f872162
commit 1dbf907eeb
79 files changed
+7869 -4419

No files matched your search

+43 -25
View File
@@ -1,13 +1,20 @@
import { showLoader, hideLoader, showAlert } from '../ui.js';
import { downloadFile, getPDFDocument } from '../utils/helpers.js';
import { downloadFile } from '../utils/helpers.js';
import { state } from '../state.js';
import { renderPagesProgressively, cleanupLazyRendering } from '../utils/render-utils.js';
import { loadPdfWithPasswordPrompt } from '../utils/password-prompt.js';
import {
renderPagesProgressively,
cleanupLazyRendering,
} from '../utils/render-utils.js';
import Sortable from 'sortablejs';
import { icons, createIcons } from 'lucide';
import { PDFDocument as PDFLibDocument } from 'pdf-lib';
import * as pdfjsLib from 'pdfjs-dist';
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString();
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url
).toString();
const duplicateOrganizeState = {
sortableInstances: {},
@@ -91,7 +98,15 @@ export async function renderDuplicateOrganizeThumbnails() {
showLoader('Rendering page previews...');
const pdfData = await state.pdfDoc.save();
const pdfjsDoc = await getPDFDocument({ data: pdfData }).promise;
hideLoader();
const loadResult = await loadPdfWithPasswordPrompt(
state.files[0],
state.files,
0
);
if (!loadResult) return;
showLoader('Rendering page previews...');
const pdfjsDoc = loadResult.pdf;
grid.textContent = '';
@@ -148,22 +163,17 @@ export async function renderDuplicateOrganizeThumbnails() {
try {
// Render pages progressively with lazy loading
await renderPagesProgressively(
pdfjsDoc,
grid,
createWrapper,
{
batchSize: 8,
useLazyLoading: true,
lazyLoadMargin: '400px',
onProgress: (current, total) => {
showLoader(`Rendering page previews: ${current}/${total}`);
},
onBatchComplete: () => {
createIcons({ icons });
}
}
);
await renderPagesProgressively(pdfjsDoc, grid, createWrapper, {
batchSize: 8,
useLazyLoading: true,
lazyLoadMargin: '400px',
onProgress: (current, total) => {
showLoader(`Rendering page previews: ${current}/${total}`);
},
onBatchComplete: () => {
createIcons({ icons });
},
});
initializePageGridSortable();
} catch (error) {
@@ -181,8 +191,10 @@ export async function processAndSave() {
const finalPageElements = grid.querySelectorAll('.page-thumbnail');
const finalIndices = Array.from(finalPageElements)
.map((el) => parseInt((el as HTMLElement).dataset.originalPageIndex || '', 10))
.filter(index => !isNaN(index) && index >= 0);
.map((el) =>
parseInt((el as HTMLElement).dataset.originalPageIndex || '', 10)
)
.filter((index) => !isNaN(index) && index >= 0);
console.log('Saving PDF with indices:', finalIndices);
console.log('Original PDF Page Count:', state.pdfDoc?.getPageCount());
@@ -195,10 +207,13 @@ export async function processAndSave() {
const newPdfDoc = await PDFLibDocument.create();
const totalPages = state.pdfDoc.getPageCount();
const invalidIndices = finalIndices.filter(i => i >= totalPages);
const invalidIndices = finalIndices.filter((i) => i >= totalPages);
if (invalidIndices.length > 0) {
console.error('Found invalid indices:', invalidIndices);
showAlert('Error', 'Some pages could not be processed. Please try again.');
showAlert(
'Error',
'Some pages could not be processed. Please try again.'
);
return;
}
@@ -212,7 +227,10 @@ export async function processAndSave() {
);
} catch (e) {
console.error('Save error:', e);
showAlert('Error', 'Failed to save the new PDF. Check console for details.');
showAlert(
'Error',
'Failed to save the new PDF. Check console for details.'
);
} finally {
hideLoader();
}