-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-images.ts
74 lines (61 loc) · 1.84 KB
/
upload-images.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as path from "https://deno.land/std@0.113.0/path/mod.ts";
import { getImages } from "./src/getImages.ts";
import config from "./.config.json" with { type: "json" };
uploadImages();
async function uploadImages() {
const images = (await getImages(config.accountId, config.apiToken)).map((o) =>
o.filename
);
// TODO: Check file size. It looks like 6 mb gif fails to upload
const files = (await dir("img", [".gif", ".jpg", ".jpeg", ".png", ".svg"]))
.map((
o,
) => o.path)
.filter(
(p) => !images.includes(p),
);
if (files.length === 0) {
console.log("no images to upload");
return;
} else {
console.log("images to upload", files.length);
}
// TODO: Check possible errors from Cloudflare (likely in the returned object)
for (const file of files) {
console.log("uploading", file);
await upload(config.accountId, config.apiToken, file);
console.log("uploaded", file);
}
}
async function upload(accountId: string, token: string, p: string) {
const form = new FormData();
form.append("file", new Blob([await Deno.readFile(p)]), p);
return fetch(
`https://api.cloudflare.com/client/v4/accounts/${accountId}/images/v1`,
{
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
},
body: form,
},
);
}
type Path = { path: string; name: string };
async function dir(p: string, extensions?: string[]) {
let ret: Path[] = [];
for await (const { name, isDirectory } of Deno.readDir(p)) {
if (isDirectory) {
ret = ret.concat(await dir(path.join(p, name), extensions));
} else {
if (extensions) {
if (extensions.includes(path.extname(name))) {
ret.push({ path: path.join(p, name), name });
}
} else {
ret.push({ path: path.join(p, name), name });
}
}
}
return ret;
}