(fileBlob: Blob, filename: string)
| 295 | } |
| 296 | |
| 297 | async function uploadToShareX(fileBlob: Blob, filename: string): Promise<string> { |
| 298 | const config = parseShareXConfigFromSettings(); |
| 299 | const method = (config.RequestMethod || "POST").toUpperCase(); |
| 300 | const requestUrl = config.RequestURL!.trim(); |
| 301 | const bodyType = (config.Body || "MultipartFormData").toLowerCase(); |
| 302 | |
| 303 | const headers = new Headers(); |
| 304 | for (const [key, value] of Object.entries(config.Headers || {})) { |
| 305 | headers.set(key, resolveShareXRequestValue(value as string | number | boolean, filename)); |
| 306 | } |
| 307 | |
| 308 | const buildArguments = () => { |
| 309 | const args: Record<string, string> = {}; |
| 310 | for (const [key, value] of Object.entries(config.Arguments || {})) { |
| 311 | args[key] = resolveShareXRequestValue(value as string | number | boolean, filename); |
| 312 | } |
| 313 | return args; |
| 314 | }; |
| 315 | |
| 316 | let body: BodyInit; |
| 317 | |
| 318 | if (bodyType === "multipartformdata" || bodyType === "formdata") { |
| 319 | headers.delete("content-type"); |
| 320 | |
| 321 | const formData = new FormData(); |
| 322 | const fileField = config.FileFormName || "file"; |
| 323 | formData.append(fileField, fileBlob, filename); |
| 324 | |
| 325 | const args = buildArguments(); |
| 326 | for (const [key, value] of Object.entries(args)) { |
| 327 | formData.append(key, value); |
| 328 | } |
| 329 | |
| 330 | body = formData; |
| 331 | } else if (bodyType === "binary") { |
| 332 | body = fileBlob; |
| 333 | } else if (bodyType === "json") { |
| 334 | if (!headers.has("content-type")) { |
| 335 | headers.set("content-type", "application/json"); |
| 336 | } |
| 337 | |
| 338 | const payload = buildArguments(); |
| 339 | body = JSON.stringify(payload); |
| 340 | } else { |
| 341 | throw new Error(`Unsupported ShareX Body type: ${config.Body || "unknown"}`); |
| 342 | } |
| 343 | |
| 344 | const response = await uploadRequestWithTimeout(requestUrl, { method, headers, body }); |
| 345 | |
| 346 | const responseText = await response.text(); |
| 347 | let responseJson: unknown = null; |
| 348 | try { |
| 349 | responseJson = responseText ? JSON.parse(responseText) : null; |
| 350 | } catch { |
| 351 | responseJson = null; |
| 352 | } |
| 353 | |
| 354 | if (!response.ok) { |
no test coverage detected