|
| 1 | +import {requestUrl} from "obsidian"; |
| 2 | +import ApiError from "../apiError"; |
| 3 | +import ImageUploader from "../imageUploader"; |
| 4 | + |
| 5 | +const GYAZO_UPLOAD_URL = "https://upload.gyazo.com/api/upload"; |
| 6 | + |
| 7 | +export interface GyazoSetting { |
| 8 | + accessToken: string; |
| 9 | + accessPolicy: "anyone" | "only_me"; |
| 10 | + desc: string; |
| 11 | +} |
| 12 | + |
| 13 | +interface GyazoUploadResponse { |
| 14 | + url?: string; |
| 15 | + message?: string; |
| 16 | +} |
| 17 | + |
| 18 | +export default class GyazoUploader implements ImageUploader { |
| 19 | + private readonly setting: GyazoSetting; |
| 20 | + |
| 21 | + constructor(setting: GyazoSetting) { |
| 22 | + this.setting = setting; |
| 23 | + } |
| 24 | + |
| 25 | + async upload(image: File, fullPath: string): Promise<string> { |
| 26 | + const boundary = "----ObsidianGyazo" + Date.now().toString(36); |
| 27 | + const body = await this.buildMultipartBody(boundary, image, fullPath); |
| 28 | + const response = await requestUrl({ |
| 29 | + url: GYAZO_UPLOAD_URL, |
| 30 | + method: "POST", |
| 31 | + headers: { |
| 32 | + Authorization: `Bearer ${this.setting.accessToken}`, |
| 33 | + "Content-Type": `multipart/form-data; boundary=${boundary}`, |
| 34 | + }, |
| 35 | + body, |
| 36 | + }); |
| 37 | + |
| 38 | + if (response.status !== 200) { |
| 39 | + const errorData = response.json as GyazoUploadResponse | undefined; |
| 40 | + const message = errorData?.message || response.text || `Gyazo upload failed (${response.status})`; |
| 41 | + throw new ApiError(`Gyazo upload failed (${response.status}): ${message}`); |
| 42 | + } |
| 43 | + |
| 44 | + const url = (response.json as GyazoUploadResponse | undefined)?.url; |
| 45 | + if (!url) { |
| 46 | + throw new ApiError("Gyazo upload succeeded but response missing 'url' field"); |
| 47 | + } |
| 48 | + |
| 49 | + return url; |
| 50 | + } |
| 51 | + |
| 52 | + private async buildMultipartBody(boundary: string, image: File, fullPath: string): Promise<ArrayBuffer> { |
| 53 | + const encoder = new TextEncoder(); |
| 54 | + const parts: Uint8Array[] = []; |
| 55 | + const mimeType = image.type || "application/octet-stream"; |
| 56 | + const filename = this.getFilename(image, fullPath); |
| 57 | + |
| 58 | + parts.push(encoder.encode( |
| 59 | + `--${boundary}\r\n` + |
| 60 | + `Content-Disposition: form-data; name="imagedata"; filename="${filename}"\r\n` + |
| 61 | + `Content-Type: ${mimeType}\r\n\r\n` |
| 62 | + )); |
| 63 | + parts.push(new Uint8Array(await image.arrayBuffer())); |
| 64 | + parts.push(encoder.encode("\r\n")); |
| 65 | + |
| 66 | + parts.push(this.encodeField(boundary, "access_policy", this.setting.accessPolicy)); |
| 67 | + |
| 68 | + if (this.setting.desc.trim()) { |
| 69 | + parts.push(this.encodeField(boundary, "desc", this.setting.desc.trim())); |
| 70 | + } |
| 71 | + |
| 72 | + parts.push(encoder.encode(`--${boundary}--\r\n`)); |
| 73 | + |
| 74 | + return this.concatParts(parts).buffer; |
| 75 | + } |
| 76 | + |
| 77 | + private encodeField(boundary: string, name: string, value: string): Uint8Array { |
| 78 | + const encoder = new TextEncoder(); |
| 79 | + return encoder.encode( |
| 80 | + `--${boundary}\r\n` + |
| 81 | + `Content-Disposition: form-data; name="${name}"\r\n\r\n` + |
| 82 | + `${value}\r\n` |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private concatParts(parts: Uint8Array[]): Uint8Array { |
| 87 | + const totalLength = parts.reduce((sum, part) => sum + part.length, 0); |
| 88 | + const merged = new Uint8Array(totalLength); |
| 89 | + let offset = 0; |
| 90 | + |
| 91 | + for (const part of parts) { |
| 92 | + merged.set(part, offset); |
| 93 | + offset += part.length; |
| 94 | + } |
| 95 | + |
| 96 | + return merged; |
| 97 | + } |
| 98 | + |
| 99 | + private getFilename(image: File, fullPath: string): string { |
| 100 | + if (image.name) { |
| 101 | + return image.name; |
| 102 | + } |
| 103 | + |
| 104 | + const pathSegments = fullPath.split(/[\\/]/); |
| 105 | + return pathSegments[pathSegments.length - 1] || "upload.bin"; |
| 106 | + } |
| 107 | +} |
0 commit comments