forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptpimg.ts
More file actions
47 lines (36 loc) · 1.14 KB
/
Copy pathptpimg.ts
File metadata and controls
47 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// src/lib/image-hosting/ptpimg.ts
import type { ImageHostAdapter, UploadOptions, UploadResult } from "./types"
const UPLOAD_URL = "https://ptpimg.me/upload.php"
export const ptpimgAdapter: ImageHostAdapter = {
id: "ptpimg",
async upload(
fileBuffer: Buffer,
fileName: string,
apiKey: string,
_options?: UploadOptions
): Promise<UploadResult> {
const form = new FormData()
form.set("api_key", apiKey)
form.set("file-upload[]", new Blob([new Uint8Array(fileBuffer)]), fileName)
const response = await fetch(UPLOAD_URL, {
method: "POST",
body: form,
signal: AbortSignal.timeout(30_000),
})
if (!response.ok) {
throw new Error(`PTPImg upload failed (${response.status})`)
}
const data = (await response.json()) as { code: string; ext: string }[]
if (!Array.isArray(data) || data.length === 0) {
throw new Error("PTPImg returned no image data")
}
const { code, ext } = data[0]
if (!code || !ext) {
throw new Error("PTPImg returned incomplete image data")
}
return {
url: `https://ptpimg.me/${code}.${ext}`,
host: "ptpimg",
}
},
}