| 415 | } |
| 416 | |
| 417 | export async function uploadToPixelDrain( |
| 418 | _: IpcMainInvokeEvent, |
| 419 | fileBuffer: ArrayBuffer, |
| 420 | filename: string, |
| 421 | apiKey?: string |
| 422 | ): Promise<NativeUploadResult> { |
| 423 | try { |
| 424 | const headers: Record<string, string> = {}; |
| 425 | if (apiKey?.trim()) { |
| 426 | headers.Authorization = `Basic ${Buffer.from(`:${apiKey.trim()}`).toString("base64")}`; |
| 427 | } |
| 428 | |
| 429 | const response = await rawFetch(`https://pixeldrain.com/api/file/${encodeURIComponent(filename)}`, "PUT", fileBuffer, headers); |
| 430 | |
| 431 | const text = await response.text(); |
| 432 | let data: { id?: string; message?: string; } | null = null; |
| 433 | try { |
| 434 | data = text ? JSON.parse(text) : null; |
| 435 | } catch { |
| 436 | data = null; |
| 437 | } |
| 438 | |
| 439 | if (!response.ok) { |
| 440 | return { success: false, error: data?.message || `Upload failed: ${response.status} ${text}` }; |
| 441 | } |
| 442 | |
| 443 | if (!data?.id) { |
| 444 | return { success: false, error: data?.message || "No URL returned from upload" }; |
| 445 | } |
| 446 | |
| 447 | return { success: true, url: `https://pixeldrain.com/u/${data.id}` }; |
| 448 | } catch (e) { |
| 449 | return { success: false, error: e instanceof Error ? e.message : "Unknown error" }; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | function isValidHttpsUrl(url: string): boolean { |
| 454 | try { |