diff --git a/locales/en.json b/locales/en.json index bf2bcc972..941af440a 100644 --- a/locales/en.json +++ b/locales/en.json @@ -98,6 +98,8 @@ "showInFinder": "Show in Finder", "showInExplorer": "Show in File Explorer", "showInFolder": "Show in folder", + "viewInAssets": "View in Assets", + "notFoundInCloud": "Couldn't find {name} in your Comfy Cloud assets. It may have a different name here.", "copy": "Copy", "copyFailed": "Couldn't copy asset", "delete": "Delete", @@ -121,7 +123,8 @@ "zoom": "Zoom", "zoomLevel": "Zoom: {level}", "openWorkflow": "Open workflow", - "savedIn": "Saved in" + "savedIn": "Saved in", + "cloudAssets": "Comfy Cloud assets" }, "kindSingular": { "image": "Image", diff --git a/locales/zh.json b/locales/zh.json index 5d16f5b78..4e3f25c6e 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -98,6 +98,8 @@ "showInFinder": "在 Finder 中显示", "showInExplorer": "在文件资源管理器中显示", "showInFolder": "在文件夹中显示", + "viewInAssets": "在资产中查看", + "notFoundInCloud": "在你的 Comfy Cloud 资产中找不到 {name}。它在这里的名称可能不同。", "copy": "复制", "copyFailed": "无法复制资产", "delete": "删除", @@ -121,7 +123,8 @@ "zoom": "缩放", "zoomLevel": "缩放:{level}", "openWorkflow": "打开工作流", - "savedIn": "保存位置" + "savedIn": "保存位置", + "cloudAssets": "Comfy Cloud 资产" }, "kindSingular": { "image": "图片", diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index 736b19656..ef23aab40 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -73,6 +73,7 @@ import { loadedAppPage, sendAppOutputAction, showAppOutputs, + shownOutputFile, showAppPage } from './appPageView' import { trackFromAppsPage } from '../lib/ipc/registerAppsTelemetryHandlers' @@ -1090,6 +1091,34 @@ describe('outputs an app page shows', () => { expect(sentToPage()).toEqual([]) }) + it('names the file Desktop found for a shown output, only while that page is loaded', () => { + expect(shownOutputFile(panelContents(), 0)).toBeNull() + + showAppOutputs(pageContents(), loadedAppPage(pageContents())!, OUTPUTS, [ + null, + '/out/cutout-2.png' + ]) + + expect(shownOutputFile(panelContents(), 0)).toBeNull() + expect(shownOutputFile(panelContents(), 1)).toBe('/out/cutout-2.png') + expect(shownOutputFile(panelContents(), 2)).toBeNull() + expect(shownOutputFile(new FakeContents() as unknown as WebContents, 1)).toBeNull() + + finishLoading() + + expect(shownOutputFile(panelContents(), 1)).toBeNull() + }) + + it('sends Show in folder back to the page like any other action', () => { + shownFromPage() + + sendAppOutputAction(panelContents(), { action: 'show-in-folder', index: 1 }) + + expect(sentToPage()).toEqual([ + [APP_OUTPUT_ACTION_CHANNEL, { action: 'show-in-folder', index: 1 }] + ]) + }) + it('takes actions only from the panel of the window the page is in', () => { shownFromPage() diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index 059e33d1d..368b7744c 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -39,6 +39,7 @@ export interface LoadedAppPage { interface ShownOutputs { page: LoadedAppPage count: number + files: readonly (string | null)[] } interface HostedPage { @@ -437,13 +438,14 @@ function isStillLoaded(hosted: HostedPage, page: LoadedAppPage): boolean { export function showAppOutputs( contents: WebContents, sentFrom: LoadedAppPage, - outputs: AppOutputs + outputs: AppOutputs, + files: readonly (string | null)[] = [] ): boolean { for (const [windowKey, hosted] of hostedByWindow) { if (hosted.view.webContents !== contents) continue const panel = panelOf(windowKey) if (!panel || !isOnScreen(hosted) || !isStillLoaded(hosted, sentFrom)) return false - hosted.outputs = { page: sentFrom, count: outputs.items.length } + hosted.outputs = { page: sentFrom, count: outputs.items.length, files } if (comfyWindows.get(windowKey)?.window.isFocused()) panel.focus() panel.send(APP_OUTPUTS_CHANNEL, sentFrom.appId, outputs) return true @@ -451,12 +453,21 @@ export function showAppOutputs( return false } -export function sendAppOutputAction(panel: WebContents, action: AppOutputAction): void { +function shownOutputsOf(panel: WebContents): { hosted: HostedPage; shown: ShownOutputs } | null { const host = hostOf(panel) const hosted = host && hostedFor(host) const shown = hosted?.outputs - if (!hosted || !shown || action.index >= shown.count || !isStillLoaded(hosted, shown.page)) return - hosted.view.webContents.send(APP_OUTPUT_ACTION_CHANNEL, action) + return hosted && shown && isStillLoaded(hosted, shown.page) ? { hosted, shown } : null +} + +export function shownOutputFile(panel: WebContents, index: number): string | null { + return shownOutputsOf(panel)?.shown.files[index] ?? null +} + +export function sendAppOutputAction(panel: WebContents, action: AppOutputAction): void { + const found = shownOutputsOf(panel) + if (!found || action.index >= found.shown.count) return + found.hosted.view.webContents.send(APP_OUTPUT_ACTION_CHANNEL, action) } export function _resetForTest(): void { diff --git a/src/main/apps/outputFiles.test.ts b/src/main/apps/outputFiles.test.ts new file mode 100644 index 000000000..6bb88c1c8 --- /dev/null +++ b/src/main/apps/outputFiles.test.ts @@ -0,0 +1,117 @@ +import path from 'path' +import { describe, expect, it, vi } from 'vitest' + +vi.mock('../installations', () => ({ get: vi.fn() })) +vi.mock('../lib/outputFolders', () => ({ comfyOutputPath: vi.fn() })) +vi.mock('./localComfy', () => ({ pageBuildId: vi.fn(() => 'apps-build') })) + +import type { WebContents } from 'electron' +import * as installations from '../installations' +import { findInPageBuild, locateOutputFiles } from './outputFiles' +import type { FindOutputFile } from './outputFiles' +import type { RequestedOutput } from './outputRequest' + +const FOUND = path.join(path.sep, 'outputs', 'apps', 'cutout_00001_.png') + +function output(extra: Partial = {}): RequestedOutput { + return { + name: 'cutout.png', + mimeType: 'image/png', + bytes: new Uint8Array([1]), + tags: [], + workflow: false, + ...extra + } +} + +const cutoutFile = { filename: 'cutout_00001_.png', subfolder: 'apps' } + +describe('locateOutputFiles', () => { + it('shows the folder of a ComfyUI file Desktop found, and never sends the reference on', async () => { + const find = vi.fn(async () => FOUND) + + const located = await locateOutputFiles( + { items: [output({ savedOn: 'computer', comfyFile: cutoutFile })], index: 0 }, + find + ) + + expect(find).toHaveBeenCalledExactlyOnceWith(cutoutFile, new Uint8Array([1])) + expect(located.outputs.items[0]).toEqual( + output({ savedOn: 'computer', folder: path.dirname(FOUND) }) + ) + expect(located.files).toEqual([FOUND]) + }) + + it('shows no folder for a ComfyUI file Desktop did not find, even one the page sent', async () => { + const located = await locateOutputFiles( + { + items: [ + output({ savedOn: 'computer', comfyFile: cutoutFile, folder: '/Users/me/out' }), + output({ savedOn: 'computer', comfyFile: cutoutFile }) + ], + index: 1 + }, + vi.fn().mockResolvedValueOnce(null).mockRejectedValueOnce(new Error('gone')) + ) + + expect(located.outputs).toEqual({ + items: [output({ savedOn: 'computer' }), output({ savedOn: 'computer' })], + index: 1 + }) + expect(located.files).toEqual([null, null]) + }) + + it('keeps the folder a page saved an output in, and looks for nothing without a ComfyUI file', async () => { + const find = vi.fn() + + const located = await locateOutputFiles( + { + items: [ + output({ savedOn: 'computer', folder: '/Users/me/out' }), + output({ savedOn: 'cloud' }), + output() + ], + index: 0 + }, + find + ) + + expect(find).not.toHaveBeenCalled() + expect(located.outputs.items).toEqual([ + output({ savedOn: 'computer', folder: '/Users/me/out' }), + output({ savedOn: 'cloud' }), + output() + ]) + expect(located.files).toEqual([null, null, null]) + }) + + it('looks for at most four files at once', async () => { + let looking = 0 + let most = 0 + const find = vi.fn(async () => { + looking += 1 + most = Math.max(most, looking) + await new Promise((resolve) => setTimeout(resolve, 1)) + looking -= 1 + return null + }) + const items = Array.from({ length: 10 }, () => + output({ savedOn: 'computer', comfyFile: cutoutFile }) + ) + + await locateOutputFiles({ items, index: 0 }, find) + + expect(find).toHaveBeenCalledTimes(10) + expect(most).toBe(4) + }) +}) + +describe('findInPageBuild', () => { + it('finds nothing when the build cannot be read', async () => { + vi.mocked(installations.get).mockRejectedValueOnce(new Error('unreadable')) + + const find = await findInPageBuild({} as WebContents) + + expect(await find(cutoutFile, new Uint8Array([1]))).toBeNull() + }) +}) diff --git a/src/main/apps/outputFiles.ts b/src/main/apps/outputFiles.ts new file mode 100644 index 000000000..4706a0869 --- /dev/null +++ b/src/main/apps/outputFiles.ts @@ -0,0 +1,41 @@ +import path from 'path' +import type { WebContents } from 'electron' + +import type { AppOutput, AppOutputs } from '../../types/ipc' +import * as installations from '../installations' +import { comfyOutputPath } from '../lib/outputFolders' +import { runPool } from '../sources/standalone/templateDownloadCore' +import { pageBuildId } from './localComfy' +import type { ComfyOutputFile, OutputRequest } from './outputRequest' + +const FILES_AT_ONCE = 4 + +export type FindOutputFile = (file: ComfyOutputFile, bytes: Uint8Array) => Promise + +export interface LocatedOutputs { + outputs: AppOutputs + files: (string | null)[] +} + +export async function locateOutputFiles( + request: OutputRequest, + find: FindOutputFile +): Promise { + const files: (string | null)[] = request.items.map(() => null) + await runPool(request.items, FILES_AT_ONCE, async (item, i) => { + if (item.comfyFile) files[i] = await find(item.comfyFile, item.bytes).catch(() => null) + }) + const items = request.items.map(({ comfyFile, folder, ...output }, i): AppOutput => { + const file = files[i] + if (file) return { ...output, folder: path.dirname(file) } + return folder && !comfyFile ? { ...output, folder } : output + }) + return { outputs: { items, index: request.index }, files } +} + +export async function findInPageBuild(page: WebContents): Promise { + const buildId = pageBuildId(page) + const build = buildId ? await installations.get(buildId).catch(() => null) : null + if (!build) return async () => null + return (file, bytes) => comfyOutputPath(build, file, bytes) +} diff --git a/src/main/apps/outputRequest.test.ts b/src/main/apps/outputRequest.test.ts index e8d46069f..df222e12a 100644 --- a/src/main/apps/outputRequest.test.ts +++ b/src/main/apps/outputRequest.test.ts @@ -35,7 +35,7 @@ describe('readOutputRequest', () => { it('keeps what the Apps pages send today and drops the fields Desktop does not show', () => { const request = readOutputRequest({ - items: [picture({ tags: [], workflow: false, savedOn: 'computer', folder: '/tmp' })], + items: [picture({ tags: [], workflow: false, savedOn: 'computer', folder: '/tmp', size: 3 })], index: 0 }) @@ -44,10 +44,74 @@ describe('readOutputRequest', () => { mimeType: 'image/png', bytes: new Uint8Array([1, 2, 3]), tags: [], - workflow: false + workflow: false, + savedOn: 'computer', + folder: '/tmp' }) }) + it('keeps where each output is saved, with a folder and a ComfyUI file only for this computer', () => { + const comfyFile = { filename: 'cutout_00001_.png', subfolder: 'apps/cutout', type: 'output' } + const request = readOutputRequest({ + items: [ + picture({ savedOn: 'cloud', folder: '/Users/me/out', comfyFile }), + picture({ savedOn: 'computer', folder: 'C:\\Users\\me\\out' }), + picture({ savedOn: 'computer', comfyFile }), + picture({ savedOn: 'computer' }), + picture({ savedOn: 'phone', folder: '/Users/me/out' }) + ], + index: 0 + }) + + expect( + request?.items.map(({ savedOn, folder, comfyFile }) => ({ savedOn, folder, comfyFile })) + ).toEqual([ + { savedOn: 'cloud', folder: undefined, comfyFile: undefined }, + { savedOn: 'computer', folder: 'C:\\Users\\me\\out', comfyFile: undefined }, + { + savedOn: 'computer', + folder: undefined, + comfyFile: { filename: 'cutout_00001_.png', subfolder: 'apps/cutout' } + }, + { savedOn: 'computer', folder: undefined, comfyFile: undefined }, + { savedOn: undefined, folder: undefined, comfyFile: undefined } + ]) + }) + + it('drops a folder that is not an absolute path or has hidden characters', () => { + const folders = ['out', './out', '~/out', '/Users/me/\u202eout', '/Users/me/\nout', 7] + const request = readOutputRequest({ + items: folders.map((folder) => picture({ savedOn: 'computer', folder })), + index: 0 + }) + + expect(request?.items.map((item) => item.folder)).toEqual(folders.map(() => undefined)) + }) + + it('drops a ComfyUI file that is not a ComfyUI output, or has a very long path', () => { + const file = (extra: Record) => ({ + filename: 'cutout.png', + subfolder: '', + type: 'output', + ...extra + }) + const refused = [ + file({ type: 'temp' }), + file({ type: undefined }), + file({ filename: 7 }), + file({ subfolder: 7 }), + file({ filename: 'n'.repeat(1025) }), + file({ subfolder: 's'.repeat(1020) }), + 'cutout.png' + ] + const request = readOutputRequest({ + items: refused.map((comfyFile) => picture({ savedOn: 'computer', comfyFile })), + index: 0 + }) + + expect(request?.items.map((item) => item.comfyFile)).toEqual(refused.map(() => undefined)) + }) + it('cuts long names and tags, and keeps at most eight tags', () => { const request = readOutputRequest({ items: [ @@ -133,7 +197,11 @@ describe('readOutputRequest', () => { }) describe('readOutputAction', () => { - it('passes on a download or an open-workflow for one output', () => { + it('passes on a download, an open-workflow or a show-in-folder for one output', () => { + expect(readOutputAction({ action: 'show-in-folder', index: 1 })).toEqual({ + action: 'show-in-folder', + index: 1 + }) expect(readOutputAction({ action: 'download', index: 2 })).toEqual({ action: 'download', index: 2 diff --git a/src/main/apps/outputRequest.ts b/src/main/apps/outputRequest.ts index 11e5acbfb..f4278f937 100644 --- a/src/main/apps/outputRequest.ts +++ b/src/main/apps/outputRequest.ts @@ -1,10 +1,25 @@ import type { AppOutput, AppOutputAction, AppOutputs } from '../../types/ipc' +export interface ComfyOutputFile { + filename: string + subfolder: string +} + +export interface RequestedOutput extends AppOutput { + comfyFile?: ComfyOutputFile +} + +export interface OutputRequest extends Omit { + items: RequestedOutput[] +} + export const MAX_OUTPUTS = 40 export const MAX_OUTPUTS_BYTES = 100_000_000 const MAX_NAME_LENGTH = 200 const MAX_TAGS = 8 const MAX_TAG_LENGTH = 40 +const MAX_PATH_LENGTH = 1024 +const ABSOLUTE_LOCAL_PATH = /^(\/|[A-Za-z]:[\\/])/ const INVISIBLE_FORMATTING = /[\u0080-\u009f\u061c\u200b-\u200f\u2028-\u202e\u2066-\u2069\ufeff]/ const FIRST_PRINTABLE_CODE = 32 const DELETE_CODE = 127 @@ -43,7 +58,30 @@ function readTags(value: unknown): string[] { .filter((tag): tag is string => tag !== null) } -function readOutput(value: unknown): AppOutput | null { +function readFolder(value: unknown): string | null { + if (typeof value !== 'string' || value.length > MAX_PATH_LENGTH) return null + if (hasHiddenCharacters(value)) return null + return ABSOLUTE_LOCAL_PATH.test(value) ? value : null +} + +function readComfyFile(value: unknown): ComfyOutputFile | null { + if (!isRecord(value)) return null + const { filename, subfolder = '', type } = value + if (type !== 'output' || typeof filename !== 'string' || typeof subfolder !== 'string') + return null + return filename.length + subfolder.length > MAX_PATH_LENGTH ? null : { filename, subfolder } +} + +function readSavedPlace(value: Record): Partial { + const savedOn = value['savedOn'] + if (savedOn === 'cloud') return { savedOn } + if (savedOn !== 'computer') return {} + const folder = readFolder(value['folder']) + const comfyFile = readComfyFile(value['comfyFile']) + return { savedOn, ...(folder ? { folder } : {}), ...(comfyFile ? { comfyFile } : {}) } +} + +function readOutput(value: unknown): RequestedOutput | null { if (!isRecord(value)) return null const name = trimmed(value['name'], MAX_NAME_LENGTH) const { mimeType, bytes } = value @@ -54,18 +92,19 @@ function readOutput(value: unknown): AppOutput | null { mimeType, bytes: bytes as Uint8Array, tags: readTags(value['tags']), - workflow: value['workflow'] === true + workflow: value['workflow'] === true, + ...readSavedPlace(value) } } -export function readOutputRequest(raw: unknown): AppOutputs | null { +export function readOutputRequest(raw: unknown): OutputRequest | null { if (!isRecord(raw)) return null const { items, index } = raw if (!Array.isArray(items) || items.length === 0 || items.length > MAX_OUTPUTS) return null if (!isIndex(index) || index >= items.length) return null const outputs = Array.from(items, readOutput) if (outputs.some((output) => output === null)) return null - const shown = outputs as AppOutput[] + const shown = outputs as RequestedOutput[] const sentBytes = shown.reduce((sum, output) => sum + output.bytes.buffer.byteLength, 0) return sentBytes > MAX_OUTPUTS_BYTES ? null : { items: shown, index } } @@ -73,6 +112,7 @@ export function readOutputRequest(raw: unknown): AppOutputs | null { export function readOutputAction(raw: unknown): AppOutputAction | null { if (!isRecord(raw)) return null const { action, index } = raw - if (action !== 'download' && action !== 'open-workflow') return null + if (action !== 'download' && action !== 'open-workflow' && action !== 'show-in-folder') + return null return isIndex(index) ? { action, index } : null } diff --git a/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts b/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts index 19025cead..1209cc05e 100644 --- a/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts +++ b/src/main/lib/ipc/registerAppsOutputsHandlers.test.ts @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' type Handler = (event: unknown, request: unknown) => Promise type Listener = (event: unknown, raw: unknown) => void +type FindFile = (file: unknown, bytes: Uint8Array) => Promise interface LoadedPage { appId: string document: number @@ -10,11 +11,17 @@ interface LoadedPage { const mocks = vi.hoisted(() => ({ handlers: new Map(), listeners: new Map(), + findFile: vi.fn(async () => null), + shownFile: null as string | null, sendAppOutputAction: vi.fn(), + shownOutputFile: vi.fn((_panel: unknown, _index: number) => mocks.shownFile), + revealOutputFile: vi.fn(async (_file: string) => true), isAppsPage: true, appsEnabled: true, page: { appId: 'remove-background', document: 1 } as LoadedPage | null, - showAppOutputs: vi.fn((_contents: unknown, _sentFrom: unknown, _outputs: unknown) => true) + showAppOutputs: vi.fn( + (_contents: unknown, _sentFrom: unknown, _outputs: unknown, _files: unknown) => true + ) })) vi.mock('electron', () => ({ @@ -28,7 +35,15 @@ vi.mock('../../apps/appsPages', () => ({ isAppsPageSender: () => mocks.isAppsPag vi.mock('../../apps/appPageView', () => ({ loadedAppPage: () => mocks.page, showAppOutputs: mocks.showAppOutputs, - sendAppOutputAction: mocks.sendAppOutputAction + sendAppOutputAction: mocks.sendAppOutputAction, + shownOutputFile: mocks.shownOutputFile +})) +vi.mock('../../apps/localComfy', () => ({ pageBuildId: () => 'apps-build' })) +vi.mock('../../installations', () => ({ get: async (id: string) => ({ id }) })) +vi.mock('../outputFolders', () => ({ + comfyOutputPath: (_build: unknown, file: unknown, bytes: Uint8Array) => + mocks.findFile(file, bytes), + revealOutputFile: mocks.revealOutputFile })) import { @@ -56,6 +71,8 @@ beforeEach(() => { vi.clearAllMocks() mocks.handlers.clear() mocks.listeners.clear() + mocks.shownFile = null + mocks.findFile.mockResolvedValue(null) mocks.isAppsPage = true mocks.appsEnabled = true mocks.page = { appId: 'remove-background', document: 1 } @@ -85,8 +102,57 @@ describe('showOutput from the hosted Apps page', () => { } ], index: 0 - } + }, + [null] + ) + }) + + it('shows where Desktop found the ComfyUI file the page named, and keeps the path for Show in folder', async () => { + mocks.findFile.mockResolvedValue('/outputs/apps/cutout_00001_.png') + + await showOutput({ + items: [ + { + ...REQUEST.items[0], + savedOn: 'computer', + comfyFile: { filename: 'cutout_00001_.png', subfolder: 'apps', type: 'output' } + } + ], + index: 0 + }) + + expect(mocks.findFile).toHaveBeenCalledExactlyOnceWith( + { filename: 'cutout_00001_.png', subfolder: 'apps' }, + new Uint8Array([1]) ) + const [, , outputs, files] = mocks.showAppOutputs.mock.calls[0]! + expect(outputs).toMatchObject({ items: [{ savedOn: 'computer', folder: '/outputs/apps' }] }) + expect(files).toEqual(['/outputs/apps/cutout_00001_.png']) + }) + + it('shows nothing from a request that a newer one replaced while Desktop looked for its files', async () => { + let finishFirst: (file: string | null) => void = () => {} + mocks.findFile.mockImplementationOnce( + () => new Promise((resolve) => (finishFirst = resolve)) + ) + const withFile = { + items: [ + { + ...REQUEST.items[0], + savedOn: 'computer', + comfyFile: { filename: 'a.png', type: 'output' } + } + ], + index: 0 + } + + const first = showOutput(withFile) + await vi.waitFor(() => expect(mocks.findFile).toHaveBeenCalledTimes(1)) + expect(await showOutput()).toBe(true) + finishFirst('/outputs/a.png') + + expect(await first).toBe(false) + expect(mocks.showAppOutputs).toHaveBeenCalledTimes(1) }) it('says so when Desktop does not show them', async () => { @@ -131,9 +197,34 @@ describe('an output action from the panel', () => { expect(APPS_OUTPUT_ACTION_CHANNEL).toBe('apps:output-action') }) - it('sends Download to the page', () => { + it('shows a file Desktop found in its folder itself, and does not send it to the page', () => { + mocks.shownFile = '/outputs/apps/cutout_00001_.png' + + takeAction({ action: 'show-in-folder', index: 0 }) + + expect(mocks.shownOutputFile).toHaveBeenCalledWith(PANEL_EVENT.sender, 0) + expect(mocks.revealOutputFile).toHaveBeenCalledExactlyOnceWith( + '/outputs/apps/cutout_00001_.png' + ) + expect(mocks.sendAppOutputAction).not.toHaveBeenCalled() + }) + + it('sends Show in folder to the page when Desktop found no file for that output', () => { + takeAction({ action: 'show-in-folder', index: 0 }) + + expect(mocks.revealOutputFile).not.toHaveBeenCalled() + expect(mocks.sendAppOutputAction).toHaveBeenCalledExactlyOnceWith(PANEL_EVENT.sender, { + action: 'show-in-folder', + index: 0 + }) + }) + + it('sends Download to the page even when Desktop found the file', () => { + mocks.shownFile = '/outputs/apps/cutout_00001_.png' + takeAction({ action: 'download', index: 0 }) + expect(mocks.revealOutputFile).not.toHaveBeenCalled() expect(mocks.sendAppOutputAction).toHaveBeenCalledExactlyOnceWith(PANEL_EVENT.sender, { action: 'download', index: 0 @@ -143,6 +234,7 @@ describe('an output action from the panel', () => { it('drops an action it cannot read', () => { takeAction({ action: 'delete', index: 0 }) + expect(mocks.revealOutputFile).not.toHaveBeenCalled() expect(mocks.sendAppOutputAction).not.toHaveBeenCalled() }) }) diff --git a/src/main/lib/ipc/registerAppsOutputsHandlers.ts b/src/main/lib/ipc/registerAppsOutputsHandlers.ts index a8e93cb10..26e05d9b7 100644 --- a/src/main/lib/ipc/registerAppsOutputsHandlers.ts +++ b/src/main/lib/ipc/registerAppsOutputsHandlers.ts @@ -1,18 +1,33 @@ import { ipcMain } from 'electron' -import type { IpcMainEvent, IpcMainInvokeEvent } from 'electron' +import type { IpcMainEvent, IpcMainInvokeEvent, WebContents } from 'electron' -import { loadedAppPage, sendAppOutputAction, showAppOutputs } from '../../apps/appPageView' +import { + loadedAppPage, + sendAppOutputAction, + showAppOutputs, + shownOutputFile +} from '../../apps/appPageView' import { areAppsEnabled } from '../../apps/appsConfig' import { isAppsPageSender } from '../../apps/appsPages' +import { findInPageBuild, locateOutputFiles } from '../../apps/outputFiles' import { readOutputAction, readOutputRequest } from '../../apps/outputRequest' +import { revealOutputFile } from '../outputFolders' export const APPS_OUTPUTS_SHOW_CHANNEL = 'apps-outputs:show' export const APPS_OUTPUT_ACTION_CHANNEL = 'apps:output-action' +const latestRequest = new WeakMap() + async function isFromAppsPage(event: IpcMainInvokeEvent): Promise { return isAppsPageSender(event.sender, event.senderFrame) && (await areAppsEnabled()) } +function takeTurn(page: WebContents): () => boolean { + const turn = (latestRequest.get(page) ?? 0) + 1 + latestRequest.set(page, turn) + return () => latestRequest.get(page) === turn +} + export async function showOutputFromAppsPage( event: IpcMainInvokeEvent, request: unknown @@ -21,12 +36,19 @@ export async function showOutputFromAppsPage( if (sentFrom === null || !(await isFromAppsPage(event))) return false const outputs = readOutputRequest(request) if (!outputs) throw new Error('Desktop cannot show these outputs.') - return showAppOutputs(event.sender, sentFrom, outputs) + const isLatest = takeTurn(event.sender) + const located = await locateOutputFiles(outputs, await findInPageBuild(event.sender)) + if (!isLatest()) return false + return showAppOutputs(event.sender, sentFrom, located.outputs, located.files) } export function takeOutputAction(event: IpcMainEvent, raw: unknown): void { const action = readOutputAction(raw) - if (action) sendAppOutputAction(event.sender, action) + if (!action) return + const file = + action.action === 'show-in-folder' ? shownOutputFile(event.sender, action.index) : null + if (file) void revealOutputFile(file).catch(() => false) + else sendAppOutputAction(event.sender, action) } export function registerAppsOutputsHandlers(): void { diff --git a/src/main/lib/outputFolders.test.ts b/src/main/lib/outputFolders.test.ts index c719b996c..ee084d01b 100644 --- a/src/main/lib/outputFolders.test.ts +++ b/src/main/lib/outputFolders.test.ts @@ -84,6 +84,7 @@ import type { InstallationRecord } from '../installations' import { MAX_LOCAL_ASSETS, THUMBNAIL_WAIT_MS, + comfyOutputPath, copyOutputImage, hasOutputFolder, revealOutputFile, @@ -331,6 +332,106 @@ describe('outputFilePath', () => { }) }) +describe('comfyOutputPath', () => { + const CUTOUT = Buffer.from('cutout bytes') + const appsBuild = (): InstallationRecord => + install('Apps', { useSharedOutput: false, outputDir: ownDir }) + const cutout = { filename: 'cutout_00001_.png', subfolder: 'apps/remove-background' } + + it("finds the file ComfyUI named in the build's own output folder, or in the shared one", async () => { + const own = path.join(ownDir, 'apps', 'remove-background', 'cutout_00001_.png') + const shared = path.join(sharedDir, 'cutout_00001_.png') + write(own, CUTOUT) + write(shared, CUTOUT) + + expect(await comfyOutputPath(appsBuild(), cutout, new Uint8Array(CUTOUT))).toBe(own) + expect( + await comfyOutputPath( + install('Test Instance'), + { filename: 'cutout_00001_.png', subfolder: '' }, + new Uint8Array(CUTOUT) + ) + ).toBe(shared) + }) + + it('finds nothing for a missing file, or a file without exactly the bytes the page shows', async () => { + write(path.join(ownDir, 'apps', 'remove-background', 'cutout_00001_.png'), CUTOUT) + + expect( + await comfyOutputPath( + appsBuild(), + { ...cutout, filename: 'gone.png' }, + new Uint8Array(CUTOUT) + ) + ).toBeNull() + expect( + await comfyOutputPath(appsBuild(), cutout, new Uint8Array(Buffer.from('cutout bytez'))), + 'same size, other bytes' + ).toBeNull() + expect( + await comfyOutputPath(appsBuild(), cutout, new Uint8Array(Buffer.from('cutout'))), + 'other size' + ).toBeNull() + }) + + it("refuses links out of the build's output folder, even into another build's", async () => { + write(path.join(outsideDir, 'secret.png'), CUTOUT) + write(path.join(sharedDir, 'shared.png'), CUTOUT) + fs.symlinkSync(path.join(outsideDir, 'secret.png'), path.join(ownDir, 'secret.png')) + fs.symlinkSync(path.join(sharedDir, 'shared.png'), path.join(ownDir, 'shared.png')) + const bytes = new Uint8Array(CUTOUT) + + expect( + await comfyOutputPath(appsBuild(), { filename: 'secret.png', subfolder: '' }, bytes) + ).toBeNull() + expect( + await comfyOutputPath(appsBuild(), { filename: 'shared.png', subfolder: '' }, bytes) + ).toBeNull() + expect( + await comfyOutputPath( + appsBuild(), + { filename: 'shared.png', subfolder: '../shared-output' }, + bytes + ) + ).toBeNull() + }) + + it("refuses a subfolder that climbs out of the build's output folder, and an absolute filename", async () => { + const secret = path.join(outsideDir, 'secret.png') + write(secret, CUTOUT) + const bytes = new Uint8Array(CUTOUT) + + expect( + await comfyOutputPath(appsBuild(), { filename: 'secret.png', subfolder: '../outside' }, bytes) + ).toBeNull() + expect( + await comfyOutputPath( + appsBuild(), + { filename: 'secret.png', subfolder: 'apps/../../outside' }, + bytes + ) + ).toBeNull() + expect( + await comfyOutputPath(appsBuild(), { filename: secret, subfolder: '' }, bytes) + ).toBeNull() + expect( + await comfyOutputPath(appsBuild(), { filename: '/etc/passwd', subfolder: '' }, bytes) + ).toBeNull() + }) + + it('finds nothing for Cloud or a build that manages its own paths', async () => { + write(path.join(sharedDir, 'cutout_00001_.png'), CUTOUT) + const file = { filename: 'cutout_00001_.png', subfolder: '' } + + expect( + await comfyOutputPath(install('Cloud', { sourceId: 'cloud' }), file, new Uint8Array(CUTOUT)) + ).toBeNull() + expect( + await comfyOutputPath(install('Old', { sourceId: 'desktop' }), file, new Uint8Array(CUTOUT)) + ).toBeNull() + }) +}) + 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')) diff --git a/src/main/lib/outputFolders.ts b/src/main/lib/outputFolders.ts index f9d7e3f2d..b008b0c4a 100644 --- a/src/main/lib/outputFolders.ts +++ b/src/main/lib/outputFolders.ts @@ -1,3 +1,4 @@ +import fs from 'fs' import fsp from 'fs/promises' import path from 'path' import { pathToFileURL } from 'url' @@ -5,6 +6,7 @@ import { ipcMain, nativeImage, net, protocol, shell } from 'electron' import * as installations from '../installations' import type { InstallationRecord } from '../installations' import * as settings from '../settings' +import type { ComfyOutputFile } from '../apps/outputRequest' import { assetKind } from './assets' import { copyImageToClipboard } from './clipboardImage' import { mediaFactsFromFile } from './mediaFacts' @@ -31,6 +33,8 @@ 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 READ_WITHOUT_FOLLOWING = + fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0) | (fs.constants.O_NONBLOCK ?? 0) const thumbnails = new Map() const thumbnailsInFlight = new Map>() @@ -104,6 +108,36 @@ export async function outputFilePath(target: string): Promise { return stat?.isFile() ? file : null } +async function hasSameBytes(file: string, bytes: Uint8Array): Promise { + const stat = await fsp.stat(file).catch(() => null) + if (!stat?.isFile() || stat.size !== bytes.byteLength) return false + const handle = await fsp.open(file, READ_WITHOUT_FOLLOWING).catch(() => null) + if (!handle) return false + try { + const content = Buffer.alloc(bytes.byteLength + 1) + const { bytesRead } = await handle.read(content, 0, content.length, 0) + const expected = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength) + return bytesRead === bytes.byteLength && expected.equals(content.subarray(0, bytesRead)) + } catch { + return false + } finally { + await handle.close().catch(() => {}) + } +} + +export async function comfyOutputPath( + inst: InstallationRecord, + file: ComfyOutputFile, + bytes: Uint8Array +): Promise { + const dir = outputDirFor(inst, sharedOutputDir()) + const root = dir ? await realDir(dir) : null + if (!root) return null + const real = await allowedPath(path.join(root, file.subfolder, file.filename)) + if (!real || !isInsideRoot(real, root)) return null + return (await hasSameBytes(real, bytes)) ? real : null +} + export async function revealOutputFile(target: string): Promise { const file = await outputFilePath(target) if (!file) return false diff --git a/src/renderer/src/panel/AppPageHost.test.ts b/src/renderer/src/panel/AppPageHost.test.ts index f42d43dae..00fa24dcc 100644 --- a/src/renderer/src/panel/AppPageHost.test.ts +++ b/src/renderer/src/panel/AppPageHost.test.ts @@ -369,6 +369,61 @@ describe('AppPageHost outputs', () => { expect(outputDialog()).toBeNull() }) + it('says where a result on this computer is saved, and sends Show in folder to the page', async () => { + await mountHost() + const [first, second] = TWO_CUTOUTS.items + await showOutputs('remove-background', { + items: [first!, { ...second!, savedOn: 'computer', folder: '/Users/me/ComfyUI/output/apps' }], + index: 1 + }) + + expect(inOutputDialog('output-detail-saved-in')?.textContent).toContain('apps') + expect(inOutputDialog('output-detail-saved-in')?.textContent).toContain( + '/Users/me/ComfyUI/output' + ) + expect(inOutputDialog('output-detail-view-in-assets')).toBeNull() + + inOutputDialog('output-detail-show-in-folder')!.click() + await flushPromises() + + expect(sendAppOutputAction).toHaveBeenCalledExactlyOnceWith({ + action: 'show-in-folder', + index: 1 + }) + expect(outputDialog()).not.toBeNull() + }) + + it('shows no saved place for a result on this computer whose folder Desktop does not know', async () => { + await mountHost() + const [first, second] = TWO_CUTOUTS.items + await showOutputs('remove-background', { + items: [first!, { ...second!, savedOn: 'computer' }], + index: 1 + }) + + expect(inOutputDialog('output-detail-saved-in')).toBeNull() + expect(inOutputDialog('output-detail-show-in-folder')).toBeNull() + }) + + it('says a Cloud result is saved in Comfy Cloud assets, and View in Assets asks for it by name', async () => { + const wrapper = await mountHost() + const [first, second] = TWO_CUTOUTS.items + await showOutputs('remove-background', { + items: [first!, { ...second!, savedOn: 'cloud' }], + index: 1 + }) + + expect(inOutputDialog('output-detail-saved-in')?.textContent).toContain('Comfy Cloud assets') + expect(inOutputDialog('output-detail-show-in-folder')).toBeNull() + + inOutputDialog('output-detail-view-in-assets')!.click() + await flushPromises() + + expect(wrapper.emitted('view-in-assets')).toEqual([['cutout-2.png']]) + expect(sendAppOutputAction).not.toHaveBeenCalled() + expect(outputDialog()).toBeNull() + }) + it('brings the app back with the keyboard once the dialog closes, and lets go of the files', async () => { const revoke = vi.spyOn(URL, 'revokeObjectURL') await mountHost() diff --git a/src/renderer/src/panel/AppPageHost.vue b/src/renderer/src/panel/AppPageHost.vue index 42eb0917d..1de40478a 100644 --- a/src/renderer/src/panel/AppPageHost.vue +++ b/src/renderer/src/panel/AppPageHost.vue @@ -21,7 +21,7 @@ const { appId, title } = defineProps<{ title: string }>() -const emit = defineEmits<{ close: [] }>() +const emit = defineEmits<{ close: []; 'view-in-assets': [name: string] }>() const { t } = useI18n() @@ -114,7 +114,9 @@ function showOutputs(shown: AppOutputs): void { tags: output.tags, previewUrl: URL.createObjectURL(new Blob([output.bytes], { type: output.mimeType })), thumbnailUrl: null, - workflow: output.workflow + workflow: output.workflow, + savedIn: output.savedOn === 'computer' ? (output.folder ?? null) : null, + inCloud: output.savedOn === 'cloud' })) outputIndex.value = shown.index outputsShown.value += 1 @@ -131,6 +133,12 @@ function sendOutputAction(action: AppOutputAction['action'], index: number): voi if (action === 'open-workflow') closeOutputs() } +function viewInAssets(index: number): void { + const name = outputs.value?.[index]?.name + releaseOutputs() + if (name) emit('view-in-assets', name) +} + function retry(): void { status.value = 'loading' retrying = true @@ -254,6 +262,8 @@ onBeforeUnmount(() => { @close="closeOutputs" @download="sendOutputAction('download', $event)" @open-workflow="sendOutputAction('open-workflow', $event)" + @show-in-folder="sendOutputAction('show-in-folder', $event)" + @view-in-assets="viewInAssets" /> diff --git a/src/renderer/src/panel/AppsView.test.ts b/src/renderer/src/panel/AppsView.test.ts index c24cd244e..90c5ac4e7 100644 --- a/src/renderer/src/panel/AppsView.test.ts +++ b/src/renderer/src/panel/AppsView.test.ts @@ -4,6 +4,7 @@ import { flushPromises, mount } from '@vue/test-utils' import '../assets/main.css' import AppsView from './AppsView.vue' +import AppPageHost from './AppPageHost.vue' import type { AppCard, AppPagePlacement, AppPageStatus } from '../types/ipc' const FOUR_COLUMN_PAGE_WIDTH = 1172 @@ -233,6 +234,16 @@ describe('AppsView', () => { expect(hideAppPage).toHaveBeenCalled() }) + it("passes on the Cloud file an app's output dialog asks to see in Assets", async () => { + const wrapper = await mountApps() + await wrapper.get('[data-testid="app-card-remove-background"]').trigger('click') + await flushPromises() + + wrapper.findComponent(AppPageHost).vm.$emit('view-in-assets', 'cutout.png') + + expect(wrapper.emitted('view-in-assets')).toEqual([['cutout.png']]) + }) + it('returns to the cards when Apps is picked in the sidebar again', async () => { const wrapper = await mountApps() await wrapper.get('[data-testid="app-card-genjutsu"]').trigger('click') diff --git a/src/renderer/src/panel/AppsView.vue b/src/renderer/src/panel/AppsView.vue index adf7996a3..ee265addd 100644 --- a/src/renderer/src/panel/AppsView.vue +++ b/src/renderer/src/panel/AppsView.vue @@ -17,7 +17,10 @@ const { revisit = 0 } = defineProps<{ revisit?: number }>() const startWith = defineModel('startWith', { default: null }) -const emit = defineEmits<{ 'app-open-change': [open: boolean] }>() +const emit = defineEmits<{ + 'app-open-change': [open: boolean] + 'view-in-assets': [name: string] +}>() const { t } = useI18n() @@ -222,7 +225,12 @@ onBeforeUnmount(() => {
- +
diff --git a/src/renderer/src/panel/AssetsView.test.ts b/src/renderer/src/panel/AssetsView.test.ts index 141d55d2a..ef4a8bc83 100644 --- a/src/renderer/src/panel/AssetsView.test.ts +++ b/src/renderer/src/panel/AssetsView.test.ts @@ -851,6 +851,112 @@ describe('AssetsView', () => { } }) + describe('a file an app asks to see', () => { + async function mountWithFocus(files: AssetSummary[]) { + window.localStorage.setItem('comfy.desktop.assets.place', 'computer') + getAuthStatus.mockResolvedValue({ signedIn: true }) + listAssets.mockResolvedValue(files) + const installationStore = useInstallationStore() + installationStore.installations = [cloudBuild('cloud')] + installationStore.loading = false + const wrapper = mount(AssetsView, { attachTo: page, props: { focusName: 'cutout.png' } }) + await flushPromises() + return wrapper + } + + it('opens on Comfy Cloud with the newest file of that name open, once, and keeps the saved place', async () => { + const wrapper = await mountWithFocus([ + asset('old', 'cutout.png', 'image', { createdAt: '2026-11-01T00:00:00Z' }), + asset('new', 'cutout.png', 'image', { createdAt: '2026-11-02T00:00:00Z' }), + asset('other', 'other.png', 'image', { createdAt: '2026-11-03T00:00:00Z' }) + ]) + + expect(wrapper.emitted('focus-used')).toHaveLength(1) + expect(listAssets).toHaveBeenCalledWith('cloud') + expect(listOutputFolders).not.toHaveBeenCalled() + expect(viewerTitle()).toBe('cutout.png') + expect( + viewer()?.querySelector('[aria-current="true"]')?.getAttribute('data-testid'), + 'the newer of the two cutouts, after the newest file overall' + ).toBe('output-detail-thumb-1') + expect(window.localStorage.getItem('comfy.desktop.assets.place')).toBe('computer') + }) + + it('says so on Comfy Cloud when the file is not there, and drops the note once the user picks a place', async () => { + const wrapper = await mountWithFocus([asset('other', 'other.png', 'image')]) + + expect(wrapper.emitted('focus-used')).toHaveLength(1) + expect(assetNames(wrapper)).toEqual(['other.png']) + expect(viewer()).toBeNull() + expect(text(wrapper, 'assets-not-found')).toBe( + "Couldn't find cutout.png in your Comfy Cloud assets. It may have a different name here." + ) + + await choose(wrapper, 'computer') + await choose(wrapper, 'cloud') + + expect(wrapper.find('[data-testid="assets-not-found"]').exists()).toBe(false) + }) + + it('shows no note when the file opened', async () => { + await mountWithFocus([asset('new', 'cutout.png', 'image')]) + expect(viewerTitle()).toBe('cutout.png') + expect(document.querySelector('[data-testid="assets-not-found"]')).toBeNull() + }) + + it('shows the load error instead of a note when the Cloud list fails', async () => { + listAssets.mockRejectedValue(new Error('offline')) + window.localStorage.setItem('comfy.desktop.assets.place', 'computer') + getAuthStatus.mockResolvedValue({ signedIn: true }) + const installationStore = useInstallationStore() + installationStore.installations = [cloudBuild('cloud')] + installationStore.loading = false + const wrapper = mount(AssetsView, { attachTo: page, props: { focusName: 'cutout.png' } }) + await flushPromises() + + expect(wrapper.get('[data-testid="assets-empty"] h2').text()).toBe("Couldn't load assets") + expect(wrapper.find('[data-testid="assets-not-found"]').exists()).toBe(false) + }) + + it('lets go of the file when Comfy Cloud cannot list it, and says why', async () => { + getAuthStatus.mockResolvedValue({ signedIn: false }) + listAssets.mockResolvedValue([asset('new', 'cutout.png', 'image')]) + const installationStore = useInstallationStore() + installationStore.installations = [cloudBuild('cloud')] + installationStore.loading = false + const signedOut = mount(AssetsView, { attachTo: page, props: { focusName: 'cutout.png' } }) + await flushPromises() + + expect(signedOut.get('[data-testid="assets-empty"] h2').text()).toBe( + 'Log in to see your assets' + ) + await signInWith([asset('new', 'cutout.png', 'image')]) + expect(assetNames(signedOut)).toEqual(['cutout.png']) + expect(viewer(), 'the file is not opened later').toBeNull() + signedOut.unmount() + + installationStore.installations = [localBuild('lab')] + const noCloudBuild = mount(AssetsView, { attachTo: page, props: { focusName: 'cutout.png' } }) + await flushPromises() + expect(noCloudBuild.get('[data-testid="assets-empty"] h2').text()).toBe( + 'This workspace has no Comfy Cloud build yet' + ) + installationStore.installations = [cloudBuild('cloud')] + await flushPromises() + expect(assetNames(noCloudBuild)).toEqual(['cutout.png']) + expect(viewer(), 'the file is not opened later').toBeNull() + }) + + it('remembers a place the user picks after that', async () => { + const wrapper = await mountWithFocus([]) + + await choose(wrapper, 'computer') + await choose(wrapper, 'cloud') + + expect(window.localStorage.getItem('comfy.desktop.assets.place')).toBe('cloud') + }) + }) + it('lists the Cloud build of the workspace the user moves to', async () => { listAssets.mockResolvedValue([asset('t1', 'team.png', 'image')]) const wrapper = await mountCloud([ diff --git a/src/renderer/src/panel/AssetsView.vue b/src/renderer/src/panel/AssetsView.vue index ba14da2ef..6ad91c6c8 100644 --- a/src/renderer/src/panel/AssetsView.vue +++ b/src/renderer/src/panel/AssetsView.vue @@ -101,7 +101,12 @@ const PLACEHOLDER_TINT = { file: 'bg-[linear-gradient(168deg,rgb(200_211_222/0.09),rgb(255_255_255/0.02))] text-[rgb(160_166_175)]' } as const -const { installationId = '' } = defineProps<{ installationId?: string }>() +const { installationId = '', focusName = null } = defineProps<{ + installationId?: string + focusName?: string | null +}>() + +const emit = defineEmits<{ 'focus-used': [] }>() const { t } = useI18n() const modal = useModal() @@ -124,7 +129,9 @@ const sortOrder = useLocalStorage('comfy.desktop.assets.sort', 'newes const showName = useLocalStorage('comfy.desktop.assets.showName', true) const showDetails = useLocalStorage('comfy.desktop.assets.showDetails', true) const savedPlace = useLocalStorage('comfy.desktop.assets.place', 'computer') -const pickedPlace = ref(null) +const pickedPlace = ref(focusName ? 'cloud' : null) +const pendingFocus = ref(null) +const notFoundName = ref(null) let windowFolderOpened = false const trail = ref([]) const outputFolders = ref(null) @@ -164,6 +171,16 @@ const cloudBuild = computed( ) ?? null ) +const cloudListable = computed( + () => buildsKnown.value && authStore.isSignedIn && cloudBuild.value !== null +) +const cloudSettled = computed( + () => + buildsKnown.value && + authStore.statusLoaded && + (!authStore.isSignedIn || authStore.workspacesLoaded) +) + const currentFolder = computed(() => place.value === 'computer' ? (trail.value.at(-1) ?? null) : null ) @@ -176,6 +193,7 @@ const placeOptions = computed(() => [ function choosePlace(next: Place): void { pickedPlace.value = next + notFoundName.value = null savedPlace.value = next } @@ -406,6 +424,7 @@ async function loadPlace(userAsked: boolean): Promise { async function loadAssets(userAsked = false): Promise { const current = ++requestId + const settled = cloudSettled.value failed.value = false loading.value = true try { @@ -414,11 +433,13 @@ async function loadAssets(userAsked = false): Promise { assets.value = loaded.assets outputFolders.value = loaded.folders subfolders.value = loaded.subfolders + openPendingFocus(settled) } catch { if (current !== requestId) return assets.value = [] subfolders.value = [] failed.value = true + openPendingFocus(settled) } finally { if (current === requestId) loading.value = false } @@ -457,6 +478,46 @@ function refresh(): void { const detailKey = ref(null) +function newestNamed(name: string): GalleryAsset | null { + return visibleAssets.value + .filter((asset) => asset.name === name) + .reduce( + (newest, asset) => (newest && createdMs(newest) >= createdMs(asset) ? newest : asset), + null + ) +} + +function openPendingFocus(settled: boolean): void { + const name = pendingFocus.value + if (!name || (place.value === 'cloud' && !cloudListable.value && !settled)) return + pendingFocus.value = null + if (place.value !== 'cloud' || failed.value || !cloudListable.value) return + const asset = newestNamed(name) + if (asset) openDetail(asset) + else notFoundName.value = name +} + +watch( + cloudSettled, + (settled) => { + if (settled && !loading.value) openPendingFocus(true) + }, + { flush: 'post' } +) + +watch( + () => focusName, + (name) => { + if (!name) return + emit('focus-used') + const cloudShown = place.value === 'cloud' && !loading.value + pendingFocus.value = name + pickedPlace.value = 'cloud' + if (cloudShown) openPendingFocus(cloudSettled.value) + }, + { immediate: true } +) + const detailIndex = computed({ get: () => { if (detailKey.value === null) return null @@ -855,6 +916,14 @@ function extensionOf(name: string): string { />
+

+ {{ t('assets.notFoundInCloud', { name: notFoundName }) }} +

    () const { t, locale } = useI18n() @@ -291,11 +294,22 @@ useEventListener(window, 'keydown', (event: KeyboardEvent) => {
-
+
{{ t('assets.detail.savedIn') }}
-
+ + {{ t('assets.detail.cloudAssets') }} + +
{{ savedFolder.name }} {{ savedFolder.parent @@ -339,6 +353,18 @@ useEventListener(window, 'keydown', (event: KeyboardEvent) => {
diff --git a/src/renderer/src/panel/PanelApp.test.ts b/src/renderer/src/panel/PanelApp.test.ts index 4f0d51894..918e60419 100644 --- a/src/renderer/src/panel/PanelApp.test.ts +++ b/src/renderer/src/panel/PanelApp.test.ts @@ -63,7 +63,12 @@ vi.mock('../views/ChooserView.vue', () => ({ } })) vi.mock('./AssetsView.vue', () => ({ - default: { name: 'AssetsView', template: '
' } + default: { + name: 'AssetsView', + props: ['focusName'], + emits: ['focus-used'], + template: '
' + } })) vi.mock('./BuildDetailsView.vue', () => ({ default: { @@ -149,6 +154,7 @@ vi.mock('../views/MigrateConfirmTakeover.vue', () => ({ import { mount, flushPromises } from '@vue/test-utils' import { createPinia, setActivePinia } from 'pinia' import PanelApp from './PanelApp.vue' +import AppsView from './AppsView.vue' import { en } from '../lib/i18nMessages' import { __resetLauncherPrefsForTest } from '../composables/useLauncherPrefs' import { useOverlay } from '../composables/useOverlay' @@ -1598,6 +1604,31 @@ describe('PanelApp', () => { ]) }) + it('opens Assets on the Cloud file an app asks to see, and passes the file on only once', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) + const wrapper = mountPanel() + await flushPromises() + await wrapper.get('[data-testid="app-sidebar-apps"]').trigger('click') + await flushPromises() + + wrapper.findComponent(AppsView).vm.$emit('view-in-assets', 'cutout.png') + await flushPromises() + + expect(wrapper.find('[data-testid="apps-view"]').exists()).toBe(false) + expect(wrapper.get('[data-testid="app-sidebar-assets"]').attributes('aria-current')).toBe( + 'page' + ) + expect(wrapper.get('[data-testid="assets-view"]').attributes('data-focus-name')).toBe( + 'cutout.png' + ) + + wrapper.findComponent({ name: 'AssetsView' }).vm.$emit('focus-used') + await flushPromises() + + expect(wrapper.get('[data-testid="assets-view"]').attributes('data-focus-name')).toBeUndefined() + }) + it('does not record a sidebar page open for See all apps on Home', async () => { window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) diff --git a/src/renderer/src/panel/PanelApp.vue b/src/renderer/src/panel/PanelApp.vue index 19aba253c..c8cff699f 100644 --- a/src/renderer/src/panel/PanelApp.vue +++ b/src/renderer/src/panel/PanelApp.vue @@ -210,6 +210,7 @@ async function showHome(): Promise { const appsVisits = ref(0) const appIsOpen = ref(false) +const assetsFocusName = ref(null) const appPickedOnHome = ref(null) function openAppFromHome(app: AppCard): void { @@ -227,8 +228,14 @@ function handleSidebarClick(page: DashboardPage): void { handleSidebarNavigate(page) } +function viewInAssets(name: string): void { + handleSidebarNavigate('assets') + assetsFocusName.value = name +} + function handleSidebarNavigate(page: DashboardPage): void { closeBuildDetails() + assetsFocusName.value = null if (page === 'apps') appsVisits.value += 1 if (page === 'home' || page === 'apps' || page === 'chooser' || page === 'assets') dashboardHomePage.value = page @@ -528,10 +535,13 @@ onUnmounted(() => { v-model:start-with="appPickedOnHome" :revisit="appsVisits" @app-open-change="appIsOpen = $event" + @view-in-assets="viewInAssets" /> ): void } +/** + * A file ComfyUI saved in its output folder, named as a run's history names + * it. `filename` has no folder in it, and `subfolder` is relative, with no + * `.` or `..` parts. Only `type: 'output'` is kept. + */ +export interface ComfyDesktopOutputFile { + filename: string + subfolder?: string + type: 'output' +} + /** * One output a hosted page asks Desktop to show. `mimeType` is one of * `image/png`, `image/jpeg`, `image/webp`, `image/gif`, `image/avif`, @@ -339,6 +350,11 @@ export interface ComfyDesktopHostedTelemetryBridge { * `workflow` is true. Desktop refuses a `name`, and drops a tag, that has * control, bidirectional or zero-width formatting characters in the part it * keeps. + * + * `savedOn` says where the output is saved. For `cloud`, Desktop shows + * "Saved in Comfy Cloud assets" and View in Assets, which opens Desktop's + * Assets page on Comfy Cloud with the newest file of that `name` open. For + * `computer`, Desktop shows the folder it is saved in and Show in folder. */ export interface ComfyDesktopOutput { name: string @@ -346,6 +362,20 @@ export interface ComfyDesktopOutput { bytes: Uint8Array tags?: string[] workflow?: boolean + savedOn?: 'cloud' | 'computer' + /** + * For `computer` without `comfyFile`: the absolute path of the folder the + * page saved the output in. Desktop only shows it, and Show in folder comes + * back to the page on `onOutputAction`. + */ + folder?: string + /** + * For `computer`: the file ComfyUI saved for this output. Desktop looks for + * it in the output folder of the build the page runs on, and uses it only + * when it has exactly the output's `bytes`. Desktop then shows its folder + * and shows the file in its folder itself; otherwise it shows no folder. + */ + comfyFile?: ComfyDesktopOutputFile } /** At most 40 outputs and 100 MB of bytes in all, and the index of the one to show first. */ @@ -356,10 +386,12 @@ export interface ComfyDesktopShowOutputRequest { /** * What the person picked in Desktop's output dialog for the output at `index` - * of the page's last `showOutput` request: download it, or open its workflow. + * of the page's last `showOutput` request: download it, open its workflow, or + * show it in its `folder`. Desktop does not send `show-in-folder` for an + * output whose `comfyFile` it found, because it shows that file itself. */ export interface ComfyDesktopOutputAction { - action: 'download' | 'open-workflow' + action: 'download' | 'open-workflow' | 'show-in-folder' index: number } @@ -367,12 +399,13 @@ export interface ComfyDesktopOutputAction { * Desktop's output dialog, for a Comfy page hosted in Desktop's Apps view. * Desktop exposes it to that page as `window.comfyDesktopOutputs`. Desktop * shows the outputs over the Apps view and does not save or open anything - * itself: Download and Open workflow come back to the page on + * itself: Download, Open workflow and Show in folder come back to the page on * `onOutputAction`, only while the page that showed the outputs is still the - * app's loaded page. + * app's loaded page. Desktop itself only shows a found `comfyFile` in its + * folder, and opens the Assets page for View in Assets. */ export interface ComfyDesktopHostedOutputsBridge { - /** Resolves true once Desktop shows the outputs, and false when the app is not on screen. Rejects a request Desktop cannot show. */ + /** Resolves true once Desktop shows the outputs, and false when the app is not on screen or a newer request from the page replaced this one. Rejects a request Desktop cannot show. */ showOutput(request: ComfyDesktopShowOutputRequest): Promise onOutputAction(callback: (action: ComfyDesktopOutputAction) => void): () => void } diff --git a/src/types/ipc.ts b/src/types/ipc.ts index cf844f364..8513fe45c 100644 --- a/src/types/ipc.ts +++ b/src/types/ipc.ts @@ -157,6 +157,8 @@ export interface AppOutput { bytes: Uint8Array tags: string[] workflow: boolean + savedOn?: 'cloud' | 'computer' + folder?: string } export interface AppOutputs { @@ -165,7 +167,7 @@ export interface AppOutputs { } export interface AppOutputAction { - action: 'download' | 'open-workflow' + action: 'download' | 'open-workflow' | 'show-in-folder' index: number }