Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
"showInFinder": "在 Finder 中显示",
"showInExplorer": "在文件资源管理器中显示",
"showInFolder": "在文件夹中显示",
"viewInAssets": "在资产中查看",
"notFoundInCloud": "在你的 Comfy Cloud 资产中找不到 {name}。它在这里的名称可能不同。",
"copy": "复制",
"copyFailed": "无法复制资产",
"delete": "删除",
Expand All @@ -121,7 +123,8 @@
"zoom": "缩放",
"zoomLevel": "缩放:{level}",
"openWorkflow": "打开工作流",
"savedIn": "保存位置"
"savedIn": "保存位置",
"cloudAssets": "Comfy Cloud 资产"
},
"kindSingular": {
"image": "图片",
Expand Down
29 changes: 29 additions & 0 deletions src/main/apps/appPageView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
loadedAppPage,
sendAppOutputAction,
showAppOutputs,
shownOutputFile,
showAppPage
} from './appPageView'
import { trackFromAppsPage } from '../lib/ipc/registerAppsTelemetryHandlers'
Expand Down Expand Up @@ -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()

Expand Down
21 changes: 16 additions & 5 deletions src/main/apps/appPageView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface LoadedAppPage {
interface ShownOutputs {
page: LoadedAppPage
count: number
files: readonly (string | null)[]
}

interface HostedPage {
Expand Down Expand Up @@ -437,26 +438,36 @@ 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
}
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium — shownOutputFile indexes shown.files[index] without the index >= shown.count bound that sendAppOutputAction applies two lines below. showAppOutputs takes files as an independent parameter defaulting to [], so nothing guarantees files.length === count; a longer array would let a panel click reveal a file outside the shown set. Apply the same bound check in shownOutputFile. Raised by 1 of 8 reviewers (claude-opus-5-thinking-max adversarial).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing: files is built from the same items that set count, so its length always equals count.

}

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 {
Expand Down
117 changes: 117 additions & 0 deletions src/main/apps/outputFiles.test.ts
Original file line number Diff line number Diff line change
@@ -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> = {}): 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<FindOutputFile>(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<FindOutputFile>().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<FindOutputFile>()

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<FindOutputFile>(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()
})
})
41 changes: 41 additions & 0 deletions src/main/apps/outputFiles.ts
Original file line number Diff line number Diff line change
@@ -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<string | null>

export interface LocatedOutputs {
outputs: AppOutputs
files: (string | null)[]
}

export async function locateOutputFiles(
request: OutputRequest,
find: FindOutputFile
): Promise<LocatedOutputs> {
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<FindOutputFile> {
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)
}
74 changes: 71 additions & 3 deletions src/main/apps/outputRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand All @@ -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<string, unknown>) => ({
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: [
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading