LCOV - code coverage report
Current view: top level - src/platform/assets/utils - assetPreviewUtil.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 17.1 % 35 6
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { useFeatureFlags } from '@/composables/useFeatureFlags'
       2              : import { assetService } from '@/platform/assets/services/assetService'
       3              : import { api } from '@/scripts/api'
       4              : import { useAssetsStore } from '@/stores/assetsStore'
       5              : 
       6              : import { getAssetContentId } from './assetUrlUtil'
       7              : 
       8              : interface AssetRecord {
       9              :   id: string
      10              :   name: string
      11              :   hash?: string | null
      12              :   preview_url?: string
      13              :   preview_id?: string | null
      14              :   user_metadata?: Record<string, unknown>
      15              : }
      16              : 
      17              : /**
      18              :  * Whether the backend can serve asset preview/thumbnail data.
      19              :  */
      20           10 : export function isAssetPreviewSupported(): boolean {
      21           10 :   return useFeatureFlags().flags.assetsEnabled
      22              : }
      23              : 
      24            0 : async function fetchOwnAssets(
      25              :   params: Record<string, string>
      26              : ): Promise<AssetRecord[]> {
      27            0 :   const query = new URLSearchParams({ ...params, include_public: 'false' })
      28            0 :   const res = await api.fetchApi(`/assets?${query}`)
      29            0 :   if (!res.ok) return []
      30              :   const data = await res.json()
      31            0 :   return data.assets ?? []
      32              : }
      33              : 
      34            2 : export function resolvePreviewUrl(asset: AssetRecord): string {
      35            2 :   if (asset.preview_url) return api.apiURL(asset.preview_url)
      36              : 
      37            2 :   const contentId = asset.preview_id ?? getAssetContentId(asset)
      38            2 :   return api.apiURL(`/assets/${contentId}/content`)
      39              : }
      40              : 
      41              : /**
      42              :  * Find an output asset record by content hash, falling back to name.
      43              :  * On cloud, output filenames are content-hashed; use hash to match.
      44              :  * On local, filenames are not hashed; use name_contains to match.
      45              :  */
      46            0 : export async function findOutputAsset(
      47              :   name: string
      48              : ): Promise<AssetRecord | undefined> {
      49              :   const byHash = await fetchOwnAssets({ hash: name })
      50            0 :   const hashMatch = byHash.find((a) => a.hash === name)
      51            0 :   if (hashMatch) return hashMatch
      52              : 
      53              :   const byName = await fetchOwnAssets({ name_contains: name })
      54            0 :   return byName.find((a) => a.name === name)
      55              : }
      56              : 
      57            0 : export async function findServerPreviewUrl(
      58              :   name: string
      59              : ): Promise<string | null> {
      60            0 :   try {
      61            0 :     const asset = await findOutputAsset(name)
      62            0 :     if (!asset?.preview_id) return null
      63              : 
      64            0 :     return resolvePreviewUrl(asset)
      65              :   } catch {
      66            0 :     return null
      67              :   }
      68              : }
      69              : 
      70            0 : export async function persistThumbnail(
      71              :   name: string,
      72              :   blob: Blob
      73              : ): Promise<void> {
      74            0 :   try {
      75            0 :     const asset = await findOutputAsset(name)
      76            0 :     if (!asset || asset.preview_id) return
      77              : 
      78            0 :     const previewFilename = `${asset.name}_preview.png`
      79            0 :     const uploaded = await assetService.uploadAssetFromBase64({
      80              :       data: await blobToDataUrl(blob),
      81              :       name: previewFilename,
      82              :       tags: ['output'],
      83              :       user_metadata: { filename: previewFilename }
      84              :     })
      85              : 
      86            0 :     await assetService.updateAsset(asset.id, {
      87              :       preview_id: uploaded.id
      88              :     })
      89            0 :     await useAssetsStore().outputAssets.invalidate()
      90              :   } catch {
      91              :     // Non-critical — client still shows the rendered thumbnail
      92              :   }
      93              : }
      94              : 
      95            0 : function blobToDataUrl(blob: Blob): Promise<string> {
      96            0 :   return new Promise((resolve, reject) => {
      97            0 :     const reader = new FileReader()
      98            0 :     reader.onload = () => resolve(reader.result as string)
      99            0 :     reader.onerror = reject
     100            0 :     reader.readAsDataURL(blob)
     101              :   })
     102              : }
        

Generated by: LCOV version 2.0-1