Line data Source code
1 : import {
2 : zAgentProbeableAttachmentExtension,
3 : zAgentReadableAttachmentExtension,
4 : zAgentReferenceAttachmentExtension,
5 : zAgentRetainedAttachmentExtension,
6 : zAgentViewableAttachmentExtension
7 : } from '@comfyorg/ingest-types/zod'
8 :
9 : import type { VideoExtension } from '@/utils/formatUtil'
10 :
11 : export type AgentAttachCapability =
12 : | 'view'
13 : | 'probe'
14 : | 'read'
15 : | 'reference'
16 : | 'retain'
17 :
18 1 : const CAPABILITY_BY_EXTENSION = new Map<string, AgentAttachCapability>([
19 : ...zAgentViewableAttachmentExtension.options.map(
20 : (extension) => [extension, 'view'] as const
21 : ),
22 : ...zAgentProbeableAttachmentExtension.options.map(
23 : (extension) => [extension, 'probe'] as const
24 : ),
25 : ...zAgentReferenceAttachmentExtension.options.map(
26 : (extension) => [extension, 'reference'] as const
27 : ),
28 : ...zAgentReadableAttachmentExtension.options.map(
29 : (extension) => [extension, 'read'] as const
30 : ),
31 : ...zAgentRetainedAttachmentExtension.options.map(
32 : (extension) => [extension, 'retain'] as const
33 : )
34 : ])
35 :
36 1 : export const AGENT_ATTACH_ACCEPT = [...CAPABILITY_BY_EXTENSION.keys()].join(',')
37 :
38 1 : const EXTENSION_PATTERN = /\.[a-z0-9]{1,8}$/i
39 1 : const AUTHORITATIVE_EXTENSION_PATTERN = /\.[^./\\]+$/
40 :
41 0 : function extensionOf(filename: string): string {
42 0 : const match = EXTENSION_PATTERN.exec(filename)?.[0].toLowerCase()
43 0 : return match && /[a-z]/.test(match) ? match : ''
44 : }
45 :
46 0 : export function agentAttachCapability(
47 : filename: string
48 : ): AgentAttachCapability | undefined {
49 0 : return CAPABILITY_BY_EXTENSION.get(extensionOf(filename))
50 : }
51 :
52 0 : export function isAgentAttachable(file: File): boolean {
53 0 : return agentAttachCapability(file.name) !== undefined
54 : }
55 :
56 : export type AgentAttachVerdict = 'accepted' | 'rejected' | 'unknown'
57 :
58 0 : export function agentAttachVerdict(filename: string): AgentAttachVerdict {
59 0 : if (agentAttachCapability(filename)) return 'accepted'
60 0 : return extensionOf(filename) === '' ? 'unknown' : 'rejected'
61 : }
62 :
63 0 : export function agentAttachRefVerdict(ref: string): AgentAttachVerdict {
64 0 : if (agentAttachCapability(ref)) return 'accepted'
65 0 : return AUTHORITATIVE_EXTENSION_PATTERN.test(ref) ? 'rejected' : 'unknown'
66 : }
67 :
68 0 : function bytesMatch(bytes: Uint8Array, offset: number, expected: string) {
69 0 : return expected
70 : .split('')
71 : .every((char, index) => bytes[offset + index] === char.charCodeAt(0))
72 : }
73 :
74 : type VideoContainerCheck = (bytes: Uint8Array) => boolean
75 :
76 1 : const ISO_BASE_MEDIA_BOXES = [
77 : 'ftyp',
78 : 'moov',
79 : 'mdat',
80 : 'wide',
81 : 'free',
82 : 'skip',
83 : 'pnot'
84 : ] as const
85 1 : const EBML_MAGIC = '\x1a\x45\xdf\xa3'
86 :
87 0 : function isIsoBaseMedia(bytes: Uint8Array) {
88 0 : return ISO_BASE_MEDIA_BOXES.some((box) => bytesMatch(bytes, 4, box))
89 : }
90 :
91 0 : function isEbml(bytes: Uint8Array) {
92 0 : return bytesMatch(bytes, 0, EBML_MAGIC)
93 : }
94 :
95 1 : const VIDEO_CONTAINER_CHECKS: Partial<Record<string, VideoContainerCheck>> = {
96 : mp4: isIsoBaseMedia,
97 : m4v: isIsoBaseMedia,
98 : mov: isIsoBaseMedia,
99 : webm: isEbml,
100 : mkv: isEbml,
101 : avi: (bytes) => bytesMatch(bytes, 0, 'RIFF') && bytesMatch(bytes, 8, 'AVI ')
102 : } satisfies Record<VideoExtension, VideoContainerCheck>
103 :
104 0 : export function isValidAgentAttachment(file: File): boolean | Promise<boolean> {
105 0 : const extension = extensionOf(file.name).slice(1)
106 0 : const check = Object.hasOwn(VIDEO_CONTAINER_CHECKS, extension)
107 : ? VIDEO_CONTAINER_CHECKS[extension]
108 : : undefined
109 0 : if (!check) return true
110 :
111 0 : return file
112 : .slice(0, 12)
113 : .arrayBuffer()
114 : .then((buffer) => check(new Uint8Array(buffer)))
115 : .catch(() => false)
116 : }
|