mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-22 04:09:22 +00:00

# Description of Changes <!-- Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --> --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: Reece Browne <you@example.com>
114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
import { useState, useCallback, useEffect } from 'react';
|
|
import { generateThumbnailForFile } from '../../../utils/thumbnailUtils';
|
|
import { zipFileService } from '../../../services/zipFileService';
|
|
|
|
|
|
export const useToolResources = () => {
|
|
const [blobUrls, setBlobUrls] = useState<string[]>([]);
|
|
|
|
const addBlobUrl = useCallback((url: string) => {
|
|
setBlobUrls(prev => [...prev, url]);
|
|
}, []);
|
|
|
|
const cleanupBlobUrls = useCallback(() => {
|
|
blobUrls.forEach(url => {
|
|
try {
|
|
URL.revokeObjectURL(url);
|
|
} catch (error) {
|
|
console.warn('Failed to revoke blob URL:', error);
|
|
}
|
|
});
|
|
setBlobUrls([]);
|
|
}, [blobUrls]);
|
|
|
|
// Cleanup on unmount
|
|
useEffect(() => {
|
|
return () => {
|
|
blobUrls.forEach(url => {
|
|
try {
|
|
URL.revokeObjectURL(url);
|
|
} catch (error) {
|
|
console.warn('Failed to revoke blob URL during cleanup:', error);
|
|
}
|
|
});
|
|
};
|
|
}, [blobUrls]);
|
|
|
|
const generateThumbnails = useCallback(async (files: File[]): Promise<string[]> => {
|
|
const thumbnails: string[] = [];
|
|
|
|
for (const file of files) {
|
|
try {
|
|
const thumbnail = await generateThumbnailForFile(file);
|
|
thumbnails.push(thumbnail);
|
|
} catch (error) {
|
|
console.warn(`Failed to generate thumbnail for ${file.name}:`, error);
|
|
thumbnails.push('');
|
|
}
|
|
}
|
|
|
|
return thumbnails;
|
|
}, []);
|
|
|
|
const extractZipFiles = useCallback(async (zipBlob: Blob): Promise<File[]> => {
|
|
try {
|
|
const zipFile = new File([zipBlob], 'temp.zip', { type: 'application/zip' });
|
|
const extractionResult = await zipFileService.extractPdfFiles(zipFile);
|
|
return extractionResult.success ? extractionResult.extractedFiles : [];
|
|
} catch (error) {
|
|
console.error('useToolResources.extractZipFiles - Error:', error);
|
|
return [];
|
|
}
|
|
}, []);
|
|
|
|
const extractAllZipFiles = useCallback(async (zipBlob: Blob): Promise<File[]> => {
|
|
try {
|
|
const JSZip = (await import('jszip')).default;
|
|
const zip = new JSZip();
|
|
|
|
const arrayBuffer = await zipBlob.arrayBuffer();
|
|
const zipContent = await zip.loadAsync(arrayBuffer);
|
|
|
|
const extractedFiles: File[] = [];
|
|
|
|
for (const [filename, file] of Object.entries(zipContent.files)) {
|
|
if (!file.dir) {
|
|
const content = await file.async('blob');
|
|
const extractedFile = new File([content], filename, { type: 'application/pdf' });
|
|
extractedFiles.push(extractedFile);
|
|
}
|
|
}
|
|
|
|
return extractedFiles;
|
|
} catch (error) {
|
|
console.error('Error in extractAllZipFiles:', error);
|
|
return [];
|
|
}
|
|
}, []);
|
|
|
|
const createDownloadInfo = useCallback(async (
|
|
files: File[],
|
|
operationType: string
|
|
): Promise<{ url: string; filename: string }> => {
|
|
if (files.length === 1) {
|
|
const url = URL.createObjectURL(files[0]);
|
|
addBlobUrl(url);
|
|
return { url, filename: files[0].name };
|
|
}
|
|
|
|
// Multiple files - create zip using shared service
|
|
const { zipFile } = await zipFileService.createZipFromFiles(files, `${operationType}_results.zip`);
|
|
const url = URL.createObjectURL(zipFile);
|
|
addBlobUrl(url);
|
|
|
|
return { url, filename: zipFile.name };
|
|
}, [addBlobUrl]);
|
|
|
|
return {
|
|
generateThumbnails,
|
|
createDownloadInfo,
|
|
extractZipFiles,
|
|
extractAllZipFiles,
|
|
cleanupBlobUrls,
|
|
};
|
|
}; |