|
| 1 | +import { Upload, SetUpload } from "./types" |
| 2 | +import { r, pathDir } from "~/utils" |
| 3 | + |
| 4 | +export const HttpDirectUpload: Upload = async ( |
| 5 | + uploadPath: string, |
| 6 | + file: File, |
| 7 | + setUpload: SetUpload, |
| 8 | + _asTask: boolean, |
| 9 | + overwrite: boolean, |
| 10 | + _rapid: boolean, |
| 11 | +) => { |
| 12 | + const path = pathDir(uploadPath) |
| 13 | + |
| 14 | + // Get direct upload info from backend |
| 15 | + const resp = await r.post( |
| 16 | + "/fs/get_direct_upload_info", |
| 17 | + { |
| 18 | + path, |
| 19 | + file_name: file.name, |
| 20 | + file_size: file.size, |
| 21 | + tool: "HttpDirect", |
| 22 | + }, |
| 23 | + { |
| 24 | + headers: { |
| 25 | + Overwrite: overwrite, |
| 26 | + }, |
| 27 | + }, |
| 28 | + ) |
| 29 | + |
| 30 | + const uploadInfo = resp.data |
| 31 | + |
| 32 | + // If upload_info is null, direct upload is not supported - fallback to Stream |
| 33 | + if (!uploadInfo) { |
| 34 | + throw new Error("Http Direct Upload not supported") |
| 35 | + } |
| 36 | + |
| 37 | + // Upload file directly to storage |
| 38 | + const chunkSize = uploadInfo.chunk_size || 0 |
| 39 | + const uploadURL = uploadInfo.upload_url |
| 40 | + const method = uploadInfo.method || "PUT" |
| 41 | + |
| 42 | + if (chunkSize > 0 && file.size > chunkSize) { |
| 43 | + // Chunked upload |
| 44 | + return await uploadChunked( |
| 45 | + file, |
| 46 | + uploadURL, |
| 47 | + chunkSize, |
| 48 | + method, |
| 49 | + uploadInfo.headers, |
| 50 | + setUpload, |
| 51 | + ) |
| 52 | + } else { |
| 53 | + // Single upload |
| 54 | + return await uploadSingle( |
| 55 | + file, |
| 56 | + uploadURL, |
| 57 | + method, |
| 58 | + uploadInfo.headers, |
| 59 | + setUpload, |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +async function uploadSingle( |
| 65 | + file: File, |
| 66 | + uploadURL: string, |
| 67 | + method: string, |
| 68 | + headers?: Record<string, string>, |
| 69 | + setUpload?: SetUpload, |
| 70 | +): Promise<undefined> { |
| 71 | + const xhr = new XMLHttpRequest() |
| 72 | + |
| 73 | + return new Promise((resolve, reject) => { |
| 74 | + xhr.upload.addEventListener("progress", (e) => { |
| 75 | + if (e.lengthComputable && setUpload) { |
| 76 | + const progress = (e.loaded / e.total) * 100 |
| 77 | + setUpload("progress", progress) |
| 78 | + setUpload("speed", 0) // Speed calculation not implemented |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + xhr.addEventListener("load", () => { |
| 83 | + if (xhr.status >= 200 && xhr.status < 300) { |
| 84 | + resolve(undefined) |
| 85 | + } else { |
| 86 | + reject(new Error(`Upload failed with status ${xhr.status}`)) |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + xhr.addEventListener("error", () => { |
| 91 | + reject(new Error("Upload failed")) |
| 92 | + }) |
| 93 | + |
| 94 | + xhr.open(method, uploadURL) |
| 95 | + |
| 96 | + // Set custom headers if provided |
| 97 | + if (headers) { |
| 98 | + Object.entries(headers).forEach(([key, value]) => { |
| 99 | + xhr.setRequestHeader(key, value) |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + xhr.send(file) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +async function uploadChunked( |
| 108 | + file: File, |
| 109 | + uploadURL: string, |
| 110 | + chunkSize: number, |
| 111 | + method: string, |
| 112 | + headers?: Record<string, string>, |
| 113 | + setUpload?: SetUpload, |
| 114 | +): Promise<undefined> { |
| 115 | + const totalChunks = Math.ceil(file.size / chunkSize) |
| 116 | + let uploadedBytes = 0 |
| 117 | + |
| 118 | + for (let i = 0; i < totalChunks; i++) { |
| 119 | + const start = i * chunkSize |
| 120 | + const end = Math.min(start + chunkSize, file.size) |
| 121 | + const chunk = file.slice(start, end) |
| 122 | + |
| 123 | + const xhr = new XMLHttpRequest() |
| 124 | + |
| 125 | + await new Promise<void>((resolve, reject) => { |
| 126 | + xhr.upload.addEventListener("progress", (e) => { |
| 127 | + if (e.lengthComputable && setUpload) { |
| 128 | + const chunkProgress = uploadedBytes + e.loaded |
| 129 | + const progress = (chunkProgress / file.size) * 100 |
| 130 | + setUpload("progress", progress) |
| 131 | + setUpload("speed", 0) // Speed calculation not implemented |
| 132 | + } |
| 133 | + }) |
| 134 | + |
| 135 | + xhr.addEventListener("load", () => { |
| 136 | + if (xhr.status >= 200 && xhr.status < 300) { |
| 137 | + uploadedBytes += chunk.size |
| 138 | + resolve() |
| 139 | + } else { |
| 140 | + reject( |
| 141 | + new Error(`Upload chunk ${i + 1} failed with status ${xhr.status}`), |
| 142 | + ) |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + xhr.addEventListener("error", () => { |
| 147 | + reject(new Error(`Upload chunk ${i + 1} failed`)) |
| 148 | + }) |
| 149 | + |
| 150 | + xhr.open(method, uploadURL) |
| 151 | + |
| 152 | + // Set Content-Range header for chunked upload |
| 153 | + xhr.setRequestHeader( |
| 154 | + "Content-Range", |
| 155 | + `bytes ${start}-${end - 1}/${file.size}`, |
| 156 | + ) |
| 157 | + |
| 158 | + // Set custom headers if provided |
| 159 | + if (headers) { |
| 160 | + Object.entries(headers).forEach(([key, value]) => { |
| 161 | + xhr.setRequestHeader(key, value) |
| 162 | + }) |
| 163 | + } |
| 164 | + |
| 165 | + xhr.send(chunk) |
| 166 | + }) |
| 167 | + } |
| 168 | + |
| 169 | + return undefined |
| 170 | +} |
0 commit comments