Line data Source code
1 : import { downloadUrlToHfRepoUrl, isCivitaiModelUrl } from '@/utils/formatUtil'
2 : import { reportError } from '@comfyorg/telemetry/reportError'
3 : import { isDesktop } from '@/platform/distribution/types'
4 : import {
5 : WHITE_LISTED_URLS,
6 : isModelUrlAllowlisted
7 : } from '@/platform/workflowErrors/missingModel/modelUrlAllowlist'
8 : import { useElectronDownloadStore } from '@/stores/electronDownloadStore'
9 : import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore'
10 :
11 : // Intentionally restrictive subset of model extensions permitted for download.
12 : // Does not include .bin, .onnx, .gguf — see MODEL_FILE_EXTENSIONS in
13 : // missingModelScan.ts for the broader scanning set.
14 2355 : const ALLOWED_SUFFIXES = [
15 : '.safetensors',
16 : '.sft',
17 : '.ckpt',
18 : '.pth',
19 : '.pt'
20 : ] as const
21 :
22 2355 : const MODEL_LIBRARY_TAB_ID = 'model-library'
23 :
24 : export interface ModelWithUrl {
25 : name: string
26 : url: string
27 : directory: string
28 : }
29 :
30 : export type ModelDownloadDispatchOutcome =
31 : | {
32 : status: 'not-dispatched'
33 : reason: 'not-downloadable' | 'missing-directory-path'
34 : }
35 : | { status: 'browser-requested' }
36 : | {
37 : status: 'host-requested'
38 : host: 'desktop2' | 'electron'
39 : hostResult: Promise<boolean>
40 : }
41 : | {
42 : status: 'dispatch-failed'
43 : host: 'desktop2' | 'electron'
44 : error: unknown
45 : }
46 :
47 0 : function openUrlInNewTab(url: string, downloadAs?: string): void {
48 0 : try {
49 0 : const protocol = new URL(url).protocol
50 0 : if (protocol !== 'https:' && protocol !== 'http:') {
51 0 : console.warn('[missingModelDownload] Blocked unsupported URL scheme')
52 0 : return
53 : }
54 : } catch {
55 0 : console.warn('[missingModelDownload] Blocked malformed download URL')
56 0 : return
57 : }
58 :
59 0 : const link = document.createElement('a')
60 0 : link.href = url
61 0 : if (downloadAs) link.download = downloadAs
62 0 : link.target = '_blank'
63 0 : link.rel = 'noopener noreferrer'
64 0 : link.click()
65 : }
66 :
67 0 : export function openGatedRepoPage(url: string): void {
68 0 : if (!isTrustedHuggingFaceUrl(url)) return
69 0 : openUrlInNewTab(url)
70 : }
71 :
72 1 : function hasHuggingFaceHost(url: string): boolean {
73 1 : try {
74 1 : return new URL(url).hostname.toLowerCase() === 'huggingface.co'
75 : } catch {
76 0 : return false
77 : }
78 : }
79 :
80 14 : export function isTrustedHuggingFaceUrl(url: string): boolean {
81 14 : try {
82 14 : return new URL(url).origin === 'https://huggingface.co'
83 : } catch {
84 0 : return false
85 : }
86 : }
87 :
88 : /**
89 : * Converts a model download URL to a browsable page URL.
90 : * - HuggingFace: `/resolve/` → `/blob/` (file page with model info)
91 : * - Civitai: strips `/api/download` or `/api/v1` prefix (model page)
92 : */
93 1 : export function toBrowsableUrl(url: string): string {
94 1 : if (isCivitaiModelUrl(url)) {
95 0 : return url.replace('/api/download/', '/').replace('/api/v1/', '/')
96 : }
97 1 : if (hasHuggingFaceHost(url)) {
98 0 : return url.replace('/resolve/', '/blob/')
99 : }
100 1 : return url
101 : }
102 :
103 220 : export function isModelDownloadable(model: ModelWithUrl): boolean {
104 220 : if (!isModelUrlAllowlisted(model.url)) return false
105 220 : if (WHITE_LISTED_URLS.has(model.url)) return true
106 201 : if (!ALLOWED_SUFFIXES.some((suffix) => model.name.endsWith(suffix)))
107 0 : return false
108 134 : return true
109 : }
110 :
111 : type Desktop2Download = NonNullable<
112 : NonNullable<typeof window.__comfyDesktop2>['downloadModel']
113 : >
114 :
115 2355 : function modelDownloadRoute():
116 : | { host: 'desktop2'; download: Desktop2Download }
117 : | { host: 'electron' }
118 : | { host: 'browser' } {
119 2355 : const bridge = window.__comfyDesktop2
120 2355 : const isRemote = bridge?.isRemote?.() ?? window.__comfyDesktop2Remote ?? false
121 2355 : if (bridge?.downloadModel && !isRemote) {
122 0 : return { host: 'desktop2', download: bridge.downloadModel.bind(bridge) }
123 : }
124 2355 : return { host: isDesktop ? 'electron' : 'browser' }
125 : }
126 :
127 : /**
128 : * Only the Electron path needs a resolved `savePath`: the desktop2 bridge takes
129 : * the logical directory name and decides where to write, and a browser opens
130 : * the URL instead.
131 : */
132 0 : export function modelDownloadNeedsFolderPaths(): boolean {
133 0 : return modelDownloadRoute().host === 'electron'
134 : }
135 :
136 : /** Whether downloads go to the Desktop 2 host, which reports their progress. */
137 2355 : export function hostReportsModelDownloadProgress(): boolean {
138 2355 : return (
139 : modelDownloadRoute().host === 'desktop2' &&
140 : !!window.__comfyDesktop2?.onDownloadProgress
141 : )
142 : }
143 :
144 0 : export function dispatchModelDownload(
145 : model: ModelWithUrl,
146 : paths: Record<string, string[]>,
147 : { revealLegacyDownload = true }: { revealLegacyDownload?: boolean } = {}
148 : ): ModelDownloadDispatchOutcome {
149 0 : if (!isModelDownloadable(model)) {
150 0 : return { status: 'not-dispatched', reason: 'not-downloadable' }
151 : }
152 :
153 0 : const route = modelDownloadRoute()
154 0 : if (route.host === 'desktop2') {
155 0 : try {
156 0 : return {
157 : status: 'host-requested',
158 : host: 'desktop2',
159 : hostResult: Promise.resolve(
160 : route.download(model.url, model.name, model.directory)
161 : )
162 : }
163 : } catch (error) {
164 0 : return { status: 'dispatch-failed', host: 'desktop2', error }
165 : }
166 : }
167 :
168 0 : if (route.host === 'browser') {
169 0 : openUrlInNewTab(model.url, model.name)
170 0 : return { status: 'browser-requested' }
171 : }
172 :
173 0 : const savePath = paths[model.directory]?.[0]
174 0 : if (!savePath) {
175 0 : return { status: 'not-dispatched', reason: 'missing-directory-path' }
176 : }
177 :
178 0 : if (revealLegacyDownload) {
179 0 : useSidebarTabStore().activeSidebarTabId = MODEL_LIBRARY_TAB_ID
180 : }
181 0 : try {
182 0 : return {
183 : status: 'host-requested',
184 : host: 'electron',
185 : hostResult: Promise.resolve(
186 : useElectronDownloadStore().start({
187 : url: model.url,
188 : savePath,
189 : filename: model.name
190 : })
191 : )
192 : }
193 : } catch (error) {
194 0 : return { status: 'dispatch-failed', host: 'electron', error }
195 : }
196 : }
197 :
198 0 : export function downloadModel(
199 : model: ModelWithUrl,
200 : paths: Record<string, string[]>
201 : ): void {
202 0 : const outcome = dispatchModelDownload(model, paths)
203 :
204 0 : if (outcome.status === 'dispatch-failed') {
205 0 : reportError(outcome.error, {
206 : surface: 'platform',
207 : errorType: 'error_starting_model_download',
208 : tags: { host: outcome.host }
209 : })
210 0 : return
211 : }
212 :
213 0 : if (outcome.status !== 'host-requested') return
214 :
215 0 : void outcome.hostResult.catch((error: unknown) => {
216 0 : reportError(error, {
217 : surface: 'platform',
218 : errorType: 'error_starting_model_download',
219 : tags: { host: outcome.host }
220 : })
221 : })
222 : }
223 :
224 : export interface ModelMetadata {
225 : fileSize: number | null
226 : gatedRepoUrl: string | null
227 : }
228 :
229 : export type ModelMetadataFetchOutcome = {
230 : metadata: ModelMetadata
231 : resolution: 'resolved' | 'failed'
232 : }
233 :
234 : interface MetadataFetchResult extends ModelMetadataFetchOutcome {
235 : cacheable: boolean
236 : }
237 :
238 : interface CivitaiModelFile {
239 : sizeKB: number
240 : downloadUrl: string
241 : }
242 :
243 : interface CivitaiModelVersionResponse {
244 : files: CivitaiModelFile[]
245 : }
246 :
247 2355 : const metadataCache = new Map<string, ModelMetadataFetchOutcome>()
248 2355 : const inflight = new Map<string, Promise<ModelMetadataFetchOutcome>>()
249 :
250 : export function clearMetadataCache(): void {
251 : metadataCache.clear()
252 : inflight.clear()
253 : }
254 :
255 0 : async function fetchCivitaiMetadata(
256 : url: string,
257 : signal?: AbortSignal
258 : ): Promise<MetadataFetchResult> {
259 0 : try {
260 0 : const pathname = new URL(url).pathname
261 0 : const versionIdMatch =
262 : pathname.match(/^\/api\/download\/models\/(\d+)$/) ??
263 : pathname.match(/^\/api\/v1\/models-versions\/(\d+)$/)
264 :
265 0 : if (!versionIdMatch) {
266 0 : return {
267 : metadata: { fileSize: null, gatedRepoUrl: null },
268 : resolution: 'failed',
269 : cacheable: false
270 : }
271 : }
272 :
273 0 : const [, modelVersionId] = versionIdMatch
274 0 : const apiUrl = `https://civitai.com/api/v1/model-versions/${modelVersionId}`
275 0 : const res = signal ? await fetch(apiUrl, { signal }) : await fetch(apiUrl)
276 0 : if (!res.ok) {
277 0 : return {
278 : metadata: { fileSize: null, gatedRepoUrl: null },
279 : resolution: 'failed',
280 : cacheable: false
281 : }
282 : }
283 :
284 : const data: CivitaiModelVersionResponse = await res.json()
285 0 : const matchingFile = data.files.find((file) => {
286 0 : const downloadUrl = file.downloadUrl
287 0 : return (
288 : typeof downloadUrl === 'string' &&
289 : downloadUrl.length > 0 &&
290 : downloadUrl.startsWith(url)
291 : )
292 : })
293 : const fileSize = matchingFile?.sizeKB ? matchingFile.sizeKB * 1024 : null
294 0 : return {
295 : metadata: { fileSize, gatedRepoUrl: null },
296 : resolution: 'resolved',
297 : cacheable: true
298 : }
299 : } catch {
300 0 : return {
301 : metadata: { fileSize: null, gatedRepoUrl: null },
302 : resolution: 'failed',
303 : cacheable: false
304 : }
305 : }
306 : }
307 :
308 2355 : const GATED_STATUS_CODES = new Set([401, 403, 451])
309 2355 : const HUGGING_FACE_GATED_ERROR_CODE = 'GatedRepo'
310 :
311 0 : function failedMetadataResult(): MetadataFetchResult {
312 0 : return {
313 : metadata: { fileSize: null, gatedRepoUrl: null },
314 : resolution: 'failed',
315 : cacheable: false
316 : }
317 : }
318 :
319 4 : function getGatedRepoUrl(url: string, response: Response): string | null {
320 4 : if (!isTrustedHuggingFaceUrl(url)) return null
321 4 : if (!GATED_STATUS_CODES.has(response.status)) return null
322 4 : if (response.headers.get('x-error-code') !== HUGGING_FACE_GATED_ERROR_CODE) {
323 0 : return null
324 : }
325 4 : return downloadUrlToHfRepoUrl(url)
326 : }
327 :
328 2317 : function getResolvedHeadMetadata(response: Response): MetadataFetchResult {
329 2317 : const contentLength = response.headers.get('content-length')
330 2317 : const parsedSize = contentLength ? parseInt(contentLength, 10) : null
331 : const fileSize =
332 : parsedSize !== null && !Number.isNaN(parsedSize) ? parsedSize : null
333 2317 : return {
334 : metadata: { fileSize, gatedRepoUrl: null },
335 : resolution: 'resolved',
336 : cacheable: true
337 : }
338 : }
339 :
340 4 : function getFailedHeadMetadata(
341 : url: string,
342 : response: Response
343 : ): MetadataFetchResult {
344 4 : const gatedRepoUrl = getGatedRepoUrl(url, response)
345 4 : if (!gatedRepoUrl) return failedMetadataResult()
346 4 : return {
347 : metadata: { fileSize: null, gatedRepoUrl },
348 : resolution: 'resolved',
349 : cacheable: true
350 : }
351 : }
352 :
353 2323 : async function fetchHeadMetadata(
354 : url: string,
355 : signal?: AbortSignal
356 : ): Promise<MetadataFetchResult> {
357 2323 : try {
358 : // Deliberately uncredentialed HEADs prevent re-checks from clearing gating.
359 2323 : const response = await fetch(url, {
360 : method: 'HEAD',
361 : ...(signal && { signal })
362 : })
363 2323 : return response.ok
364 : ? getResolvedHeadMetadata(response)
365 : : getFailedHeadMetadata(url, response)
366 : } catch {
367 0 : return failedMetadataResult()
368 : }
369 : }
370 :
371 2323 : async function fetchMetadataResult(
372 : url: string,
373 : signal?: AbortSignal
374 : ): Promise<ModelMetadataFetchOutcome> {
375 2323 : const result = isCivitaiModelUrl(url)
376 : ? await fetchCivitaiMetadata(url, signal)
377 : : await fetchHeadMetadata(url, signal)
378 2321 : const outcome: ModelMetadataFetchOutcome = {
379 : metadata: result.metadata,
380 : resolution: result.resolution
381 : }
382 2321 : if (result.cacheable) metadataCache.set(url, outcome)
383 2321 : return outcome
384 : }
385 :
386 2682 : export async function fetchModelMetadataWithStatus(
387 : url: string,
388 : { signal }: { signal?: AbortSignal } = {}
389 : ): Promise<ModelMetadataFetchOutcome> {
390 2682 : if (!isModelUrlAllowlisted(url)) {
391 0 : return {
392 : metadata: { fileSize: null, gatedRepoUrl: null },
393 : resolution: 'resolved'
394 : }
395 : }
396 :
397 2682 : const cached = metadataCache.get(url)
398 2682 : if (cached !== undefined) return cached
399 :
400 : // A cancellable consumer owns its request lifecycle. Do not join a shared
401 : // request that cannot be aborted with the caller's signal.
402 2682 : if (signal) return fetchMetadataResult(url, signal)
403 :
404 2334 : const existing = inflight.get(url)
405 2682 : if (existing) return existing
406 :
407 2323 : const promise = fetchMetadataResult(url)
408 :
409 2323 : inflight.set(url, promise)
410 2346 : try {
411 2323 : return await promise
412 : } finally {
413 2321 : inflight.delete(url)
414 : }
415 : }
416 :
417 2682 : export async function fetchModelMetadata(url: string): Promise<ModelMetadata> {
418 2682 : return (await fetchModelMetadataWithStatus(url)).metadata
419 : }
|