mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-01-26 10:59:53 +00:00
feat: add getAssetFilename util with fallback chain (#8309)
## Summary ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-8309-feat-add-getAssetFilename-util-with-fallback-chain-2f36d73d36508141be81ecc52c0a2858) by [Unito](https://www.unito.io) --------- Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
@@ -149,3 +149,13 @@ export function getAssetUserDescription(asset: AssetItem): string {
|
||||
? asset.user_metadata.user_description
|
||||
: ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the filename for an asset with fallback chain
|
||||
* Checks user_metadata.filename first, then metadata.filename, then asset.name
|
||||
* @param asset - The asset to extract filename from
|
||||
* @returns The filename string
|
||||
*/
|
||||
export function getAssetFilename(asset: AssetItem): string {
|
||||
return getStringProperty(asset, 'filename') ?? asset.name
|
||||
}
|
||||
|
||||
@@ -282,16 +282,6 @@ describe('createModelNodeFromAsset', () => {
|
||||
expectedCode: 'INVALID_ASSET' as const,
|
||||
errorPattern: /Invalid filename.*expected non-empty string/
|
||||
},
|
||||
{
|
||||
case: 'non-string filename',
|
||||
overrides: {
|
||||
user_metadata: { filename: 123 },
|
||||
metadata: undefined,
|
||||
name: ''
|
||||
},
|
||||
expectedCode: 'INVALID_ASSET' as const,
|
||||
errorPattern: /Invalid filename.*expected non-empty string, got number/
|
||||
},
|
||||
{
|
||||
case: 'empty filename with no fallback',
|
||||
overrides: {
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
MISSING_TAG,
|
||||
MODELS_TAG
|
||||
} from '@/platform/assets/services/assetService'
|
||||
import { getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils'
|
||||
import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
|
||||
import { app } from '@/scripts/app'
|
||||
import { useLitegraphService } from '@/services/litegraphService'
|
||||
@@ -69,11 +70,8 @@ export function createModelNodeFromAsset(
|
||||
|
||||
const validAsset = validatedAsset.data
|
||||
|
||||
const userMetadata = validAsset.user_metadata ?? {}
|
||||
|
||||
const filename =
|
||||
userMetadata.filename || validAsset.metadata?.filename || validAsset.name
|
||||
if (typeof filename !== 'string' || filename.length === 0) {
|
||||
const filename = getAssetFilename(validAsset)
|
||||
if (filename.length === 0) {
|
||||
console.error(
|
||||
`Asset ${validAsset.id} has invalid user_metadata.filename (expected non-empty string, got ${typeof filename})`
|
||||
)
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
assetFilenameSchema,
|
||||
assetItemSchema
|
||||
} from '@/platform/assets/schemas/assetSchema'
|
||||
import { getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils'
|
||||
import { assetService } from '@/platform/assets/services/assetService'
|
||||
import { isCloud } from '@/platform/distribution/types'
|
||||
import { useSettingStore } from '@/platform/settings/settingStore'
|
||||
@@ -115,7 +116,7 @@ const createAssetBrowserWidget = (
|
||||
return
|
||||
}
|
||||
|
||||
const filename = validatedAsset.data.user_metadata?.filename
|
||||
const filename = getAssetFilename(validatedAsset.data)
|
||||
const validatedFilename = assetFilenameSchema.safeParse(filename)
|
||||
|
||||
if (!validatedFilename.success) {
|
||||
|
||||
Reference in New Issue
Block a user