(scanDirectoryPath, window = null)
| 2582 | restartHttpServerNow().catch((error) => { |
| 2583 | console.error('Error during server restart:', error); |
| 2584 | }); |
| 2585 | }, 100); |
| 2586 | return { success: true, message: 'Server restart initiated' }; |
| 2587 | } |
| 2588 | |
| 2589 | let db; |
| 2590 | let mainWindow; |
| 2591 | let isGeneratingHashes = false; // Track hash generation state |
| 2592 | let isHashGenerationScheduled = false; |
| 2593 | let isCompressingThumbnailsBackground = false; |
| 2594 | const THUMBNAIL_MIGRATION_DELAY_MS = Math.max( |
| 2595 | 15000, |
| 2596 | Number.parseInt(process.env.PRINTVENTORY_THUMBNAIL_MIGRATION_DELAY_MS || '30000', 10) || 30000 |
| 2597 | ); |
| 2598 | const THUMBNAIL_MIGRATION_MAX_PER_SESSION = Math.max( |
| 2599 | 25, |
| 2600 | Number.parseInt(process.env.PRINTVENTORY_THUMBNAIL_MIGRATION_MAX_PER_SESSION || '200', 10) || 200 |
| 2601 | ); |
| 2602 | const THUMBNAIL_MIGRATION_YIELD_MS = 25; |
| 2603 | |
| 2604 | function scheduleBackgroundThumbnailCompression(reason) { |
| 2605 | setTimeout(() => { |
| 2606 | compressExistingThumbnailsInBackground(reason).catch((error) => { |
| 2607 | console.error('Background thumbnail compression failed:', error); |
| 2608 | }); |
| 2609 | }, THUMBNAIL_MIGRATION_DELAY_MS); |
| 2610 | } |
| 2611 | |
| 2612 | function getThumbnailStoredLength(filePath) { |
| 2613 | if (!db || !filePath) return 0; |
| 2614 | const row = db.prepare('SELECT LENGTH(thumbnail) AS len FROM models WHERE filePath = ?').get(filePath); |
| 2615 | return row?.len ?? 0; |
| 2616 | } |
| 2617 | |
| 2618 | function clearThumbnailForPath(filePath, reason) { |
| 2619 | if (!db || !filePath) return false; |
| 2620 | try { |
| 2621 | const result = db.prepare('UPDATE models SET thumbnail = NULL WHERE filePath = ?').run(filePath); |
| 2622 | if (result.changes > 0) { |
| 2623 | console.warn(`Cleared thumbnail for ${filePath}${reason ? ` (${reason})` : ''}`); |
| 2624 | } |
| 2625 | return result.changes > 0; |
| 2626 | } catch (error) { |
| 2627 | console.error(`Failed to clear thumbnail for ${filePath}:`, error); |
| 2628 | return false; |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | function purgeCorruptThumbnailsOnly(maxChars = THUMBNAIL_ABSOLUTE_MAX_LOAD_CHARS) { |
| 2633 | if (!db) return 0; |
| 2634 | try { |
| 2635 | const result = db.prepare(` |
| 2636 | UPDATE models |
| 2637 | SET thumbnail = NULL |
| 2638 | WHERE thumbnail IS NOT NULL |
| 2639 | AND LENGTH(thumbnail) > ? |
| 2640 | `).run(maxChars); |
| 2641 | if (result.changes > 0) { |
no test coverage detected