|
| 1 | +interface BlobResult { |
| 2 | + pathname: string |
| 3 | + url?: string |
| 4 | + contentType?: string |
| 5 | + size: number |
| 6 | +} |
| 7 | + |
| 8 | +function createObjectUrl(file: File): string { |
| 9 | + return URL.createObjectURL(file) |
| 10 | +} |
| 11 | + |
| 12 | +function fileToInput(file: File): HTMLInputElement { |
| 13 | + const dataTransfer = new DataTransfer() |
| 14 | + dataTransfer.items.add(file) |
| 15 | + |
| 16 | + const input = document.createElement('input') |
| 17 | + input.type = 'file' |
| 18 | + input.files = dataTransfer.files |
| 19 | + |
| 20 | + return input |
| 21 | +} |
| 22 | + |
| 23 | +export function useFileUploadWithStatus(chatId: string) { |
| 24 | + const files = ref<FileWithStatus[]>([]) |
| 25 | + const toast = useToast() |
| 26 | + const { loggedIn } = useUserSession() |
| 27 | + |
| 28 | + const upload = useUpload(`/api/upload/${chatId}`, { method: 'PUT' }) |
| 29 | + |
| 30 | + async function uploadFiles(newFiles: File[]) { |
| 31 | + if (!loggedIn.value) { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + const filesWithStatus: FileWithStatus[] = newFiles.map(file => ({ |
| 36 | + file, |
| 37 | + id: crypto.randomUUID(), |
| 38 | + previewUrl: createObjectUrl(file), |
| 39 | + status: 'uploading' as const |
| 40 | + })) |
| 41 | + |
| 42 | + files.value = [...files.value, ...filesWithStatus] |
| 43 | + |
| 44 | + const uploadPromises = filesWithStatus.map(async (fileWithStatus) => { |
| 45 | + const index = files.value.findIndex(f => f.id === fileWithStatus.id) |
| 46 | + if (index === -1) return |
| 47 | + |
| 48 | + try { |
| 49 | + const input = fileToInput(fileWithStatus.file) |
| 50 | + const response = await upload(input) as BlobResult | BlobResult[] | undefined |
| 51 | + |
| 52 | + if (!response) { |
| 53 | + throw new Error('Upload failed') |
| 54 | + } |
| 55 | + |
| 56 | + const result = Array.isArray(response) ? response[0] : response |
| 57 | + |
| 58 | + if (!result) { |
| 59 | + throw new Error('Upload failed') |
| 60 | + } |
| 61 | + |
| 62 | + files.value[index] = { |
| 63 | + ...files.value[index]!, |
| 64 | + status: 'uploaded', |
| 65 | + uploadedUrl: result.url, |
| 66 | + uploadedPathname: result.pathname |
| 67 | + } |
| 68 | + } catch (error) { |
| 69 | + const errorMessage = (error as { data?: { message?: string } }).data?.message |
| 70 | + || (error as Error).message |
| 71 | + || 'Upload failed' |
| 72 | + toast.add({ |
| 73 | + title: 'Upload failed', |
| 74 | + description: errorMessage, |
| 75 | + icon: 'i-lucide-alert-circle', |
| 76 | + color: 'error' |
| 77 | + }) |
| 78 | + files.value[index] = { |
| 79 | + ...files.value[index]!, |
| 80 | + status: 'error', |
| 81 | + error: errorMessage |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + await Promise.allSettled(uploadPromises) |
| 87 | + } |
| 88 | + |
| 89 | + const { dropzoneRef, isDragging } = useFileUpload({ |
| 90 | + accept: FILE_UPLOAD_CONFIG.acceptPattern, |
| 91 | + multiple: true, |
| 92 | + onUpdate: uploadFiles |
| 93 | + }) |
| 94 | + |
| 95 | + const isUploading = computed(() => |
| 96 | + files.value.some(f => f.status === 'uploading') |
| 97 | + ) |
| 98 | + |
| 99 | + const uploadedFiles = computed(() => |
| 100 | + files.value |
| 101 | + .filter(f => f.status === 'uploaded' && f.uploadedUrl) |
| 102 | + .map(f => ({ |
| 103 | + type: 'file' as const, |
| 104 | + mediaType: f.file.type, |
| 105 | + url: f.uploadedUrl! |
| 106 | + })) |
| 107 | + ) |
| 108 | + |
| 109 | + function removeFile(id: string) { |
| 110 | + const file = files.value.find(f => f.id === id) |
| 111 | + if (!file) return |
| 112 | + |
| 113 | + URL.revokeObjectURL(file.previewUrl) |
| 114 | + files.value = files.value.filter(f => f.id !== id) |
| 115 | + |
| 116 | + if (file.status === 'uploaded' && file.uploadedPathname) { |
| 117 | + fetch(`/api/upload/${file.uploadedPathname}`, { |
| 118 | + method: 'DELETE' |
| 119 | + }).catch((error) => { |
| 120 | + console.error('Failed to delete file from blob:', error) |
| 121 | + }) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + function clearFiles() { |
| 126 | + if (files.value.length === 0) return |
| 127 | + files.value.forEach(fileWithStatus => URL.revokeObjectURL(fileWithStatus.previewUrl)) |
| 128 | + files.value = [] |
| 129 | + } |
| 130 | + |
| 131 | + onUnmounted(() => { |
| 132 | + clearFiles() |
| 133 | + }) |
| 134 | + |
| 135 | + return { |
| 136 | + dropzoneRef, |
| 137 | + isDragging, |
| 138 | + files, |
| 139 | + isUploading, |
| 140 | + uploadedFiles, |
| 141 | + addFiles: uploadFiles, |
| 142 | + removeFile, |
| 143 | + clearFiles |
| 144 | + } |
| 145 | +} |
0 commit comments