TagSpaces is an offline, open source, document manager with tagging support
chore: improving comments
Ilian Sapundshiev73d45f233804
diff --git a/src/renderer/services/thumbsgenerator.ts b/src/renderer/services/thumbsgenerator.tsindex 696de1f13..4b0db122f 100644--- a/src/renderer/services/thumbsgenerator.ts+++ b/src/renderer/services/thumbsgenerator.ts@@ -279,10 +279,10 @@ function getPropertiesThumbnail(propertiesFile: string): string | null {const sectionHeader = '[InternetShortcut]';const startInx = propertiesFile.indexOf(sectionHeader);- // 1. If the section doesn't exist, exit immediately+ // If the section doesn't exist, exit immediatelyif (startInx === -1) return null;- // 2. Isolate the section body to avoid searching the whole file+ // Isolate the section body to avoid searching the whole file// Find the start of the next section or the end of the stringlet endInx = propertiesFile.indexOf('[', startInx + sectionHeader.length);const sectionBody =@@ -291,7 +291,7 @@ function getPropertiesThumbnail(propertiesFile: string): string | null {: propertiesFile.slice(startInx, endInx);/**- * 3. Targeted extraction+ * Targeted extraction* Match 'COMMENT' key followed by '=' and 'data:image/'* [ \t]* matches optional spaces/tabs around the equals sign* ([^ \n\r\t]+) captures the URI until the first whitespace or newline@@ -323,10 +323,10 @@ export async function resizeImg(img.onload = async () => {let bitmap: ImageBitmap | null = null;try {- // 1. Decode pixels off the main thread+ // Decode pixels off the main threadbitmap = await createImageBitmap(img);- // 2. Calculate dimensions using a more concise scale factor+ // Calculate dimensions using a more concise scale factor// This handles both landscape and portrait in one calculationconst scale = Math.min(maxSize / bitmap.width,@@ -336,12 +336,12 @@ export async function resizeImg(const width = Math.max(1, Math.round(bitmap.width * scale));const height = Math.max(1, Math.round(bitmap.height * scale));- // 3. Use OffscreenCanvas for better performance+ // Use OffscreenCanvas for better performanceconst canvas = new OffscreenCanvas(width, height);const ctx = canvas.getContext('2d');if (!ctx) throw new Error('Canvas context failed');- // 4. Draw and Export+ // Draw and Exportctx.drawImage(bitmap, 0, 0, width, height);const blob = await canvas.convertToBlob({@@ -402,19 +402,19 @@ export async function generateImageThumbnail(let objectURL: string | null = null;try {- // 1. Handle Remote URLs (Simple path)+ // Handle Remote URLs (Simple path)if (/^https?:\/\//.test(fileURL)) {return await getResizedImageThumbnail(fileURL, maxTmbSize);}- // 2. Handle Local Files+ // Handle Local Filesconst content = await getFileContentPromise(fileURL, 'arraybuffer');if (!content) return '';const ext = extractFileExtension(fileURL, dirSeparator).toLowerCase();const blob = new Blob([content], { type: getMimeType(ext) });- // 3. Create temporary URL+ // Create temporary URLif (AppConfig.isCordova) {// Assuming cordovaCreateObjectURL returns a Promise<string>objectURL = await cordovaCreateObjectURL(blob);@@ -424,10 +424,10 @@ export async function generateImageThumbnail(if (!objectURL) return '';- // 4. Generate the thumbnail+ // Generate the thumbnailconst thumbnail = await getResizedImageThumbnail(objectURL, maxTmbSize);- // 5. CRITICAL: Cleanup Memory+ // Cleanup Memory// We must revoke the URL after processing to free up the original image bytesif (objectURL && !AppConfig.isCordova) {URL.revokeObjectURL(objectURL);@@ -439,7 +439,7 @@ export async function generateImageThumbnail(console.error(`Error creating image thumb for : ${fileURL}`, e);return '';} finally {- // 6. Safety Cleanup for errors+ // Safety Cleanup for errorsif (objectURL && !AppConfig.isCordova) {URL.revokeObjectURL(objectURL);}@@ -492,15 +492,15 @@ export async function getResizedImageThumbnailOptimized(img.onload = async () => {let bitmap: ImageBitmap | null = null;try {- // 1. Decode the image+ // Decode the imagebitmap = await createImageBitmap(img);- // 2. Safety check: Ensure bitmap has dimensions+ // Safety check: Ensure bitmap has dimensionsif (!bitmap.width || !bitmap.height) {throw new Error('Image has no dimensions');}- // 3. Calculate Scale+ // Calculate Scale// Ensure maxSize is at least 1 to avoid division by zero or zero-size canvasconst maxSize = Math.max(1, maxTmbSize || 500);const scale = Math.min(@@ -509,12 +509,12 @@ export async function getResizedImageThumbnailOptimized(1,);- // 4. Force dimensions to be valid integers >= 1+ // Force dimensions to be valid integers >= 1// The || 1 handles cases where Math.round results in 0 or NaNconst width = Math.max(1, Math.round(bitmap.width * scale)) || 1;const height = Math.max(1, Math.round(bitmap.height * scale)) || 1;- // 5. Create Canvas+ // Create Canvasconst canvas = new OffscreenCanvas(width, height);const ctx = canvas.getContext('2d');if (!ctx) throw new Error('Canvas context unavailable');@@ -599,20 +599,20 @@ export async function getResizedImageThumbnail(export function getHtmlThumbnail(html: string): string | null {const startMarker = 'data-screenshot="';- // 1. Find the start index+ // Find the start indexconst inxBegin = html.indexOf(startMarker);if (inxBegin === -1) return null;- // 2. Find the end quote STARTING from the end of the marker+ // Find the end quote STARTING from the end of the markerconst valStart = inxBegin + startMarker.length;const inxEnd = html.indexOf('"', valStart);if (inxEnd === -1) return null;- // 3. Extract the exact slice+ // Extract the exact sliceconst imgDataUrl = html.slice(valStart, inxEnd).trim();- // 4. Validate prefix+ // Validate prefixif (imgDataUrl.startsWith('data:image/')) {return imgDataUrl;}@@ -876,16 +876,16 @@ export async function generateTextThumbnail(loadTextFilePromise: (url: string, asText: boolean) => Promise<string>,): Promise<string> {try {- // 1. Load content+ // Load contentconst rawContent = await loadTextFilePromise(fileURL, true);if (!rawContent) return '';- // 2. PERFORMANCE: Only process the start of the file+ // PERFORMANCE: Only process the start of the file// Splitting a 10MB file into lines would crash the appconst previewText = rawContent.substring(0, 2500);const lines = previewText.split('\n').slice(0, 15);- // 3. Initialize Canvas (with Fallback)+ // Initialize Canvas (with Fallback)let canvas: any;let isOffscreen = false;@@ -901,7 +901,7 @@ export async function generateTextThumbnail(const ctx = canvas.getContext('2d');if (!ctx) return '';- // 4. Background and Typography Setup+ // Background and Typography Setupctx.fillStyle = '#ffffff';ctx.fillRect(0, 0, maxSize, maxSize);@@ -912,7 +912,7 @@ export async function generateTextThumbnail(ctx.fillStyle = '#222222';ctx.font = `${fontSize}px sans-serif`;- // 5. Draw Lines+ // Draw Linesfor (let i = 0; i < lines.length; i++) {const y = padding + (i + 1) * lineHeight;if (y > maxSize - padding) break; // Stop if we go off canvas@@ -924,7 +924,7 @@ export async function generateTextThumbnail(}}- // 6. Export to DataURL+ // Export to DataURLif (isOffscreen) {// OffscreenCanvas uses asynchronous blob conversionconst blob = await (canvas as OffscreenCanvas).convertToBlob({