-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-image.mjs
More file actions
71 lines (62 loc) · 1.92 KB
/
upload-image.mjs
File metadata and controls
71 lines (62 loc) · 1.92 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import FormData from "form-data";
import dotenv from "dotenv";
import fetch from "node-fetch";
dotenv.config();
const API_BASE =
process.env.PAPERLESSPAPER_API_BASE ?? "https://api.memo.wirewire.de/v1";
const API_KEY = process.env.PAPERLESSPAPER_API_KEY;
function usageAndExit() {
// eslint-disable-next-line no-console
console.error(
"Usage:\n" +
" PAPERLESSPAPER_API_KEY=... node upload-image.mjs <paperId> <imagePath>\n\n" +
"Optional:\n" +
" PAPERLESSPAPER_API_BASE=https://api.memo.wirewire.de/v1\n"
);
process.exit(2);
}
export async function uploadPaperImage(paperId, filePath) {
if (!API_KEY) throw new Error("Missing env var: PAPERLESSPAPER_API_KEY");
const filename = path.basename(filePath);
const form = new FormData();
form.append("picture", fs.createReadStream(filePath), {
filename,
contentType: "image/png",
});
const res = await fetch(
`${API_BASE}/papers/uploadSingleImage/${encodeURIComponent(paperId)}`,
{
method: "POST",
headers: {
...form.getHeaders(),
"x-api-key": API_KEY,
},
body: form,
}
);
if (!res.ok) {
throw new Error(`Upload failed (${res.status}): ${await res.text()}`);
}
return res.json();
}
if (import.meta.url === `file://${process.argv[1]}`) {
const [paperId, imagePath] = process.argv.slice(2);
if (!paperId || !imagePath) usageAndExit();
uploadPaperImage(paperId, imagePath)
.then((paper) => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(paper, null, 2));
if (paper?.imageUpdatedAt) {
// eslint-disable-next-line no-console
console.log(`\nimageUpdatedAt: ${paper.imageUpdatedAt}`);
}
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err?.stack || String(err));
process.exit(1);
});
}