Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/components/sidebar/tabs/AssetsSidebarTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ import { getOutputAssetMetadata } from '@/platform/assets/schemas/assetMetadataS
import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
import { getAssetDisplayName } from '@/platform/assets/utils/assetMetadataUtils'
import {
getAssetSubfolder,
getAssetUrl
getAssetFileUrl,
getAssetSubfolder
} from '@/platform/assets/utils/assetUrlUtil'
import type { MediaKind } from '@/platform/assets/schemas/mediaAssetSchema'
import { resolveOutputAssetItems } from '@/platform/assets/utils/outputAssetUtil'
Expand Down Expand Up @@ -572,7 +572,7 @@ const handleZoomClick = (asset: AssetItem) => {
title: getAssetDisplayName(asset),
component: Load3dViewerContent,
props: {
modelUrl: asset.preview_url || getAssetUrl(asset)
modelUrl: getAssetFileUrl(asset)
},
dialogComponentProps: {
renderer: 'reka',
Expand Down
47 changes: 47 additions & 0 deletions src/composables/useLoad3d.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,53 @@ describe('useLoad3d', () => {
)
})

it('falls back to the last loaded model file when the node has no model widget', async () => {
const { isAssetPreviewSupported, persistThumbnail } =
await import('@/platform/assets/utils/assetPreviewUtil')
vi.mocked(isAssetPreviewSupported).mockReturnValue(true)
vi.mocked(Load3dUtils.splitFilePath).mockReturnValue([
'3d',
'ComfyUI_00110.glb'
] as unknown as ReturnType<typeof Load3dUtils.splitFilePath>)
mockNode.widgets = [
{ name: 'viewport_state', value: {} } as unknown as IWidget
]
mockNode.properties['Last Time Model File'] = '3d/ComfyUI_00110.glb'

const { handler } = await getModelReadyHandler()
handler()
await new Promise((r) => setTimeout(r, 0))

expect(Load3dUtils.splitFilePath).toHaveBeenCalledWith(
'3d/ComfyUI_00110.glb'
)
expect(persistThumbnail).toHaveBeenCalledWith(
'ComfyUI_00110.glb',
expect.any(Blob)
)
})

it('falls back to the last loaded model file when the model widget value is empty', async () => {
const { isAssetPreviewSupported, persistThumbnail } =
await import('@/platform/assets/utils/assetPreviewUtil')
vi.mocked(isAssetPreviewSupported).mockReturnValue(true)
vi.mocked(Load3dUtils.splitFilePath).mockReturnValue([
'3d',
'ComfyUI_00110.glb'
] as unknown as ReturnType<typeof Load3dUtils.splitFilePath>)
mockNode.widgets = [{ name: 'image', value: '' } as unknown as IWidget]
Comment thread
jtydhr88 marked this conversation as resolved.
Resolved
mockNode.properties['Last Time Model File'] = '3d/ComfyUI_00110.glb'

const { handler } = await getModelReadyHandler()
handler()
await new Promise((r) => setTimeout(r, 0))

expect(persistThumbnail).toHaveBeenCalledWith(
'ComfyUI_00110.glb',
expect.any(Blob)
)
})

it('skips persistence when the model widget has no value', async () => {
const { isAssetPreviewSupported, persistThumbnail } =
await import('@/platform/assets/utils/assetPreviewUtil')
Expand Down
6 changes: 5 additions & 1 deletion src/composables/useLoad3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,11 @@ export const useLoad3d = (nodeOrRef: MaybeRef<LGraphNode | null>) => {
const modelWidget = node?.widgets?.find(
(w) => w.name === 'model_file' || w.name === 'image'
)
const value = modelWidget?.value
const widgetValue = modelWidget?.value
const value =
typeof widgetValue === 'string' && widgetValue.trim()
? widgetValue
: node?.properties['Last Time Model File']
if (typeof value !== 'string' || !value) return

const filename = value.trim().replace(/\s*\[output\]$/, '')
Expand Down
24 changes: 24 additions & 0 deletions src/platform/assets/composables/media/assetMappers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,28 @@ describe('unflattenOutputAssets', () => {
'output'
])
})

it('keeps the representative asset id apart from same-named outputs', () => {
const asset = {
job_id: 'job-id',
name: 'ComfyUI_00001.glb',
size: 1,
tags: ['output'],
updated_at: '2026-01-01T00:00:00Z'
}
const assets = [
{ ...asset, id: 'earlier-id', created_at: '2026-01-01T00:00:00Z' },
{ ...asset, id: 'later-id', created_at: '2026-01-01T00:00:01Z' }
] satisfies AssetItem[]

const [grouped] = unflattenOutputAssets(assets)
const metadata = getOutputAssetMetadata(grouped.user_metadata)

expect(grouped.id).toBe('job-id')
expect(metadata?.assetId).toBe('later-id')
expect(metadata?.allOutputs?.map((output) => output.assetId)).toEqual([
'earlier-id',
'later-id'
])
})
})
1 change: 1 addition & 0 deletions src/platform/assets/composables/media/assetMappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export function unflattenOutputAssets(
jobId: job_id,
subfolder: '',
...representative.user_metadata,
assetId: representative.id,
outputCount: ordered.length,
allOutputs: ordered.map(flatAssetToResultItem)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ describe('useMediaAssetActions', () => {
unmount()
})

it('keeps single explicit assets on the direct download path in cloud', () => {
it('downloads a single explicit asset by asset id in cloud', () => {
mockIsCloud.value = true
mockGetOutputAssetMetadata.mockReturnValue({
jobId: 'job1',
Expand All @@ -600,7 +600,7 @@ describe('useMediaAssetActions', () => {

expect(mockDownloadFile).toHaveBeenCalledOnce()
expect(mockDownloadFile).toHaveBeenCalledWith(
'https://example.com/single-output.png',
'http://localhost:8188/api/assets/single-output/content',
'single-output.png'
)
expect(mockCreateAssetExport).not.toHaveBeenCalled()
Expand Down
5 changes: 2 additions & 3 deletions src/platform/assets/composables/useMediaAssetActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getAssetStoredFilename
} from '../utils/assetMetadataUtils'
import { getAssetType } from '../utils/assetTypeUtil'
import { getAssetUrl } from '../utils/assetUrlUtil'
import { getAssetFileUrl } from '../utils/assetUrlUtil'
import { clearDeletedAssetWidgetValues } from '../utils/clearDeletedAssetWidgetValues'
import { clearNodePreviewCacheForValues } from '../utils/clearNodePreviewCacheForValues'
import { markDeletedAssetsAsMissingMedia } from '../utils/markDeletedAssetsAsMissingMedia'
Expand Down Expand Up @@ -144,8 +144,7 @@ export function useMediaAssetActions() {

function downloadSingleAsset(asset: AssetItem) {
const filename = getAssetDisplayName(asset)
const downloadUrl = asset.preview_url || getAssetUrl(asset)
downloadFile(downloadUrl, filename)
downloadFile(getAssetFileUrl(asset), filename)
}

async function expandAssetForDownload(
Expand Down
1 change: 1 addition & 0 deletions src/platform/assets/schemas/assetMetadataSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface OutputAssetMetadata extends Record<string, unknown> {
workflow?: ComfyWorkflowJSON
outputCount?: number
allOutputs?: AugmentedResultItem[]
assetId?: string
}

/**
Expand Down
91 changes: 90 additions & 1 deletion src/platform/assets/utils/assetUrlUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { describe, expect, it, vi } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
import {
getAssetFileUrl,
getAssetSubfolder,
getAssetUrl
} from '@/platform/assets/utils/assetUrlUtil'
import type { AugmentedResultItem } from '@/utils/resultItem'

const mockApiURL = vi.hoisted(() =>
vi.fn((path: string) => `http://localhost:8188/api${path}`)
)
const mockFlags = vi.hoisted(() => ({ assetsEnabled: false }))

vi.mock<unknown>(import('@/scripts/api'), () => ({
api: { apiURL: mockApiURL }
}))

vi.mock<unknown>(import('@/composables/useFeatureFlags'), () => ({
useFeatureFlags: () => ({ flags: mockFlags })
}))

function createAsset(overrides: Partial<AssetItem> = {}): AssetItem {
return {
id: 'asset-1',
Expand Down Expand Up @@ -73,3 +80,85 @@ describe('getAssetUrl', () => {
expect(getAssetUrl(createAsset())).not.toContain('subfolder')
})
})

describe('getAssetFileUrl', () => {
describe('with the assets API enabled', () => {
beforeEach(() => {
mockFlags.assetsEnabled = true
})

it('addresses the file by asset id without any path inference', () => {
const asset = createAsset({
id: '9a7111df-ccba-4be6-8057-c673755d097c',
name: 'ComfyUI_00110.glb',
display_name: '3d/ComfyUI_00110.glb'
})

expect(getAssetFileUrl(asset)).toBe(
'http://localhost:8188/api/assets/9a7111df-ccba-4be6-8057-c673755d097c/content'
)
})

it('uses the own asset id kept by a card grouped per job', () => {
const asset = createAsset({
id: 'job-1',
name: 'ComfyUI_00110.glb',
user_metadata: {
jobId: 'job-1',
subfolder: '',
assetId: 'asset-model-later',
outputCount: 2,
allOutputs: [
{ assetId: 'asset-model-earlier', filename: 'ComfyUI_00110.glb' },
{ assetId: 'asset-model-later', filename: 'ComfyUI_00110.glb' }
] as AugmentedResultItem[]
}
})

expect(getAssetFileUrl(asset)).toBe(
'http://localhost:8188/api/assets/asset-model-later/content'
)
})

it('does not use a preview url that points at a thumbnail', () => {
const asset = createAsset({
id: 'asset-model',
name: 'mesh.glb',
preview_id: 'asset-thumb',
preview_url: '/api/view?type=output&filename=thumb.png'
})

expect(getAssetFileUrl(asset)).toBe(
'http://localhost:8188/api/assets/asset-model/content'
)
})
})

describe('with history-backed assets', () => {
beforeEach(() => {
mockFlags.assetsEnabled = false
})

it('uses preview_url, which already points at the file', () => {
const asset = createAsset({
preview_url: '/api/view?filename=clip.webm&type=output&subfolder=vid'
})

expect(getAssetFileUrl(asset)).toBe(
'/api/view?filename=clip.webm&type=output&subfolder=vid'
)
})

it('builds a /view url from the metadata subfolder when preview_url is absent', () => {
const asset = createAsset({
name: 'mesh.glb',
user_metadata: { subfolder: '3d' }
})

const { searchParams } = new URL(getAssetFileUrl(asset))
expect(searchParams.get('filename')).toBe('mesh.glb')
expect(searchParams.get('type')).toBe('output')
expect(searchParams.get('subfolder')).toBe('3d')
})
})
})
25 changes: 25 additions & 0 deletions src/platform/assets/utils/assetUrlUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Utilities for constructing asset URLs
*/

import { useFeatureFlags } from '@/composables/useFeatureFlags'
import { api } from '@/scripts/api'
import { getOutputAssetMetadata } from '../schemas/assetMetadataSchema'
import type { AssetItem } from '../schemas/assetSchema'
import { getAssetType } from './assetTypeUtil'

Expand Down Expand Up @@ -51,3 +53,26 @@ export function getAssetSubfolder(asset: AssetItem): string {
const { subfolder } = asset.user_metadata ?? {}
return typeof subfolder === 'string' ? subfolder : ''
}

/**
* Id of the assets-API asset holding this item's own file. A card grouped per
* job carries the job id as its `id` and keeps its own asset id in metadata.
*/
function getAssetContentId(asset: AssetItem): string {
return getOutputAssetMetadata(asset.user_metadata)?.assetId || asset.id
}

/**
* URL of the asset's own file, for downloading or loading it whole.
*
* With the assets API enabled the file is served by id, so no path inference
* is needed and a preview that is only a thumbnail is never mistaken for the
* file. Otherwise the item came from the history API, whose `preview_url`
* already points at the file, with a `/view` URL as fallback.
*/
export function getAssetFileUrl(asset: AssetItem): string {
if (useFeatureFlags().flags.assetsEnabled) {
return api.apiURL(`/assets/${getAssetContentId(asset)}/content`)
}
return asset.preview_url || getAssetUrl(asset)
}
40 changes: 40 additions & 0 deletions src/platform/assets/utils/outputAssetUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type OutputOverrides = Partial<{
nodeId: SerializedNodeId
url: string
display_name: string
assetId: string
}>

function createOutput(overrides: OutputOverrides = {}): AugmentedResultItem {
Expand Down Expand Up @@ -169,6 +170,45 @@ describe('resolveOutputAssetItems', () => {
mocks.isCloud = false
})

it('keeps the asset id an output already carries as the item id', async () => {
const output = createOutput({
filename: 'a.png',
nodeId: '1',
assetId: 'asset-a'
})
const metadata: OutputAssetMetadata = {
jobId: 'job-1',
nodeId: '1',
subfolder: 'sub',
outputCount: 1,
allOutputs: [output]
}

const results = await resolveOutputAssetItems(metadata)

expect(results).toHaveLength(1)
expect(results[0].id).toBe('asset-a')
})

it('synthesizes an item id when the output carries an empty asset id', async () => {
const output = createOutput({
filename: 'a.png',
nodeId: '1',
assetId: ''
})
const metadata: OutputAssetMetadata = {
jobId: 'job-1',
nodeId: '1',
subfolder: 'sub',
outputCount: 1,
allOutputs: [output]
}

const results = await resolveOutputAssetItems(metadata)

expect(results[0].id).toBe(`job-1-${getOutputKey(output)}`)
})

it('maps outputs and excludes a composite output key', async () => {
const outputA = createOutput({
filename: 'a.png',
Expand Down
2 changes: 1 addition & 1 deletion src/platform/assets/utils/outputAssetUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function mapOutputsToAssetItems({
seenOutputKeys.add(outputKey)

items.push({
id: `${jobId}-${outputKey}`,
id: output.assetId || `${jobId}-${outputKey}`,
name: output.filename,
display_name: output.display_name,
size: 0,
Expand Down
Loading