mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-26 14:19:24 +00:00
Fix image thumbnail persistance
This commit is contained in:
parent
cbaa945b7e
commit
c0a08598dd
@ -25,6 +25,7 @@ interface IndexedDBContextValue {
|
|||||||
|
|
||||||
// Utilities
|
// Utilities
|
||||||
getStorageStats: () => Promise<{ used: number; available: number; fileCount: number }>;
|
getStorageStats: () => Promise<{ used: number; available: number; fileCount: number }>;
|
||||||
|
updateThumbnail: (fileId: FileId, thumbnail: string) => Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const IndexedDBContext = createContext<IndexedDBContextValue | null>(null);
|
const IndexedDBContext = createContext<IndexedDBContextValue | null>(null);
|
||||||
@ -174,6 +175,10 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) {
|
|||||||
return await fileStorage.getStorageStats();
|
return await fileStorage.getStorageStats();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const updateThumbnail = useCallback(async (fileId: FileId, thumbnail: string): Promise<boolean> => {
|
||||||
|
return await fileStorage.updateThumbnail(fileId, thumbnail);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const value: IndexedDBContextValue = {
|
const value: IndexedDBContextValue = {
|
||||||
saveFile,
|
saveFile,
|
||||||
loadFile,
|
loadFile,
|
||||||
@ -182,7 +187,8 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) {
|
|||||||
loadAllMetadata,
|
loadAllMetadata,
|
||||||
deleteMultiple,
|
deleteMultiple,
|
||||||
clearAll,
|
clearAll,
|
||||||
getStorageStats
|
getStorageStats,
|
||||||
|
updateThumbnail
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -238,15 +238,6 @@ export async function addFiles(
|
|||||||
|
|
||||||
const record = toFileRecord(file, fileId);
|
const record = toFileRecord(file, fileId);
|
||||||
|
|
||||||
// Restore metadata from storage
|
|
||||||
if (metadata.thumbnail) {
|
|
||||||
record.thumbnailUrl = metadata.thumbnail;
|
|
||||||
// Track blob URLs for cleanup (images return blob URLs that need revocation)
|
|
||||||
if (metadata.thumbnail.startsWith('blob:')) {
|
|
||||||
lifecycleManager.trackBlobUrl(metadata.thumbnail);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate processedFile metadata for stored files
|
// Generate processedFile metadata for stored files
|
||||||
let pageCount: number = 1;
|
let pageCount: number = 1;
|
||||||
|
|
||||||
@ -271,6 +262,15 @@ export async function addFiles(
|
|||||||
if (DEBUG) console.log(`📄 addFiles(stored): Non-PDF file ${file.name}, no page count`);
|
if (DEBUG) console.log(`📄 addFiles(stored): Non-PDF file ${file.name}, no page count`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore metadata from storage
|
||||||
|
if (metadata.thumbnail) {
|
||||||
|
record.thumbnailUrl = metadata.thumbnail;
|
||||||
|
// Track blob URLs for cleanup (images return blob URLs that need revocation)
|
||||||
|
if (metadata.thumbnail.startsWith('blob:')) {
|
||||||
|
lifecycleManager.trackBlobUrl(metadata.thumbnail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create processedFile metadata with correct page count
|
// Create processedFile metadata with correct page count
|
||||||
if (pageCount > 0) {
|
if (pageCount > 0) {
|
||||||
record.processedFile = createProcessedFile(pageCount, metadata.thumbnail);
|
record.processedFile = createProcessedFile(pageCount, metadata.thumbnail);
|
||||||
|
@ -66,6 +66,15 @@ export function useIndexedDBThumbnail(file: FileMetadata | undefined | null): {
|
|||||||
const thumbnail = await generateThumbnailForFile(fileObject);
|
const thumbnail = await generateThumbnailForFile(fileObject);
|
||||||
if (!cancelled) {
|
if (!cancelled) {
|
||||||
setThumb(thumbnail);
|
setThumb(thumbnail);
|
||||||
|
|
||||||
|
// Save thumbnail to IndexedDB for persistence
|
||||||
|
if (file.id && indexedDB && thumbnail) {
|
||||||
|
try {
|
||||||
|
await indexedDB.updateThumbnail(file.id, thumbnail);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Failed to save thumbnail to IndexedDB:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('Failed to generate thumbnail for file', file.name, error);
|
console.warn('Failed to generate thumbnail for file', file.name, error);
|
||||||
|
@ -333,9 +333,14 @@ export async function generateThumbnailForFile(file: File): Promise<string> {
|
|||||||
return generatePlaceholderThumbnail(file);
|
return generatePlaceholderThumbnail(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle image files - creates blob URL that needs cleanup by caller
|
// Handle image files - convert to data URL for persistence
|
||||||
if (file.type.startsWith('image/')) {
|
if (file.type.startsWith('image/')) {
|
||||||
return URL.createObjectURL(file);
|
return new Promise((resolve, reject) => {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = () => resolve(reader.result as string);
|
||||||
|
reader.onerror = () => reject(reader.error);
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle PDF files
|
// Handle PDF files
|
||||||
|
Loading…
x
Reference in New Issue
Block a user