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:
Alexander Brown
2026-01-24 17:50:00 -08:00
committed by GitHub
parent 7b0830a4ca
commit 702c917e57
4 changed files with 15 additions and 16 deletions

View File

@@ -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
}

View File

@@ -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: {

View File

@@ -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})`
)

View File

@@ -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) {