Line data Source code
1 16 : <template>
2 16 : <div class="flex gap-2">
3 : <Input
4 : v-model="modelValue"
5 : class="flex-1"
6 : :placeholder="$t('g.imageUrl')"
7 : />
8 : <Button
9 : :tooltip="$t('g.upload')"
10 : variant="secondary"
11 : size="sm"
12 : :aria-label="$t('g.upload')"
13 : :disabled="isUploading"
14 : @click="triggerFileInput"
15 : >
16 : <i
17 : :class="
18 : isUploading
19 : ? 'icon-[lucide--loader-circle] animate-spin motion-reduce:animate-none'
20 : : 'icon-[lucide--upload]'
21 : "
22 : />
23 : </Button>
24 : <Button
25 : :tooltip="$t('g.clear')"
26 : variant="destructive"
27 : size="sm"
28 : :aria-label="$t('g.clear')"
29 : :disabled="!modelValue"
30 : @click="clearImage"
31 : >
32 : <i class="icon-[lucide--trash-2]" />
33 : </Button>
34 : <input
35 : ref="fileInput"
36 : type="file"
37 : class="hidden"
38 : accept="image/*"
39 : @change="handleFileUpload"
40 : />
41 : </div>
42 : </template>
43 :
44 : <script setup lang="ts">
45 : import { ref } from 'vue'
46 : import { useI18n } from 'vue-i18n'
47 :
48 : import Button from '@comfyorg/design-system/button/Button'
49 : import Input from '@comfyorg/design-system/input/Input'
50 : import { appendCloudResParam } from '@/platform/distribution/cloudPreviewUtil'
51 : import { useToast } from '@/components/ui/toast/toastStore'
52 : import { api } from '@/scripts/api'
53 : import { getErrorMessage } from '@comfyorg/shared-frontend-utils/errorUtil'
54 :
55 7 : const { t } = useI18n()
56 :
57 7 : const modelValue = defineModel<string>()
58 :
59 7 : const fileInput = ref<HTMLInputElement | null>(null)
60 7 : const isUploading = ref(false)
61 :
62 7 : const triggerFileInput = () => {
63 1 : fileInput.value?.click()
64 : }
65 :
66 7 : const uploadFile = async (file: File): Promise<string | null> => {
67 1 : const body = new FormData()
68 1 : body.append('image', file)
69 1 : body.append('subfolder', 'backgrounds')
70 :
71 1 : const resp = await api.fetchApi('/upload/image', {
72 : method: 'POST',
73 : body
74 : })
75 :
76 1 : if (resp.status !== 200) {
77 0 : useToast().warning(
78 : t('g.uploadFailed', { reason: `${resp.status} - ${resp.statusText}` })
79 : )
80 0 : return null
81 : }
82 :
83 1 : const data = await resp.json()
84 1 : return data.subfolder ? `${data.subfolder}/${data.name}` : data.name
85 : }
86 :
87 7 : const handleFileUpload = async (event: Event) => {
88 1 : const target = event.target as HTMLInputElement
89 1 : if (target.files && target.files[0]) {
90 1 : const file = target.files[0]
91 :
92 1 : isUploading.value = true
93 1 : try {
94 1 : const uploadedPath = await uploadFile(file)
95 1 : if (uploadedPath) {
96 : // Set the value to the API view URL with subfolder parameter
97 1 : const params = new URLSearchParams({
98 : filename: uploadedPath,
99 : type: 'input',
100 : subfolder: 'backgrounds'
101 : })
102 1 : appendCloudResParam(params, file.name)
103 1 : modelValue.value = `/api/view?${params.toString()}`
104 : }
105 : } catch (error) {
106 0 : useToast().warning(
107 : t('g.uploadFailed', {
108 : reason: getErrorMessage(error) ?? t('g.unknownError')
109 : })
110 : )
111 : } finally {
112 1 : isUploading.value = false
113 : }
114 : }
115 : }
116 :
117 7 : const clearImage = () => {
118 2 : modelValue.value = ''
119 2 : if (fileInput.value) {
120 2 : fileInput.value.value = ''
121 : }
122 : }
123 : </script>
|