diff --git a/e2e/assets.test.ts b/e2e/assets.test.ts new file mode 100644 index 000000000..401ab7d80 --- /dev/null +++ b/e2e/assets.test.ts @@ -0,0 +1,69 @@ +import { copyFile, mkdir, mkdtemp, rm } from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' +import { test, expect } from '@playwright/test' +import { launchApp, type AppContext } from './launchApp' + +const SIDEBAR_ASSETS = '[data-testid="app-sidebar-assets"]' +const ASSETS_GRID = '[data-testid="assets-grid"]' +const SAMPLE_IMAGE = path.resolve('src/renderer/src/assets/sidebar/hills-sunset.jpg') +const SAMPLE_VIDEO = path.resolve('src/renderer/public/install-showcase-scene/Assets/earpods.webm') +const SAMPLE_VIDEO_SHAPE = 714 / 530 + +let ctx: AppContext +let outputDir: string + +test.describe.configure({ mode: 'serial' }) + +test.beforeAll(async () => { + outputDir = await mkdtemp(path.join(os.tmpdir(), 'comfy-assets-output-')) + await mkdir(path.join(outputDir, 'landscapes'), { recursive: true }) + await copyFile(SAMPLE_IMAGE, path.join(outputDir, 'landscapes', 'ComfyUI_00001_.jpg')) + await copyFile(SAMPLE_VIDEO, path.join(outputDir, 'ComfyUI_00002_.webm')) + ctx = await launchApp( + { settings: { firstUseCompleted: true, telemetryEnabled: false, outputDir } }, + { stayOnHome: true } + ) +}) + +test.afterAll(async () => { + await ctx.cleanup() + await rm(outputDir, { recursive: true, force: true }) +}) + +test('Assets shows the images in the output folder with no build running and no login @windows @macos @linux', async () => { + expect(await ctx.panel.click(SIDEBAR_ASSETS)).toBe(true) + + await ctx.panel.waitForVisible(ASSETS_GRID) + await expect + .poll( + () => + ctx.panel.evaluate( + `(() => { + const img = document.querySelector('${ASSETS_GRID} img') + return !!img && img.complete && img.naturalWidth > 0 + })()` + ), + { message: 'the first image from disk loads', timeout: 20_000 } + ) + .toBe(true) + const src = await ctx.panel.evaluate( + `document.querySelector('${ASSETS_GRID} img').getAttribute('src')` + ) + expect(src.startsWith('comfy-output://file/')).toBe(true) + expect(decodeURIComponent(src)).toContain('ComfyUI_00001_.jpg') +}) + +test('A video in the output folder keeps its shape and shows how long it runs @windows @macos @linux', async () => { + const lengthLabel = `document.querySelector('${ASSETS_GRID} [data-testid="asset-duration"]')` + await expect + .poll(() => ctx.panel.evaluate(`${lengthLabel}?.textContent?.trim() ?? null`), { + message: 'the video card shows its length, read from the file', + timeout: 20_000 + }) + .toBe('0:08') + const shape = await ctx.panel.evaluate( + `(() => { const box = ${lengthLabel}.parentElement.getBoundingClientRect(); return box.width / box.height })()` + ) + expect(shape, 'the card has the video shape, not a 16:9 crop').toBeCloseTo(SAMPLE_VIDEO_SHAPE, 1) +}) diff --git a/locales/en.json b/locales/en.json index 5026b0d26..58ff37259 100644 --- a/locales/en.json +++ b/locales/en.json @@ -19,6 +19,7 @@ "builds": "Builds", "navigation": "Navigation", "home": "Home", + "assets": "Assets", "collapse": "Collapse sidebar", "expand": "Expand sidebar", "signInTitle": "Log in to Comfy", @@ -30,6 +31,41 @@ "crashedWithSignal": "Crashed ({signal})", "failedToStartReason": "Failed to start: {reason}" }, + "assets": { + "kindLabel": "Asset type", + "kind": { + "all": "All", + "image": "Images", + "video": "Videos", + "3d": "3D", + "audio": "Audio" + }, + "refresh": "Refresh assets", + "search": "Search by name...", + "viewOptions": "View options", + "layoutLabel": "Layout", + "layout": { + "masonry": "Masonry", + "grid": "Grid" + }, + "sortLabel": "Sort by", + "sort": { + "newest": "Newest first", + "oldest": "Oldest first" + }, + "showLabel": "Show", + "showName": "File name", + "showDetails": "File details", + "emptyTitle": "No assets yet", + "emptyBody": "Open an app to make assets.", + "loadFailed": "Couldn't load assets", + "noMatches": "No matching assets", + "noMatchesHint": "Try another type or search.", + "filter": "Filter", + "signedOutTitle": "Log in to see your assets", + "signedOutBody": "Assets that you make in Comfy Cloud show here.", + "signIn": "Log in to Comfy Cloud" + }, "home": { "title": "Welcome. Let's create!", "openComfyUI": "Open ComfyUI", diff --git a/locales/zh.json b/locales/zh.json index daa831b65..3c57ab1ac 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -19,6 +19,7 @@ "builds": "构建", "navigation": "导航", "home": "主页", + "assets": "资产", "collapse": "收起侧边栏", "expand": "展开侧边栏", "signInTitle": "登录 Comfy", @@ -30,6 +31,41 @@ "crashedWithSignal": "已崩溃({signal})", "failedToStartReason": "启动失败:{reason}" }, + "assets": { + "kindLabel": "资产类型", + "kind": { + "all": "全部", + "image": "图片", + "video": "视频", + "3d": "3D", + "audio": "音频" + }, + "refresh": "刷新资产", + "search": "按名称搜索...", + "viewOptions": "视图选项", + "layoutLabel": "布局", + "layout": { + "masonry": "瀑布流", + "grid": "网格" + }, + "sortLabel": "排序方式", + "sort": { + "newest": "最新优先", + "oldest": "最早优先" + }, + "showLabel": "显示", + "showName": "文件名", + "showDetails": "文件详情", + "emptyTitle": "暂无资产", + "emptyBody": "打开一个应用来生成资产。", + "loadFailed": "无法加载资产", + "noMatches": "没有匹配的资产", + "noMatchesHint": "试试其他类型或搜索词。", + "filter": "筛选", + "signedOutTitle": "登录以查看你的资产", + "signedOutBody": "你在 Comfy Cloud 中生成的资产会显示在这里。", + "signIn": "登录 Comfy Cloud" + }, "home": { "title": "欢迎。开始创作吧!", "openComfyUI": "打开 ComfyUI", diff --git a/package.json b/package.json index e49a7f151..0f6098187 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,8 @@ "@xterm/addon-fit": "^0.10.0", "@xterm/xterm": "^5.5.0", "electron-updater": "^6.7.3", + "image-size": "^2.0.4", + "music-metadata": "^12.3.2", "node-pty": "^1.1.0", "posthog-node": "^5.30.7", "semver": "^7.7.4", @@ -94,6 +96,7 @@ "@pinia/testing": "^1.0.3", "@playwright/test": "^1.58.2", "@tailwindcss/vite": "^4.1.8", + "@tanstack/vue-virtual": "^3.13.48", "@types/node": "^22.19.1", "@types/semver": "^7.7.1", "@vitejs/plugin-vue": "^6.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9556b774b..1d32ce583 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,6 +29,12 @@ importers: electron-updater: specifier: ^6.7.3 version: 6.8.3 + image-size: + specifier: ^2.0.4 + version: 2.0.4 + music-metadata: + specifier: ^12.3.2 + version: 12.3.2 node-pty: specifier: ^1.1.0 version: 1.1.0 @@ -72,6 +78,9 @@ importers: '@tailwindcss/vite': specifier: ^4.1.8 version: 4.2.1(vite@7.3.1(@types/node@22.19.12)(jiti@2.6.1)(lightningcss@1.31.1)(yaml@2.9.0)) + '@tanstack/vue-virtual': + specifier: ^3.13.48 + version: 3.13.48(vue@3.5.44(typescript@5.9.3)) '@types/node': specifier: ^22.19.1 version: 22.19.12 @@ -274,6 +283,9 @@ packages: '@blazediff/core@1.9.1': resolution: {integrity: sha512-ehg3jIkYKulZh+8om/O25vkvSsXXwC+skXmyA87FFx6A/45eqOkZsBltMw/TVteb0mloiGT8oGRTcjRAz66zaA==} + '@borewit/text-codec@0.2.3': + resolution: {integrity: sha512-49CycTz1aAZmvFcwkeFyhud1HUmRIB5Tabf7ALV+VA4Iu85MTfz2cUwzPagMFAdVBvrWdkCxG67I30tUwwpKsw==} + '@comfyorg/design-system@1.3.0-alpha.1': resolution: {integrity: sha512-PJ2NBBOf8YDkeO6YeY4ABGHToWbYXnBY3R2+tI3bREiABUHdR20Gr4Fzr43LOiLubzsi76cSN6Z4+Ym6q5TeLw==} peerDependencies: @@ -1229,17 +1241,24 @@ packages: peerDependencies: vite: ^5.2.0 || ^6 || ^7 - '@tanstack/virtual-core@3.19.8': - resolution: {integrity: sha512-Oz/SYBo/aVPyQkwtOw7Q23myITUgFIof2glQqydYJAPL6mH2SkJDkvDKZTklB+7gv5jGLZQhyTcd7SO1A1EODQ==} + '@tanstack/virtual-core@3.19.7': + resolution: {integrity: sha512-gxy5SvH0mFRJ0YujtrflB/1HAu9Wn1KGvL0bjZfQkYVVMsIpQiwcf1mR06/ViN4NjllUb4HNCjqE9FGwgTvMCQ==} - '@tanstack/vue-virtual@3.13.49': - resolution: {integrity: sha512-+hh0M+apciv9RYYVGnaEmcTncH6dpo3e2MTexNlOaTo7WXdLvQ6JSvtmiZCMbcBacZxibXGvVzp7sa1WsmZRHA==} + '@tanstack/vue-virtual@3.13.48': + resolution: {integrity: sha512-exH0/lELoCW9D6x8V1Uq61XY4zrN1AeDH4rDKUMTOJ93ndkeEVIY8zo366IBM13HSjhULpuWPdPhJ+ccidg58A==} peerDependencies: vue: ^2.7.0 || ^3.0.0 '@todesktop/runtime@2.1.3': resolution: {integrity: sha512-dtC/BwpW5k0kJN/aSxHRznkjwCNcvPRUQ1AeZ/OeBszvdYrAmdGtw8mb4Ztk/6Z/2jzlCJ6mPtf4jtujXMpIHg==} + '@tokenizer/inflate@0.6.0': + resolution: {integrity: sha512-+0RDxhBkyT9xMnGdJAwmats9yxRLR8ypJrQgrnLqOu4Gv/aHlnxYgdGVy2QTwvBl6IF/w2Hr+ZNirH+iMUbVZA==} + engines: {node: '>=18'} + + '@tokenizer/token@0.3.0': + resolution: {integrity: sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==} + '@tootallnate/once@2.0.1': resolution: {integrity: sha512-HqmEUIGRJ5fSXchkVgR5F7qn48bDBzv0kWj/Kfu5e6uci4UlEeng4331LnBkWffb++Ei3FOVLxo8JJWMFBDMeQ==} engines: {node: '>= 10'} @@ -1898,6 +1917,10 @@ packages: config-chain@1.1.13: resolution: {integrity: sha512-qj+f8APARXHrM0hraqXYb2/bOVSV4PvJQlNZ/DVj0QrmNM2q2euizkeuVckQ57J+W0mRH6Hvi+k50M4Jul2VRQ==} + content-type@3.1.1: + resolution: {integrity: sha512-GW4qUsfFo59d0HbUibDlWv5wPz+vAAcaTWbKIuKCf0JkC7wWkSyf8f13IpXn5JkeMlB2P8iTSSIjwXljorg2vA==} + engines: {node: '>=22'} + convert-source-map@2.0.0: resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==} @@ -2364,6 +2387,10 @@ packages: resolution: {integrity: sha512-XXTUwCvisa5oacNGRP9SfNtYBNAMi+RPwBFmblZEF7N7swHYQS6/Zfk7SRwx4D5j3CH211YNRco1DEMNVfZCnQ==} engines: {node: '>=16.0.0'} + file-type@22.3.0: + resolution: {integrity: sha512-PrPedgnnWl5bWz3JHgar0QSzRvzeyFh7dZ9JpYOgcPZu4KALS9zw4MMa6qXRYyjeBsAlpaWB2boLuG991+0HIQ==} + engines: {node: '>=22'} + filelist@1.0.6: resolution: {integrity: sha512-5giy2PkLYY1cP39p17Ech+2xlpTRL9HLspOfEgm0L6CwBXBTgsK5ou0JtzYuepxkaQ/tvhCFIJ5uXo0OrM2DxA==} @@ -2622,6 +2649,11 @@ packages: resolution: {integrity: sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg==} engines: {node: '>= 4'} + image-size@2.0.4: + resolution: {integrity: sha512-QRUkFFsRV/6fuESxb9Vkq+a0LkSrgKXuc2NEqfikiXxxN/G3tjWt5EVUlMaImRBZRZK/jRBEbYvpPYZL8t08Zw==} + engines: {node: '>=18'} + hasBin: true + immediate@3.0.6: resolution: {integrity: sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==} @@ -2951,6 +2983,10 @@ packages: mdn-data@2.27.1: resolution: {integrity: sha512-9Yubnt3e8A0OKwxYSXyhLymGW4sCufcLG6VdiDdUGVkPhpqLxlvP5vl1983gQjJl3tqbrM731mjaZaP68AgosQ==} + media-typer@2.0.0: + resolution: {integrity: sha512-kOy3OxT2HH39N70UnKgu4NWDZjLOz8W/mfyvniHjRH/DrL3f2pOfvWQ4p60offbbtDAnXWp0v9LfMIqMec269Q==} + engines: {node: '>=18'} + merge-stream@2.0.0: resolution: {integrity: sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==} @@ -3090,6 +3126,10 @@ packages: muggle-string@0.4.1: resolution: {integrity: sha512-VNTrAak/KhO2i8dqqnqnAHOa3cYBwXEZe9h+D5h/1ZqFSTEFHdM65lR7RoIqq3tBBYavsOXV84NoHXZ0AkPyqQ==} + music-metadata@12.3.2: + resolution: {integrity: sha512-U+Wu8c/7xa8wP331oVuRTAJSHcEWQIkLjpuyRJG+CAIszGYhpr5CRDv6PqkkVHDoX3ndjmdJlB6S49AaLq8SbQ==} + engines: {node: '>=22'} + mute-stream@0.0.8: resolution: {integrity: sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==} @@ -3672,6 +3712,10 @@ packages: strnum@2.2.0: resolution: {integrity: sha512-Y7Bj8XyJxnPAORMZj/xltsfo55uOiyHcU2tnAVzHUnSJR/KsEX+9RoDeXEnsXtl/CX4fAcrt64gZ13aGaWPeBg==} + strtok3@10.8.0: + resolution: {integrity: sha512-e6bW3fD6V01DPmcbLfNtn0cjsiHvTKS/hivgbzqYMfHb/nLes4QQ0H5QBxlkPQayF4xv08AvlSgptGwN86Eemw==} + engines: {node: '>=18'} + sumchecker@3.0.1: resolution: {integrity: sha512-MvjXzkz/BOfyVDkG0oFOtBxHX2u3gKbMHIF/dXblZsgD3BWOFLmHovIpZY7BykJdAjcqRCBi1WYBNdEC9yI7vg==} engines: {node: '>= 8.0'} @@ -3766,6 +3810,10 @@ packages: resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==} engines: {node: '>=8.0'} + token-types@6.2.0: + resolution: {integrity: sha512-s9euYHM//T3loJtbNUYdbAzsMlvKBeRS+XCLFy+UGosMQH19Hwjap5k19i4PPAEJJBsVIpMpVTxl4fIqCuNfUg==} + engines: {node: '>=14.16'} + totalist@3.0.1: resolution: {integrity: sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ==} engines: {node: '>=6'} @@ -3812,6 +3860,10 @@ packages: engines: {node: '>=14.17'} hasBin: true + uint8array-extras@1.6.0: + resolution: {integrity: sha512-8iAasVS4wUx0gPLjH8Xtz2PeDzTSiy8QiLGu2DT3X5GU+egFEQhpnbtkehbAwjvDcxIERmCYcysiODFmE3Ud0Q==} + engines: {node: '>=18'} + undici-types@6.21.0: resolution: {integrity: sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==} @@ -4003,6 +4055,9 @@ packages: engines: {node: '>=8'} hasBin: true + win-guid@0.2.1: + resolution: {integrity: sha512-gEIQU4mkgl2OPeoNrWflcJFJ3Ae2BPd4eCsHHA/XikslkIVms/nHhvnvzIZV7VLmBvtFlDOzLt9rrZT+n6D67A==} + word-wrap@1.2.5: resolution: {integrity: sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==} engines: {node: '>=0.10.0'} @@ -4213,6 +4268,8 @@ snapshots: '@blazediff/core@1.9.1': {} + '@borewit/text-codec@0.2.3': {} + '@comfyorg/design-system@1.3.0-alpha.1(@vueuse/core@14.4.0(vue@3.5.44(typescript@5.9.3)))(@vueuse/integrations@14.4.0(axios@1.13.6)(fuse.js@7.5.0)(vue@3.5.44(typescript@5.9.3)))(reka-ui@2.11.0(vue@3.5.44(typescript@5.9.3)))(tailwindcss@4.2.1)(typescript@5.9.3)(vue-component-type-helpers@3.3.13)(vue@3.5.44(typescript@5.9.3))': dependencies: '@iconify-json/lucide': 1.2.148 @@ -5097,11 +5154,11 @@ snapshots: tailwindcss: 4.2.1 vite: 7.3.1(@types/node@22.19.12)(jiti@2.6.1)(lightningcss@1.31.1)(yaml@2.9.0) - '@tanstack/virtual-core@3.19.8': {} + '@tanstack/virtual-core@3.19.7': {} - '@tanstack/vue-virtual@3.13.49(vue@3.5.44(typescript@5.9.3))': + '@tanstack/vue-virtual@3.13.48(vue@3.5.44(typescript@5.9.3))': dependencies: - '@tanstack/virtual-core': 3.19.8 + '@tanstack/virtual-core': 3.19.7 vue: 3.5.44(typescript@5.9.3) '@todesktop/runtime@2.1.3': @@ -5115,6 +5172,16 @@ snapshots: transitivePeerDependencies: - supports-color + '@tokenizer/inflate@0.6.0': + dependencies: + debug: 4.4.3 + strtok3: 10.8.0 + token-types: 6.2.0 + transitivePeerDependencies: + - supports-color + + '@tokenizer/token@0.3.0': {} + '@tootallnate/once@2.0.1': {} '@tootallnate/quickjs-emscripten@0.23.0': {} @@ -5895,6 +5962,8 @@ snapshots: ini: 1.3.8 proto-list: 1.2.4 + content-type@3.1.1: {} + convert-source-map@2.0.0: {} copy-anything@4.0.5: @@ -6492,6 +6561,15 @@ snapshots: dependencies: flat-cache: 4.0.1 + file-type@22.3.0: + dependencies: + '@tokenizer/inflate': 0.6.0 + strtok3: 10.8.0 + token-types: 6.2.0 + uint8array-extras: 1.6.0 + transitivePeerDependencies: + - supports-color + filelist@1.0.6: dependencies: minimatch: 5.1.9 @@ -6802,6 +6880,8 @@ snapshots: ignore@7.0.5: {} + image-size@2.0.4: {} + immediate@3.0.6: {} import-meta-resolve@4.2.0: {} @@ -7115,6 +7195,8 @@ snapshots: mdn-data@2.27.1: {} + media-typer@2.0.0: {} + merge-stream@2.0.0: {} merge2@1.4.1: {} @@ -7235,6 +7317,21 @@ snapshots: muggle-string@0.4.1: {} + music-metadata@12.3.2: + dependencies: + '@borewit/text-codec': 0.2.3 + '@tokenizer/token': 0.3.0 + content-type: 3.1.1 + debug: 4.4.3 + file-type: 22.3.0 + media-typer: 2.0.0 + strtok3: 10.8.0 + token-types: 6.2.0 + uint8array-extras: 1.6.0 + win-guid: 0.2.1 + transitivePeerDependencies: + - supports-color + mute-stream@0.0.8: {} nanoid@3.3.11: {} @@ -7569,7 +7666,7 @@ snapshots: '@floating-ui/vue': 2.0.1(vue@3.5.44(typescript@5.9.3)) '@internationalized/date': 3.12.4 '@internationalized/number': 3.6.9 - '@tanstack/vue-virtual': 3.13.49(vue@3.5.44(typescript@5.9.3)) + '@tanstack/vue-virtual': 3.13.48(vue@3.5.44(typescript@5.9.3)) '@vueuse/core': 14.4.0(vue@3.5.44(typescript@5.9.3)) '@vueuse/shared': 14.4.0(vue@3.5.44(typescript@5.9.3)) aria-hidden: 1.2.6 @@ -7824,6 +7921,10 @@ snapshots: strnum@2.2.0: {} + strtok3@10.8.0: + dependencies: + '@tokenizer/token': 0.3.0 + sumchecker@3.0.1: dependencies: debug: 4.4.3 @@ -7924,6 +8025,12 @@ snapshots: dependencies: is-number: 7.0.0 + token-types@6.2.0: + dependencies: + '@borewit/text-codec': 0.2.3 + '@tokenizer/token': 0.3.0 + ieee754: 1.2.1 + totalist@3.0.1: {} truncate-utf8-bytes@1.0.2: @@ -7962,6 +8069,8 @@ snapshots: typescript@5.9.3: {} + uint8array-extras@1.6.0: {} + undici-types@6.21.0: {} undici-types@7.16.0: {} @@ -8113,6 +8222,8 @@ snapshots: siginfo: 2.0.0 stackback: 0.0.2 + win-guid@0.2.1: {} + word-wrap@1.2.5: {} wrap-ansi@6.2.0: diff --git a/src/main/host/panelView.test.ts b/src/main/host/panelView.test.ts index 89adc9148..c57981986 100644 --- a/src/main/host/panelView.test.ts +++ b/src/main/host/panelView.test.ts @@ -214,6 +214,7 @@ describe('comfy-window:navigate-panel', () => { it.each([ ['chooser', 'comfy', 'benchmarks'], + ['assets', 'comfy', 'benchmarks'], ['benchmarks', 'benchmarks', 'comfy'], ['performance-test', 'performance-test', 'comfy'] ] as const)('opens %s in a dashboard window', (page, panel, startingPanel) => { diff --git a/src/main/host/panelView.ts b/src/main/host/panelView.ts index 13d193696..0d263411a 100644 --- a/src/main/host/panelView.ts +++ b/src/main/host/panelView.ts @@ -22,6 +22,7 @@ import type { BodyMode, ComfyPanelKey, ComfyWindowEntry } from './registry' const DASHBOARD_PAGE_PANELS: ReadonlyMap = new Map([ ['home', 'comfy'], + ['assets', 'comfy'], ['chooser', 'comfy'], ['benchmarks', 'benchmarks'], ['performance-test', 'performance-test'] diff --git a/src/main/index.ts b/src/main/index.ts index 25ff80dab..2396fdc83 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -30,6 +30,7 @@ import { migrateXdgPaths, persistWinDataRootChoice } from './lib/paths' import { saveWindowBounds } from './lib/windowState' import { flushLastSessionSync, recordDashboardSurface } from './lib/lastSession' import { registerProcessErrorHandlers } from './lib/processErrorHandlers' +import { registerOutputFilesScheme, registerOutputFolderHandlers } from './lib/outputFolders' import { initAppLog, flushOperationOutput } from './lib/appLog' import { pruneCrashDumps } from './lib/crashDumps' import { createStartupReentryGate } from './lib/startupReentryGate' @@ -194,6 +195,8 @@ if (settings.get('hardwareAcceleration') === false) { app.disableHardwareAcceleration() } +registerOutputFilesScheme() + todesktop.init({ autoUpdater: false }) const APP_VERSION = getAppVersion() @@ -935,6 +938,7 @@ if (app.isPackaged && !app.requestSingleInstanceLock()) { }) setHostFactories({ createChooser: openChooserHostWindow }) registerPanelViewIpc() + registerOutputFolderHandlers() migrateXdgPaths() persistWinDataRootChoice() diff --git a/src/main/lib/assets.test.ts b/src/main/lib/assets.test.ts new file mode 100644 index 000000000..9f241534b --- /dev/null +++ b/src/main/lib/assets.test.ts @@ -0,0 +1,213 @@ +import fs from 'node:fs' +import path from 'node:path' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const getInstallation = vi.fn() +const getAccessToken = vi.fn() + +vi.mock('electron', () => ({ + ipcMain: { handle: vi.fn() } +})) +vi.mock('../installations', () => ({ get: (id: string) => getInstallation(id) })) +vi.mock('../devplatform/session', () => ({ + getCloudSession: () => ({ getAccessToken }) +})) +vi.mock('./ipc/shared', () => ({ + sourceMap: { + cloud: { category: 'cloud' }, + standalone: { category: 'local' }, + remote: { category: 'remote' } + } +})) + +const { assetKind, listAssets, signedStorageUrl } = await import('./assets') + +const fetchMock = vi.fn() +const SAMPLE_VIDEO = path.resolve('src/renderer/public/install-showcase-scene/Assets/earpods.webm') + +function pngHeader(width: number, height: number): Uint8Array { + const header = Buffer.alloc(33) + Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(header, 0) + header.writeUInt32BE(13, 8) + header.write('IHDR', 12, 'ascii') + header.writeUInt32BE(width, 16) + header.writeUInt32BE(height, 20) + header.writeUInt8(8, 24) + header.writeUInt8(2, 25) + return new Uint8Array(header) +} + +function jsonResponse(body: unknown): Response { + return new Response(JSON.stringify(body), { + status: 200, + headers: { 'Content-Type': 'application/json' } + }) +} + +beforeEach(() => { + vi.stubGlobal('fetch', fetchMock) +}) + +afterEach(() => { + vi.unstubAllGlobals() + vi.clearAllMocks() +}) + +describe('assetKind', () => { + it.each([ + ['clip.bin', 'video/mp4', 'video'], + ['still.png', undefined, 'image'], + ['narration.WAV', undefined, 'audio'], + ['mesh.glb', undefined, '3d'], + ['notes.txt', undefined, null], + ['shader.constructor', undefined, null], + ['png', undefined, null] + ])('classifies %s (%s) as %s', (name, mimeType, kind) => { + expect(assetKind(name, mimeType)).toBe(kind) + }) +}) + +describe('signedStorageUrl', () => { + it.each([ + ['https://storage.googleapis.com/bucket/a.png?sig=1', true], + ['https://evil.example.com/a.png', false], + ['http://storage.googleapis.com/a.png', false], + [null, false] + ])('accepts only https Google storage links: %s', (location, accepted) => { + expect(signedStorageUrl(location) !== null).toBe(accepted) + }) +}) + +describe('listAssets', () => { + it('never asks a local or remote build over HTTP', async () => { + getInstallation.mockResolvedValue({ id: 'local-1', sourceId: 'standalone' }) + expect(await listAssets('local-1')).toEqual([]) + getInstallation.mockResolvedValue({ id: 'remote-1', sourceId: 'remote' }) + expect(await listAssets('remote-1')).toEqual([]) + expect(fetchMock).not.toHaveBeenCalled() + }) + + it('returns nothing for Cloud when signed out', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue(null) + + expect(await listAssets('cloud-1')).toEqual([]) + expect(fetchMock).not.toHaveBeenCalled() + }) + + it('drops Cloud rows whose fields have the wrong type and keeps an empty size', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue('token') + fetchMock + .mockResolvedValueOnce( + jsonResponse({ + assets: [ + { id: 'bad', name: 'bad.png', mime_type: 1 }, + { id: 'good', name: 'good.png', mime_type: null, created_at: null, size: 0 } + ] + }) + ) + .mockResolvedValue(new Response(null, { status: 404 })) + + const assets = await listAssets('cloud-1') + + expect(assets.map((a) => [a.id, a.sizeBytes])).toEqual([['good', 0]]) + }) + + it('keeps one page of Cloud rows when the server sends more than it was asked for', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue('token') + fetchMock + .mockResolvedValueOnce( + jsonResponse({ + assets: Array.from({ length: 150 }, (_, i) => ({ id: `a${i}`, name: `a${i}.bin` })) + }) + ) + .mockResolvedValue(new Response(null, { status: 404 })) + + const assets = await listAssets('cloud-1') + + expect(assets).toHaveLength(100) + expect(assets.at(-1)!.id).toBe('a99') + }) + + it('lists the signed-in cloud workspace and resolves signed media links for playable kinds', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue('token') + fetchMock + .mockResolvedValueOnce( + jsonResponse({ + assets: [ + { id: 'v1', name: 'clip.mp4', created_at: '2026-11-09T10:00:00Z' }, + { id: 's1', name: 'audio/voice.wav', created_at: '2026-11-09T09:00:00Z' }, + { id: 'm1', name: 'mesh.glb', created_at: '2026-11-09T08:00:00Z' } + ] + }) + ) + .mockImplementation( + async (url: string) => + new Response(null, { + status: 302, + headers: { + Location: `https://storage.googleapis.com/bucket/${/\/assets\/([^/]+)\/content/.exec(url)?.[1]}?sig=1` + } + }) + ) + + const assets = await listAssets('cloud-1') + + expect(fetchMock.mock.calls[0]![1].headers.Authorization).toBe('Bearer token') + expect(assets.map((a) => [a.name, a.kind, a.previewUrl])).toEqual([ + ['clip.mp4', 'video', 'https://storage.googleapis.com/bucket/v1?sig=1'], + ['voice.wav', 'audio', 'https://storage.googleapis.com/bucket/s1?sig=1'], + ['mesh.glb', '3d', null] + ]) + expect(fetchMock).toHaveBeenCalledTimes(4) + }) + + it('reads the size of each Cloud picture and video, and how long a video runs, from the start of the file', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue('token') + const files: Record> = { + p1: pngHeader(768, 1344), + v1: new Uint8Array(fs.readFileSync(SAMPLE_VIDEO)) + } + const rangesAsked: string[] = [] + fetchMock.mockImplementation(async (url: string, init?: RequestInit) => { + if (url.includes('/assets?')) + return jsonResponse({ + assets: [ + { id: 'p1', name: 'portrait.png', created_at: '2026-11-09T10:00:00Z' }, + { id: 'v1', name: 'clip.webm', created_at: '2026-11-09T09:00:00Z' } + ] + }) + const id = /\/assets\/([^/]+)\/content/.exec(url)?.[1] + if (id) + return new Response(null, { + status: 302, + headers: { Location: `https://storage.googleapis.com/bucket/${id}?sig=1` } + }) + rangesAsked.push(new Headers(init?.headers).get('Range') ?? '') + const file = files[/bucket\/([^?]+)/.exec(url)![1]!]! + return new Response(file, { status: 206 }) + }) + + const assets = await listAssets('cloud-1') + + expect( + assets.map((a) => [ + a.name, + a.width, + a.height, + a.durationSeconds && Math.round(a.durationSeconds) + ]) + ).toEqual([ + ['portrait.png', 768, 1344, null], + ['clip.webm', 714, 530, 8] + ]) + expect(rangesAsked.toSorted(), 'only the start of each file is asked for').toEqual([ + 'bytes=0-524287', + 'bytes=0-65535' + ]) + }) +}) diff --git a/src/main/lib/assets.ts b/src/main/lib/assets.ts new file mode 100644 index 000000000..4c8cf16a6 --- /dev/null +++ b/src/main/lib/assets.ts @@ -0,0 +1,209 @@ +import { ipcMain } from 'electron' +import { CLOUD_CONFIG } from '../cloud/config' +import { getCloudSession } from '../devplatform/session' +import * as installations from '../installations' +import { sourceMap } from './ipc/shared' +import { runPool } from '../sources/standalone/templateDownloadCore' +import { mediaFactsFromBytes } from './mediaFacts' +import type { AssetKind, AssetSummary } from '../../types/ipc' + +const ASSETS_LIST_CHANNEL = 'assets:list' + +const PAGE_SIZE = 100 +const LIST_QUERY = `tags_any=output&sort=created_at&order=desc&limit=${PAGE_SIZE}` +const REQUEST_TIMEOUT_MS = 20_000 +const PREVIEW_BATCH_TIMEOUT_MS = 30_000 +const SIGNED_URL_CONCURRENCY = 6 +const SIGNED_STORAGE_HOST = 'storage.googleapis.com' +const HEADER_BYTES: Partial> = { + image: 64 * 1024, + video: 512 * 1024 +} + +const KIND_BY_EXTENSION = new Map([ + ...['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'avif', 'tif', 'tiff', 'svg'].map( + (ext) => [ext, 'image'] as const + ), + ...['mp4', 'm4v', 'webm', 'mov', 'avi', 'mkv'].map((ext) => [ext, 'video'] as const), + ...['mp3', 'wav', 'ogg', 'flac', 'opus', 'm4a'].map((ext) => [ext, 'audio'] as const), + ...['obj', 'fbx', 'gltf', 'glb', 'stl', 'usdz', 'ply', 'spz', 'splat', 'ksplat'].map( + (ext) => [ext, '3d'] as const + ) +]) + +interface AssetRow { + id: string + name: string + size?: number | null + mime_type?: string | null + created_at?: string | null + preview_url?: string | null + tags?: unknown +} + +type ContentDisposition = 'inline' | 'attachment' + +function isOptional(value: unknown, type: 'string' | 'number'): boolean { + return value === undefined || value === null || typeof value === type +} + +function isAssetRow(value: unknown): value is AssetRow { + if (!value || typeof value !== 'object') return false + const row = value as Record + return ( + typeof row.id === 'string' && + typeof row.name === 'string' && + isOptional(row.size, 'number') && + isOptional(row.mime_type, 'string') && + isOptional(row.created_at, 'string') && + isOptional(row.preview_url, 'string') + ) +} + +export function assetKind(name: string, mimeType?: string | null): AssetKind | null { + const mimeKind = mimeType?.split('/')[0] + if (mimeKind === 'image' || mimeKind === 'video' || mimeKind === 'audio') return mimeKind + if (mimeType?.startsWith('model/')) return '3d' + const dot = name.lastIndexOf('.') + if (dot < 0) return null + return KIND_BY_EXTENSION.get(name.slice(dot + 1).toLowerCase()) ?? null +} + +function toSummary(row: AssetRow, previewUrl: string | null): AssetSummary { + return { + id: row.id, + name: row.name.slice(row.name.lastIndexOf('/') + 1), + kind: assetKind(row.name, row.mime_type), + sizeBytes: typeof row.size === 'number' ? row.size : null, + createdAt: row.created_at ?? null, + mimeType: row.mime_type ?? null, + tags: Array.isArray(row.tags) + ? row.tags.filter((tag): tag is string => typeof tag === 'string') + : [], + previewUrl, + thumbnailUrl: null, + width: null, + height: null, + durationSeconds: null + } +} + +async function fetchAssetRows(url: string, headers: Record): Promise { + const res = await fetch(url, { + headers: { Accept: 'application/json', ...headers }, + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) + }) + if (!res.ok) throw new Error(`List assets failed: HTTP ${res.status}`) + const body: unknown = await res.json().catch(() => null) + const rows = body && typeof body === 'object' ? (body as { assets?: unknown }).assets : undefined + if (!Array.isArray(rows)) throw new Error('List assets returned an invalid page') + return rows.filter(isAssetRow).slice(0, PAGE_SIZE) +} + +export function signedStorageUrl(location: string | null): string | null { + if (!location) return null + try { + const url = new URL(location) + return url.protocol === 'https:' && url.hostname.toLowerCase() === SIGNED_STORAGE_HOST + ? url.toString() + : null + } catch { + return null + } +} + +function contentPath(assetId: string, disposition: ContentDisposition): string { + return `/api/assets/${encodeURIComponent(assetId)}/content?disposition=${disposition}` +} + +async function resolveCloudContentUrl( + assetId: string, + accessToken: string, + disposition: ContentDisposition, + deadline: AbortSignal +): Promise { + try { + const res = await fetch(`${CLOUD_CONFIG.issuer}${contentPath(assetId, disposition)}`, { + headers: { Authorization: `Bearer ${accessToken}` }, + redirect: 'manual', + signal: AbortSignal.any([AbortSignal.timeout(REQUEST_TIMEOUT_MS), deadline]) + }) + return signedStorageUrl(res.headers.get('location')) + } catch { + return null + } +} + +async function firstBytes(response: Response, limit: number): Promise { + const reader = response.body?.getReader() + if (!reader) return new Uint8Array() + const chunks: Uint8Array[] = [] + let length = 0 + while (length < limit) { + const { done, value } = await reader.read() + if (done) break + chunks.push(value) + length += value.length + } + await reader.cancel().catch(() => {}) + const bytes = new Uint8Array(length) + let offset = 0 + for (const chunk of chunks) { + bytes.set(chunk, offset) + offset += chunk.length + } + return bytes.subarray(0, limit) +} + +async function addMediaFacts(summary: AssetSummary, deadline: AbortSignal): Promise { + const limit = summary.kind ? HEADER_BYTES[summary.kind] : undefined + if (!summary.previewUrl || !limit) return + try { + const res = await fetch(summary.previewUrl, { + headers: { Range: `bytes=0-${limit - 1}` }, + signal: AbortSignal.any([AbortSignal.timeout(REQUEST_TIMEOUT_MS), deadline]) + }) + if (!res.ok) return + const bytes = await firstBytes(res, limit) + Object.assign(summary, await mediaFactsFromBytes(bytes, summary.kind, summary.mimeType)) + } catch { + return + } +} + +async function cloudAccessToken(installationId: string): Promise { + const inst = await installations.get(installationId) + if (!inst || sourceMap[inst.sourceId]?.category !== 'cloud') return null + return getCloudSession().getAccessToken() +} + +async function listCloudAssets(accessToken: string): Promise { + const rows = await fetchAssetRows( + `${CLOUD_CONFIG.apiBase}/assets?${LIST_QUERY}&include_public=false`, + { Authorization: `Bearer ${accessToken}` } + ) + const summaries = rows.map((row) => toSummary(row, null)) + const deadline = AbortSignal.timeout(PREVIEW_BATCH_TIMEOUT_MS) + await runPool( + summaries, + SIGNED_URL_CONCURRENCY, + async (summary) => { + if (summary.kind === null || summary.kind === '3d') return + summary.previewUrl = await resolveCloudContentUrl(summary.id, accessToken, 'inline', deadline) + await addMediaFacts(summary, deadline) + }, + deadline + ) + return summaries +} + +export async function listAssets(installationId: string): Promise { + const accessToken = await cloudAccessToken(installationId) + return accessToken ? listCloudAssets(accessToken) : [] +} + +export function registerAssetsHandlers(): void { + ipcMain.handle(ASSETS_LIST_CHANNEL, (_event, installationId: unknown) => + typeof installationId === 'string' ? listAssets(installationId) : [] + ) +} diff --git a/src/main/lib/ipc/index.ts b/src/main/lib/ipc/index.ts index 092fc132d..cacb24984 100644 --- a/src/main/lib/ipc/index.ts +++ b/src/main/lib/ipc/index.ts @@ -35,6 +35,7 @@ import { registerLogsHandlers } from './registerLogsHandlers' import { registerCrashHandlers } from './registerCrashHandlers' import { registerTelemetryHandlers } from './registerTelemetryHandlers' import { registerDevPlatformHandlers } from './registerDevPlatformHandlers' +import { registerAssetsHandlers } from '../assets' import { registerEmbeddedSessionHandlers } from './registerEmbeddedSessionHandlers' import { registerCatalogHandlers } from './registerCatalogHandlers' import { reconcileAdoptedSettings } from '../desktopAdopt' @@ -279,6 +280,7 @@ export function register(callbacks: RegisterCallbacks = {}): Promise { registerCrashHandlers() registerTelemetryHandlers() registerDevPlatformHandlers() + registerAssetsHandlers() registerEmbeddedSessionHandlers() registerCatalogHandlers() return startupRecovery.then(() => undefined) diff --git a/src/main/lib/ipc/sessionActions/launch.ts b/src/main/lib/ipc/sessionActions/launch.ts index ec4028df4..f400ad996 100644 --- a/src/main/lib/ipc/sessionActions/launch.ts +++ b/src/main/lib/ipc/sessionActions/launch.ts @@ -167,6 +167,7 @@ import { writeCampaignRecord } from '../../coreBetaCampaignFlags' import { getIdClass } from '../../deviceId' +import { outputDirOverride } from '../../outputFolders' import type { ComfyArgsSchema } from '../../comfy-args' export function desktopFeatureFlags( @@ -512,7 +513,6 @@ export function applyStorageLaunchArgs( } if (argsAvailable) { const useSharedInput = (inst.useSharedInput as boolean | undefined) !== false - const useSharedOutput = (inst.useSharedOutput as boolean | undefined) !== false if (useSharedInput) { const inputDir = storage.inputDir || settings.defaults.inputDir fs.mkdirSync(inputDir, { recursive: true }) @@ -524,16 +524,10 @@ export function applyStorageLaunchArgs( launchCmd.args!.push('--input-directory', perInstallInput) } } - if (useSharedOutput) { - const outputDir = storage.outputDir || settings.defaults.outputDir + const outputDir = outputDirOverride(inst, storage.outputDir || settings.defaults.outputDir) + if (outputDir) { fs.mkdirSync(outputDir, { recursive: true }) launchCmd.args!.push('--output-directory', outputDir) - } else { - const perInstallOutput = inst.outputDir as string | undefined - if (perInstallOutput) { - fs.mkdirSync(perInstallOutput, { recursive: true }) - launchCmd.args!.push('--output-directory', perInstallOutput) - } } } diff --git a/src/main/lib/mediaFacts.ts b/src/main/lib/mediaFacts.ts new file mode 100644 index 000000000..909569a0d --- /dev/null +++ b/src/main/lib/mediaFacts.ts @@ -0,0 +1,72 @@ +import { imageSize } from 'image-size' +import { imageSizeFromFile } from 'image-size/fromFile' +import type { IAudioMetadata, IOptions } from 'music-metadata' +import type { AssetKind } from '../../types/ipc' + +export interface MediaFacts { + width: number | null + height: number | null + durationSeconds: number | null +} + +interface PictureSize { + width?: number + height?: number + orientation?: number +} + +const NO_FACTS: MediaFacts = { width: null, height: null, durationSeconds: null } +const FIRST_SIDEWAYS_EXIF_ORIENTATION = 5 +const HEADERS_ONLY: IOptions = { duration: false, skipCovers: true, mkvUseIndex: true } + +function pictureFacts(size: PictureSize | null): MediaFacts { + if (!size?.width || !size.height) return NO_FACTS + const sideways = (size.orientation ?? 0) >= FIRST_SIDEWAYS_EXIF_ORIENTATION + return { + width: sideways ? size.height : size.width, + height: sideways ? size.width : size.height, + durationSeconds: null + } +} + +function videoFacts(parsed: IAudioMetadata | null): MediaFacts { + if (!parsed) return NO_FACTS + const video = parsed.format.trackInfo.find( + (track) => track.video?.pixelWidth && track.video.pixelHeight + )?.video + const duration = parsed.format.duration + return { + width: video?.pixelWidth ?? null, + height: video?.pixelHeight ?? null, + durationSeconds: duration && Number.isFinite(duration) ? duration : null + } +} + +function pictureSizeOf(bytes: Uint8Array): PictureSize | null { + try { + return imageSize(bytes) + } catch { + return null + } +} + +export async function mediaFactsFromFile( + file: string, + kind: AssetKind | null +): Promise { + if (kind === 'image') return pictureFacts(await imageSizeFromFile(file).catch(() => null)) + if (kind !== 'video') return NO_FACTS + const { parseFile } = await import('music-metadata') + return videoFacts(await parseFile(file, HEADERS_ONLY).catch(() => null)) +} + +export async function mediaFactsFromBytes( + bytes: Uint8Array, + kind: AssetKind | null, + mimeType: string | null +): Promise { + if (kind === 'image') return pictureFacts(pictureSizeOf(bytes)) + if (kind !== 'video') return NO_FACTS + const { parseBuffer } = await import('music-metadata') + return videoFacts(await parseBuffer(bytes, mimeType ?? undefined, HEADERS_ONLY).catch(() => null)) +} diff --git a/src/main/lib/outputFolders.test.ts b/src/main/lib/outputFolders.test.ts new file mode 100644 index 000000000..7c57fa8d9 --- /dev/null +++ b/src/main/lib/outputFolders.test.ts @@ -0,0 +1,360 @@ +import fs from 'node:fs' +import path from 'node:path' +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' + +const { + root, + sharedDir, + ownDir, + installDir, + outsideDir, + installs, + fetchFile, + thumbnailImage, + fullImage +} = await vi.hoisted(async () => { + const nodeFs = await import('node:fs') + const nodeOs = await import('node:os') + const nodePath = await import('node:path') + const base = nodeFs.realpathSync( + nodeFs.mkdtempSync(nodePath.join(nodeOs.tmpdir(), 'output-folders-')) + ) + return { + root: base, + sharedDir: nodePath.join(base, 'shared-output'), + ownDir: nodePath.join(base, 'own-output'), + installDir: nodePath.join(base, 'install'), + outsideDir: nodePath.join(base, 'outside'), + installs: vi.fn(), + fetchFile: vi.fn(async (url: string) => new Response(`file ${url}`)), + thumbnailImage: vi.fn(), + fullImage: vi.fn() + } +}) + +vi.mock('electron', () => ({ + ipcMain: { handle: vi.fn() }, + protocol: { handle: vi.fn(), registerSchemesAsPrivileged: vi.fn() }, + net: { fetch: (url: string) => fetchFile(url) }, + nativeImage: { + createThumbnailFromPath: (file: string) => thumbnailImage(file), + createFromPath: (file: string) => fullImage(file) + } +})) +vi.mock('../installations', () => ({ list: () => installs() })) +vi.mock('../settings', () => ({ + get: (key: string) => (key === 'outputDir' ? sharedDir : undefined), + defaults: { outputDir: path.join(root, 'default-output') } +})) +vi.mock('./ipc/shared', () => ({ + sourceMap: { + standalone: { category: 'local', getLaunchCommand: () => ({ cmd: 'python' }) }, + desktop: { + category: 'local', + getLaunchCommand: () => ({ cmd: 'open', skipSharedPaths: true }) + }, + cloud: { category: 'cloud' } + } +})) +vi.mock('./models', () => ({ + installOutputDir: (installPath: string) => path.join(installPath, 'ComfyUI', 'output') +})) +vi.mock('./assets', () => ({ + assetKind: (name: string) => + ({ png: 'image', mp4: 'video', webm: 'video', wav: 'audio', glb: '3d' })[ + name.split('.').pop() ?? '' + ] ?? null +})) + +import type { InstallationRecord } from '../installations' +import { + MAX_FOLDER_DEPTH, + MAX_LOCAL_ASSETS, + THUMBNAIL_WAIT_MS, + listLocalAssets, + listOutputFolders, + outputDirOverride, + outputFileUrl, + serveOutputFile +} from './outputFolders' + +function install(name: string, extra: Record = {}): InstallationRecord { + return { + id: name, + name, + sourceId: 'standalone', + installPath: installDir, + createdAt: '2026-11-01T00:00:00Z', + ...extra + } +} + +const SAMPLE_VIDEO = path.resolve('src/renderer/public/install-showcase-scene/Assets/earpods.webm') + +function pngHeader(width: number, height: number): Buffer { + const header = Buffer.alloc(33) + Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(header, 0) + header.writeUInt32BE(13, 8) + header.write('IHDR', 12, 'ascii') + header.writeUInt32BE(width, 16) + header.writeUInt32BE(height, 20) + header.writeUInt8(8, 24) + header.writeUInt8(2, 25) + return header +} + +function write( + file: string, + body: string | Buffer = 'x', + modified = new Date('2026-11-01T00:00:00Z') +): void { + fs.mkdirSync(path.dirname(file), { recursive: true }) + fs.writeFileSync(file, body) + fs.utimesSync(file, modified, modified) +} + +beforeEach(() => { + vi.clearAllMocks() + thumbnailImage.mockReset() + fullImage.mockReset() + for (const dir of [sharedDir, ownDir, outsideDir, installDir]) + fs.rmSync(dir, { recursive: true, force: true }) + for (const dir of [sharedDir, ownDir, outsideDir, path.join(installDir, 'ComfyUI', 'output')]) + fs.mkdirSync(dir, { recursive: true }) + installs.mockResolvedValue([ + install('Test Instance'), + install('My Local 1'), + install('Apps', { useSharedOutput: false, outputDir: ownDir }), + install('Bare', { useSharedOutput: false }), + install('Cloud', { sourceId: 'cloud' }), + install('Old Desktop', { sourceId: 'desktop' }) + ]) +}) + +afterAll(() => fs.rmSync(root, { recursive: true, force: true })) + +describe('outputDirOverride', () => { + it('uses the shared folder unless the build turned sharing off', () => { + expect(outputDirOverride(install('a'), sharedDir)).toBe(sharedDir) + expect( + outputDirOverride(install('a', { useSharedOutput: false, outputDir: ownDir }), sharedDir) + ).toBe(ownDir) + expect(outputDirOverride(install('a', { useSharedOutput: false }), sharedDir)).toBeNull() + }) +}) + +describe('listOutputFolders', () => { + it('lists each real output folder once, leaving out Cloud and builds that manage their own paths', async () => { + expect(await listOutputFolders()).toEqual([ + sharedDir, + ownDir, + path.join(installDir, 'ComfyUI', 'output') + ]) + }) + + it('leaves out folders that do not exist yet', async () => { + installs.mockResolvedValue([ + install('Gone', { useSharedOutput: false, outputDir: path.join(root, 'missing') }) + ]) + expect(await listOutputFolders()).toEqual([sharedDir]) + }) +}) + +describe('listLocalAssets', () => { + it('lists media from every output folder and its subfolders, newest first', async () => { + write(path.join(sharedDir, 'old.png'), 'x', new Date('2026-10-01T00:00:00Z')) + write(path.join(sharedDir, 'video', 'new.mp4'), 'xx', new Date('2026-11-05T00:00:00Z')) + write(path.join(ownDir, 'voice.wav'), 'xxx', new Date('2026-11-03T00:00:00Z')) + write( + path.join(installDir, 'ComfyUI', 'output', 'mesh.glb'), + 'x', + new Date('2026-11-02T00:00:00Z') + ) + write(path.join(sharedDir, 'notes.txt')) + write(path.join(sharedDir, '.hidden.png')) + write(path.join(sharedDir, '.cache', 'inside-hidden.png')) + + const assets = await listLocalAssets() + + expect(assets.map((a) => a.name)).toEqual(['new.mp4', 'voice.wav', 'mesh.glb', 'old.png']) + const newVideo = path.join(sharedDir, 'video', 'new.mp4') + expect(assets[0]).toEqual({ + id: newVideo, + name: 'new.mp4', + kind: 'video', + sizeBytes: 2, + createdAt: '2026-11-05T00:00:00.000Z', + mimeType: null, + tags: ['output'], + previewUrl: outputFileUrl(newVideo), + thumbnailUrl: null, + width: null, + height: null, + durationSeconds: null + }) + const oldPicture = path.join(sharedDir, 'old.png') + expect(assets[3]!.thumbnailUrl, 'an image thumbnail link changes when the file does').toBe( + outputFileUrl(oldPicture, fs.statSync(oldPicture).mtimeMs) + ) + expect(assets[2]!.previewUrl, '3D files have no preview').toBeNull() + }) + + it('reports the size of each picture and video, and how long a video runs, before they load', async () => { + write(path.join(sharedDir, 'portrait.png'), pngHeader(640, 960)) + write(path.join(sharedDir, 'clip.webm'), fs.readFileSync(SAMPLE_VIDEO)) + write(path.join(sharedDir, 'broken.png'), 'not a picture') + + const facts = Object.fromEntries( + (await listLocalAssets()).map((a) => [ + a.name, + [a.width, a.height, a.durationSeconds && Math.round(a.durationSeconds)] + ]) + ) + + expect(facts).toEqual({ + 'portrait.png': [640, 960, null], + 'clip.webm': [714, 530, 8], + 'broken.png': [null, null, null] + }) + }) + + it('finds a file once when two builds save to the same folder', async () => { + write(path.join(sharedDir, 'shared.png')) + + expect((await listLocalAssets()).map((a) => a.name)).toEqual(['shared.png']) + }) + + it('keeps only the newest files when the folders are very large', async () => { + for (let i = 0; i <= MAX_LOCAL_ASSETS; i++) + write(path.join(sharedDir, `f${i}.png`), '', new Date(Date.UTC(2026, 0, 1, 0, 0, i))) + + const assets = await listLocalAssets() + + expect(assets).toHaveLength(MAX_LOCAL_ASSETS) + expect(assets.map((a) => a.name)).not.toContain('f0.png') + }) + + it('does not follow links out of an output folder or walk without end', async () => { + write(path.join(outsideDir, 'secret.png')) + fs.symlinkSync(outsideDir, path.join(sharedDir, 'escape')) + const deep = path.join( + sharedDir, + ...Array.from({ length: MAX_FOLDER_DEPTH + 1 }, (_, i) => `d${i}`) + ) + write(path.join(deep, 'too-deep.png')) + + expect(await listLocalAssets()).toEqual([]) + }) +}) + +describe('serveOutputFile', () => { + it('serves the file itself when the OS thumbnail is slow, then the thumbnail once it is made', async () => { + write(path.join(sharedDir, 'slow.png')) + await listOutputFolders() + let finishThumbnail: (image: unknown) => void = () => {} + thumbnailImage.mockReturnValueOnce(new Promise((resolve) => (finishThumbnail = resolve))) + const url = outputFileUrl(path.join(sharedDir, 'slow.png'), 1) + const platform = process.platform + Object.defineProperty(process, 'platform', { value: 'darwin' }) + try { + const startedAt = Date.now() + const slow = await serveOutputFile(new Request(url)) + + expect( + Date.now() - startedAt, + 'it waits for the thumbnail before giving up' + ).toBeGreaterThanOrEqual(THUMBNAIL_WAIT_MS - 50) + expect(await slow.text()).toMatch(/^file file:\/\/.*slow\.png$/) + + finishThumbnail({ + isEmpty: () => false, + getSize: () => ({ width: 100, height: 100 }), + toJPEG: () => Buffer.from('late') + }) + await vi.waitFor(async () => { + const later = await serveOutputFile(new Request(url)) + expect(later.headers.get('Content-Type')).toBe('image/jpeg') + expect(await later.text()).toBe('late') + }) + expect(thumbnailImage, 'the slow thumbnail is made once, not again').toHaveBeenCalledOnce() + } finally { + Object.defineProperty(process, 'platform', { value: platform }) + } + }) + + it('serves a file inside an output folder and refuses one outside', async () => { + write(path.join(sharedDir, 'picture.png')) + write(path.join(outsideDir, 'secret.png')) + await listOutputFolders() + + const ok = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'picture.png'))) + ) + expect(ok.status).toBe(200) + expect(fetchFile).toHaveBeenCalledWith(expect.stringMatching(/^file:\/\/.*picture\.png$/)) + + expect(ok.headers.get('X-Content-Type-Options')).toBe('nosniff') + expect(ok.headers.get('Content-Security-Policy')).toBe('sandbox') + + const refused = await serveOutputFile( + new Request(outputFileUrl(path.join(outsideDir, 'secret.png'))) + ) + expect(refused.status).toBe(404) + expect(fetchFile).toHaveBeenCalledOnce() + }) + + it('answers 404 for files it does not show, broken addresses and files that are gone', async () => { + write(path.join(sharedDir, 'page.html')) + write(path.join(sharedDir, 'gone.png')) + await listOutputFolders() + + const html = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'page.html'))) + ) + expect(html.status).toBe(404) + const broken = await serveOutputFile(new Request('comfy-output://file/%')) + expect(broken.status).toBe(404) + fetchFile.mockRejectedValueOnce(new Error('ENOENT')) + const gone = await serveOutputFile(new Request(outputFileUrl(path.join(sharedDir, 'gone.png')))) + expect(gone.status).toBe(404) + }) + + it('serves a small JPEG thumbnail, or the file itself when none can be made', async () => { + write(path.join(sharedDir, 'thumb.png')) + await listOutputFolders() + const resize = vi.fn(() => ({ toJPEG: () => Buffer.from('small') })) + const big = { isEmpty: () => false, getSize: () => ({ width: 2000, height: 1000 }), resize } + thumbnailImage.mockResolvedValueOnce(big) + fullImage.mockReturnValueOnce(big) + + const thumb = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'thumb.png'), 1)) + ) + expect(thumb.headers.get('Content-Type')).toBe('image/jpeg') + expect(await thumb.text()).toBe('small') + expect(resize).toHaveBeenCalledWith({ width: 480, height: 240 }) + expect(thumb.headers.get('X-Content-Type-Options')).toBe('nosniff') + + write(path.join(sharedDir, 'blank.png')) + const blank = { + isEmpty: () => false, + getSize: () => ({ width: 10, height: 10 }), + toJPEG: () => Buffer.alloc(0) + } + thumbnailImage.mockResolvedValueOnce(blank) + fullImage.mockReturnValueOnce(blank) + const empty = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'blank.png'), 1)) + ) + expect(empty.headers.get('Content-Type')).not.toBe('image/jpeg') + expect(await empty.text()).toMatch(/^file file:\/\/.*blank\.png$/) + + write(path.join(sharedDir, 'broken.png')) + thumbnailImage.mockRejectedValueOnce(new Error('no thumbnail')) + fullImage.mockReturnValueOnce({ isEmpty: () => true }) + const none = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'broken.png'), 1)) + ) + expect(await none.text()).toMatch(/^file file:\/\/.*broken\.png$/) + }) +}) diff --git a/src/main/lib/outputFolders.ts b/src/main/lib/outputFolders.ts new file mode 100644 index 000000000..1dac492f5 --- /dev/null +++ b/src/main/lib/outputFolders.ts @@ -0,0 +1,277 @@ +import fsp from 'fs/promises' +import type { Dirent } from 'fs' +import path from 'path' +import { pathToFileURL } from 'url' +import { ipcMain, nativeImage, net, protocol } from 'electron' +import * as installations from '../installations' +import type { InstallationRecord } from '../installations' +import * as settings from '../settings' +import { assetKind } from './assets' +import { mediaFactsFromFile } from './mediaFacts' +import { runPool } from '../sources/standalone/templateDownloadCore' +import { installOutputDir } from './models' +import { sourceMap } from './ipc/shared' +import type { AssetSummary } from '../../types/ipc' + +export const OUTPUT_FILES_SCHEME = 'comfy-output' + +const LIST_LOCAL_CHANNEL = 'assets:list-local' +export const MAX_LOCAL_ASSETS = 500 +export const MAX_FOLDER_DEPTH = 6 +const THUMBNAIL_SIZE = 480 +const MAX_CACHED_THUMBNAILS = 400 +const THUMBNAILS_AT_ONCE = 2 +export const THUMBNAIL_WAIT_MS = 1500 +const STATS_AT_ONCE = 32 +const ROOTS_FRESH_MS = 5000 +const MAX_LINUX_THUMBNAIL_SOURCE_BYTES = 40 * 1024 * 1024 +const SERVED_KINDS = new Set(['image', 'video', 'audio']) + +const thumbnails = new Map() +const thumbnailsInFlight = new Map>() +const thumbnailQueue: (() => void)[] = [] +let thumbnailsRunning = 0 +let allowedRoots: string[] = [] +let allowedRootsAt = 0 + +export function sharedOutputDir(): string { + return (settings.get('outputDir') as string | undefined) || settings.defaults.outputDir +} + +export function outputDirOverride(inst: InstallationRecord, sharedDir: string): string | null { + if ((inst.useSharedOutput as boolean | undefined) !== false) return sharedDir + return (inst.outputDir as string | undefined) || null +} + +function outputDirFor(inst: InstallationRecord, sharedDir: string): string | null { + const source = sourceMap[inst.sourceId] + if (source?.category !== 'local' || source.getLaunchCommand(inst)?.skipSharedPaths) return null + return outputDirOverride(inst, sharedDir) ?? installOutputDir(inst.installPath) +} + +async function realDir(dir: string): Promise { + const real = await fsp.realpath(dir).catch(() => null) + if (!real) return null + const stat = await fsp.stat(real).catch(() => null) + return stat?.isDirectory() ? real : null +} + +export async function listOutputFolders(): Promise { + const shared = sharedOutputDir() + const folders = new Set() + const sharedReal = await realDir(shared) + if (sharedReal) folders.add(sharedReal) + for (const inst of await installations.list()) { + const dir = outputDirFor(inst, shared) + const real = dir ? await realDir(dir) : null + if (real) folders.add(real) + } + allowedRoots = [...folders] + allowedRootsAt = Date.now() + return allowedRoots +} + +function isInsideRoot(target: string, root: string): boolean { + const relative = path.relative(root, target) + return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative)) +} + +async function allowedPath(target: string): Promise { + if (!path.isAbsolute(target)) return null + if (Date.now() - allowedRootsAt > ROOTS_FRESH_MS) await listOutputFolders() + const real = await fsp.realpath(target).catch(() => null) + if (!real) return null + return allowedRoots.some((root) => isInsideRoot(real, root)) ? real : null +} + +export function outputFileUrl(file: string, thumbnailOf?: number): string { + const url = `${OUTPUT_FILES_SCHEME}://file/${encodeURIComponent(file)}` + return thumbnailOf === undefined ? url : `${url}?thumbnail=${Math.round(thumbnailOf)}` +} + +async function collectMediaFiles(root: string): Promise { + const files: string[] = [] + const walk = async (dir: string, depth: number): Promise => { + const dirents: Dirent[] = await fsp.readdir(dir, { withFileTypes: true }).catch(() => []) + const subfolders: string[] = [] + for (const dirent of dirents) { + if (dirent.name.startsWith('.')) continue + const full = path.join(dir, dirent.name) + if (dirent.isDirectory()) subfolders.push(full) + else if (dirent.isFile() && assetKind(dirent.name) !== null) files.push(full) + } + if (depth < MAX_FOLDER_DEPTH) { + for (const sub of subfolders) await walk(sub, depth + 1) + } + } + await walk(root, 0) + return files +} + +function localSummary(file: string, sizeBytes: number, modifiedMs: number): AssetSummary { + const kind = assetKind(file) + return { + id: file, + name: path.basename(file), + kind, + sizeBytes, + createdAt: new Date(modifiedMs).toISOString(), + mimeType: null, + tags: ['output'], + previewUrl: kind && SERVED_KINDS.has(kind) ? outputFileUrl(file) : null, + thumbnailUrl: kind === 'image' ? outputFileUrl(file, modifiedMs) : null, + width: null, + height: null, + durationSeconds: null + } +} + +async function addMediaFacts(asset: AssetSummary): Promise { + Object.assign(asset, await mediaFactsFromFile(asset.id, asset.kind)) +} + +export async function listLocalAssets(): Promise { + const folders = await listOutputFolders() + const files = [...new Set((await Promise.all(folders.map(collectMediaFiles))).flat())] + const found: AssetSummary[] = [] + await runPool(files, STATS_AT_ONCE, async (file) => { + const stat = await fsp.stat(file).catch(() => null) + if (!stat?.isFile()) return + const modifiedMs = Number.isFinite(stat.mtimeMs) ? stat.mtimeMs : 0 + found.push(localSummary(file, stat.size, modifiedMs)) + }) + const newest = found + .sort((a, b) => b.createdAt!.localeCompare(a.createdAt!)) + .slice(0, MAX_LOCAL_ASSETS) + await runPool(newest, STATS_AT_ONCE, addMediaFacts) + return newest +} + +function linuxThumbnailSource(file: string, sizeBytes: number): Electron.NativeImage | null { + if (assetKind(file) !== 'image' || sizeBytes > MAX_LINUX_THUMBNAIL_SOURCE_BYTES) return null + try { + return nativeImage.createFromPath(file) + } catch { + return null + } +} + +async function makeThumbnail(file: string, sizeBytes: number): Promise { + const size = { width: THUMBNAIL_SIZE, height: THUMBNAIL_SIZE } + const image = + process.platform === 'linux' + ? linuxThumbnailSource(file, sizeBytes) + : await nativeImage.createThumbnailFromPath(file, size).catch(() => null) + if (!image || image.isEmpty()) return null + const { width, height } = image.getSize() + const scale = Math.min(1, THUMBNAIL_SIZE / Math.max(width, height)) + const scaled = + scale < 1 + ? image.resize({ + width: Math.max(1, Math.round(width * scale)), + height: Math.max(1, Math.round(height * scale)) + }) + : image + const jpeg = scaled.toJPEG(82) + return jpeg.length > 0 ? jpeg : null +} + +async function fewAtATime(work: () => Promise): Promise { + while (thumbnailsRunning >= THUMBNAILS_AT_ONCE) + await new Promise((resolve) => thumbnailQueue.push(resolve)) + thumbnailsRunning++ + try { + return await work() + } finally { + thumbnailsRunning-- + thumbnailQueue.shift()?.() + } +} + +async function thumbnailFor(file: string): Promise { + const stat = await fsp.stat(file).catch(() => null) + if (!stat) return null + const key = `${file}\n${stat.mtimeMs}` + const cached = thumbnails.get(key) + if (cached) return cached + let making = thumbnailsInFlight.get(key) + if (!making) { + making = fewAtATime(() => makeThumbnail(file, stat.size)) + .then((made) => { + if (made) { + thumbnails.set(key, made) + if (thumbnails.size > MAX_CACHED_THUMBNAILS) + thumbnails.delete(thumbnails.keys().next().value!) + } + return made + }) + .catch(() => null) + .finally(() => thumbnailsInFlight.delete(key)) + thumbnailsInFlight.set(key, making) + } + let timer: ReturnType | undefined + const tooSlow = new Promise((resolve) => { + timer = setTimeout(() => resolve(null), THUMBNAIL_WAIT_MS) + }) + try { + return await Promise.race([making, tooSlow]) + } finally { + clearTimeout(timer) + } +} + +function decodedPath(url: URL): string | null { + if (url.host !== 'file') return null + try { + return decodeURIComponent(url.pathname.slice(1)) + } catch { + return null + } +} + +function notFound(): Response { + return new Response(null, { status: 404 }) +} + +async function fetchFile(file: string, request: Request): Promise { + const res = await net + .fetch(pathToFileURL(file).toString(), { headers: request.headers }) + .catch(() => null) + if (!res) return notFound() + const headers = new Headers(res.headers) + headers.set('X-Content-Type-Options', 'nosniff') + headers.set('Content-Security-Policy', 'sandbox') + return new Response(res.body, { status: res.status, statusText: res.statusText, headers }) +} + +export async function serveOutputFile(request: Request): Promise { + const url = new URL(request.url) + const target = decodedPath(url) + const kind = target ? assetKind(target) : null + const file = target && kind && SERVED_KINDS.has(kind) ? await allowedPath(target) : null + if (!file) return notFound() + if (!url.searchParams.has('thumbnail')) return fetchFile(file, request) + const jpeg = await thumbnailFor(file) + if (!jpeg) return fetchFile(file, request) + return new Response(new Uint8Array(jpeg), { + headers: { + 'Content-Type': 'image/jpeg', + 'Cache-Control': 'private, max-age=3600', + 'X-Content-Type-Options': 'nosniff' + } + }) +} + +export function registerOutputFilesScheme(): void { + protocol.registerSchemesAsPrivileged([ + { + scheme: OUTPUT_FILES_SCHEME, + privileges: { standard: true, secure: true, stream: true } + } + ]) +} + +export function registerOutputFolderHandlers(): void { + protocol.handle(OUTPUT_FILES_SCHEME, serveOutputFile) + ipcMain.handle(LIST_LOCAL_CHANNEL, () => listLocalAssets()) +} diff --git a/src/preload/api.ts b/src/preload/api.ts index 11121193b..8f6a2cb6e 100644 --- a/src/preload/api.ts +++ b/src/preload/api.ts @@ -98,6 +98,8 @@ export function buildElectronApi(): ElectronApi { closeAnnouncement: () => ipcRenderer.send('comfy-window:close-announcement'), signalOverlayReady: () => ipcRenderer.send('comfy-window:overlay-ready'), navigatePanel: (page) => ipcRenderer.send('comfy-window:navigate-panel', page), + listAssets: (installationId) => ipcRenderer.invoke('assets:list', installationId), + listLocalAssets: () => ipcRenderer.invoke('assets:list-local'), listHomeWorkflows: () => ipcRenderer.invoke('catalog:list-home-workflows'), openCatalogWorkflow: (id) => ipcRenderer.invoke('catalog:open-workflow', id), openCatalogWorkflowHub: () => ipcRenderer.invoke('catalog:open-workflow-hub'), diff --git a/src/renderer/csp.test.ts b/src/renderer/csp.test.ts index 9c9e0d0a1..ad4376fc7 100644 --- a/src/renderer/csp.test.ts +++ b/src/renderer/csp.test.ts @@ -47,6 +47,13 @@ describe('Content-Security-Policy: panel.html', () => { expect(csp['script-src']).toBe("'self'") }) + it('shows output files from this computer only through the output-folder protocol', () => { + expect(csp['img-src']).toContain('comfy-output:') + expect(csp['media-src']).toContain('comfy-output:') + expect(csp['img-src']).not.toContain('127.0.0.1') + expect(csp['media-src']).not.toContain('127.0.0.1') + }) + it('restricts default-src to self only', () => { expect(csp['default-src']).toBe("'self'") }) diff --git a/src/renderer/panel.html b/src/renderer/panel.html index a97228cc1..e0ee5a255 100644 --- a/src/renderer/panel.html +++ b/src/renderer/panel.html @@ -5,7 +5,7 @@ Comfy Desktop — Panel