mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-11-16 01:21:16 +01:00
## default
<img width="1012" height="627"
alt="{BF57458D-50A6-4057-94F1-D6AB4628EFD8}"
src="https://github.com/user-attachments/assets/85e550ab-0aed-4341-be95-d5d3bc7146db"
/>
## disabled
<img width="1141" height="620"
alt="{140DB87B-05CF-4E0E-A14A-ED15075BD2EE}"
src="https://github.com/user-attachments/assets/e0f56e84-fb9d-4787-b5cb-ba7c5a54b1e1"
/>
## unzip options
<img width="530" height="255"
alt="{482CE185-73D5-4D90-91BB-B9305C711391}"
src="https://github.com/user-attachments/assets/609b18ee-4eae-4cee-afc1-5db01f9d1088"
/>
<img width="579" height="473"
alt="{4DFCA96D-792D-4370-8C62-4BA42C9F1A5F}"
src="https://github.com/user-attachments/assets/c67fa4af-04ef-41df-9420-65ce4247e25b"
/>
## pop up and maintains version metadata
<img width="1071" height="1220"
alt="{7F2A785C-5717-4A79-9D45-74BDA46DF273}"
src="https://github.com/user-attachments/assets/9374cd2a-b7e5-46c4-a722-e141ab42f0de"
/>
---------
Co-authored-by: Connor Yoh <connor@stirlingpdf.com>
159 lines
5.6 KiB
TypeScript
159 lines
5.6 KiB
TypeScript
import { useState, useCallback, useEffect, useRef } from 'react';
|
|
import { generateThumbnailForFile, generateThumbnailWithMetadata, ThumbnailWithMetadata } from '../../../utils/thumbnailUtils';
|
|
import { zipFileService } from '../../../services/zipFileService';
|
|
import { usePreferences } from '../../../contexts/PreferencesContext';
|
|
|
|
|
|
export const useToolResources = () => {
|
|
const { preferences } = usePreferences();
|
|
const [blobUrls, setBlobUrls] = useState<string[]>([]);
|
|
|
|
const addBlobUrl = useCallback((url: string) => {
|
|
setBlobUrls(prev => [...prev, url]);
|
|
}, []);
|
|
|
|
const cleanupBlobUrls = useCallback(() => {
|
|
setBlobUrls(prev => {
|
|
prev.forEach(url => {
|
|
try {
|
|
URL.revokeObjectURL(url);
|
|
} catch (error) {
|
|
console.warn('Failed to revoke blob URL:', error);
|
|
}
|
|
});
|
|
return [];
|
|
});
|
|
}, []); // No dependencies - use functional update pattern
|
|
|
|
// Cleanup on unmount - use ref to avoid dependency on blobUrls state
|
|
const blobUrlsRef = useRef<string[]>([]);
|
|
|
|
useEffect(() => {
|
|
blobUrlsRef.current = blobUrls;
|
|
}, [blobUrls]);
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
blobUrlsRef.current.forEach(url => {
|
|
try {
|
|
URL.revokeObjectURL(url);
|
|
} catch (error) {
|
|
console.warn('Failed to revoke blob URL during cleanup:', error);
|
|
}
|
|
});
|
|
};
|
|
}, []); // No dependencies - use ref to access current URLs
|
|
|
|
const generateThumbnails = useCallback(async (files: File[]): Promise<string[]> => {
|
|
console.log(`🖼️ useToolResources.generateThumbnails: Starting for ${files.length} files`);
|
|
const thumbnails: string[] = [];
|
|
|
|
for (const file of files) {
|
|
try {
|
|
console.log(`🖼️ Generating thumbnail for: ${file.name} (${file.type}, ${file.size} bytes)`);
|
|
const thumbnail = await generateThumbnailForFile(file);
|
|
console.log(`🖼️ Generated thumbnail for ${file.name}: SUCCESS`);
|
|
thumbnails.push(thumbnail);
|
|
} catch (error) {
|
|
console.warn(`🖼️ Failed to generate thumbnail for ${file.name}:`, error);
|
|
thumbnails.push('');
|
|
}
|
|
}
|
|
|
|
return thumbnails;
|
|
}, []);
|
|
|
|
const generateThumbnailsWithMetadata = useCallback(async (files: File[]): Promise<ThumbnailWithMetadata[]> => {
|
|
console.log(`🖼️ useToolResources.generateThumbnailsWithMetadata: Starting for ${files.length} files`);
|
|
const results: ThumbnailWithMetadata[] = [];
|
|
|
|
for (const file of files) {
|
|
try {
|
|
console.log(`🖼️ Generating thumbnail with metadata for: ${file.name} (${file.type}, ${file.size} bytes)`);
|
|
const result = await generateThumbnailWithMetadata(file);
|
|
console.log(`🖼️ Generated thumbnail with metadata for ${file.name}: SUCCESS, ${result.pageCount} pages`);
|
|
results.push(result);
|
|
} catch (error) {
|
|
console.warn(`🖼️ Failed to generate thumbnail with metadata for ${file.name}:`, error);
|
|
results.push({ thumbnail: '', pageCount: 1 });
|
|
}
|
|
}
|
|
|
|
console.log(`🖼️ useToolResources.generateThumbnailsWithMetadata: Complete. Generated ${results.length}/${files.length} thumbnails with metadata`);
|
|
return results;
|
|
}, []);
|
|
|
|
const extractZipFiles = useCallback(async (zipBlob: Blob, skipAutoUnzip = false): Promise<File[]> => {
|
|
try {
|
|
// Check if we should extract based on preferences
|
|
const shouldExtract = await zipFileService.shouldUnzip(
|
|
zipBlob,
|
|
preferences.autoUnzip,
|
|
preferences.autoUnzipFileLimit,
|
|
skipAutoUnzip
|
|
);
|
|
|
|
if (!shouldExtract) {
|
|
return [new File([zipBlob], 'result.zip', { type: 'application/zip' })];
|
|
}
|
|
|
|
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 [];
|
|
}
|
|
}, [preferences.autoUnzip, preferences.autoUnzipFileLimit]);
|
|
|
|
const extractAllZipFiles = useCallback(async (zipBlob: Blob, skipAutoUnzip = false): Promise<File[]> => {
|
|
try {
|
|
// Check if we should extract based on preferences
|
|
const shouldExtract = await zipFileService.shouldUnzip(
|
|
zipBlob,
|
|
preferences.autoUnzip,
|
|
preferences.autoUnzipFileLimit,
|
|
skipAutoUnzip
|
|
);
|
|
|
|
if (!shouldExtract) {
|
|
return [new File([zipBlob], 'result.zip', { type: 'application/zip' })];
|
|
}
|
|
|
|
const zipFile = new File([zipBlob], 'temp.zip', { type: 'application/zip' });
|
|
const extractionResult = await zipFileService.extractAllFiles(zipFile);
|
|
return extractionResult.success ? extractionResult.extractedFiles : [];
|
|
} catch (error) {
|
|
console.error('useToolResources.extractAllZipFiles - Error:', error);
|
|
return [];
|
|
}
|
|
}, [preferences.autoUnzip, preferences.autoUnzipFileLimit]);
|
|
|
|
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,
|
|
generateThumbnailsWithMetadata,
|
|
createDownloadInfo,
|
|
extractZipFiles,
|
|
extractAllZipFiles,
|
|
cleanupBlobUrls,
|
|
};
|
|
};
|