mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-24 06:35:10 +00:00
## Summary Adds tests for metadata parsers ## Changes - **What**: - add test file generation script - identified & fixed bug in webp exif parsing over-reading - identified & fix bug in mp3/ogg parser where it would read from a fixed position instead of relative, causing incorrect reads throwing RangeError - added catch in latent + json parsing to resolve errors ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-11307-test-add-metadata-parser-coverage-3446d73d36508108ac36dddcec0a54d4) by [Unito](https://www.unito.io) --------- Co-authored-by: GitHub Action <action@github.com>
37 lines
1023 B
TypeScript
37 lines
1023 B
TypeScript
import { isObject } from 'es-toolkit/compat'
|
|
|
|
export function getDataFromJSON(
|
|
file: File
|
|
): Promise<Record<string, object> | undefined> {
|
|
return new Promise<Record<string, object> | undefined>((resolve) => {
|
|
const reader = new FileReader()
|
|
reader.onload = async () => {
|
|
try {
|
|
if (typeof reader.result !== 'string') {
|
|
resolve(undefined)
|
|
return
|
|
}
|
|
const jsonContent = JSON.parse(reader.result)
|
|
if (jsonContent?.templates) {
|
|
resolve({ templates: jsonContent.templates })
|
|
return
|
|
}
|
|
if (isApiJson(jsonContent)) {
|
|
resolve({ prompt: jsonContent })
|
|
return
|
|
}
|
|
resolve({ workflow: jsonContent })
|
|
} catch {
|
|
resolve(undefined)
|
|
}
|
|
}
|
|
reader.onerror = () => resolve(undefined)
|
|
reader.onabort = () => resolve(undefined)
|
|
reader.readAsText(file)
|
|
})
|
|
}
|
|
|
|
function isApiJson(data: unknown) {
|
|
return isObject(data) && Object.values(data).every((v) => v.class_type)
|
|
}
|