Line data Source code
1 : import { t } from '@/i18n'
2 : import { useToast } from '@/components/ui/toast/toastStore'
3 : import { api } from '@/scripts/api'
4 : import { useApp } from '@/scripts/appInstance'
5 : import { uploadTempFile } from '@/services/uploadTempFile'
6 : import { getErrorMessage } from '@comfyorg/shared-frontend-utils/errorUtil'
7 :
8 2356 : class Load3dUtils {
9 : static async uploadTempImage(
10 : imageData: string,
11 : prefix: string,
12 : fileType: string = 'png'
13 : ) {
14 9 : const blob = await fetch(imageData).then((r) => r.blob())
15 9 : const name = `${prefix}_${Date.now()}.${fileType}`
16 : const file = new File([blob], name, {
17 : type: fileType === 'mp4' ? 'video/mp4' : 'image/png'
18 : })
19 :
20 9 : const upload = await uploadTempFile(file, 'threed')
21 9 : if (!upload.ok)
22 0 : throw new Error(`Error uploading temp file: ${upload.reason}`)
23 9 : return upload.file
24 : }
25 :
26 : static readonly MAX_UPLOAD_SIZE_MB = 100
27 :
28 : static async uploadFile(file: File, subfolder: string) {
29 8 : let uploadPath
30 :
31 8 : const fileSizeMB = file.size / 1024 / 1024
32 8 : if (fileSizeMB > this.MAX_UPLOAD_SIZE_MB) {
33 0 : const message = t('toastMessages.fileTooLarge', {
34 : size: fileSizeMB.toFixed(1),
35 : maxSize: this.MAX_UPLOAD_SIZE_MB
36 : })
37 0 : console.warn(
38 : '[Load3D] uploadFile: file too large',
39 : fileSizeMB.toFixed(2),
40 : 'MB'
41 : )
42 0 : useToast().warning(message)
43 0 : return undefined
44 : }
45 :
46 8 : try {
47 8 : const body = new FormData()
48 8 : body.append('image', file)
49 :
50 8 : body.append('subfolder', subfolder)
51 :
52 8 : const resp = await api.fetchApi('/upload/image', {
53 : method: 'POST',
54 : body
55 : })
56 :
57 8 : if (resp.status === 200) {
58 8 : const data = await resp.json()
59 8 : let path = data.name
60 :
61 8 : if (data.subfolder) {
62 8 : path = data.subfolder + '/' + path
63 : }
64 :
65 8 : uploadPath = path
66 : } else {
67 0 : useToast().warning(
68 : t('g.uploadFailed', {
69 : reason: `${resp.status} - ${resp.statusText}`
70 : })
71 : )
72 : }
73 : } catch (error) {
74 0 : console.error('[Load3D] uploadFile: exception', error)
75 0 : useToast().warning(
76 : t('g.uploadFailed', {
77 : reason: getErrorMessage(error) ?? t('g.unknownError')
78 : })
79 : )
80 : }
81 :
82 8 : return uploadPath
83 : }
84 :
85 : static splitFilePath(path: string): [string, string] {
86 21 : const folder_separator = path.lastIndexOf('/')
87 21 : if (folder_separator === -1) {
88 4 : return ['', path]
89 : }
90 17 : return [
91 : path.substring(0, folder_separator),
92 : path.substring(folder_separator + 1)
93 : ]
94 : }
95 :
96 : static getResourceURL(
97 : subfolder: string,
98 : filename: string,
99 : type: string = 'input'
100 : ): string {
101 : const params = [
102 : 'filename=' + encodeURIComponent(filename),
103 : 'type=' + type,
104 : 'subfolder=' + subfolder,
105 : useApp().getRandParam().substring(1)
106 : ].join('&')
107 :
108 21 : return `/view?${params}`
109 : }
110 :
111 : static isSameResourceURL(a: string | null, b: string): boolean {
112 0 : if (!a) return false
113 0 : const withoutRand = (url: string) => {
114 0 : const parsed = new URL(url, window.location.origin)
115 0 : parsed.searchParams.delete('rand')
116 0 : parsed.searchParams.sort()
117 0 : return parsed.toString()
118 : }
119 0 : return withoutRand(a) === withoutRand(b)
120 : }
121 :
122 : static async uploadMultipleFiles(files: FileList, subfolder: string = '3d') {
123 0 : const uploadPromises = Array.from(files).map((file) =>
124 : this.uploadFile(file, subfolder)
125 : )
126 :
127 0 : await Promise.all(uploadPromises)
128 : }
129 :
130 : static mapSceneLightIntensityToHdri(
131 : sceneIntensity: number,
132 : sceneMin: number,
133 : sceneMax: number
134 : ): number {
135 0 : const span = sceneMax - sceneMin
136 0 : const t = span > 0 ? (sceneIntensity - sceneMin) / span : 0
137 : const clampedT = Math.min(1, Math.max(0, t))
138 0 : const mapped = clampedT * 5
139 : const minHdri = 0.25
140 0 : return Math.min(5, Math.max(minHdri, mapped))
141 : }
142 : }
143 :
144 : export default Load3dUtils
|