-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemoji-data-setup-app.ts
66 lines (50 loc) · 1.4 KB
/
emoji-data-setup-app.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
import { Hono } from "hono";
import { HTTPException } from "hono/http-exception";
import { parseTarGzip } from "nanotar";
const app = new Hono<{
Bindings: {
EMOJI_DATA: R2Bucket;
};
}>();
app.post(
"/upload",
async (c) => {
const body = await c.req.parseBody();
const file = Array.isArray(body.file) ? body.file[0] : body.file;
if (file == null) {
throw new HTTPException(400, {
message: "No file uploaded",
});
}
if (typeof file === "string") {
throw new HTTPException(400, {
message: "invalid file uploaded",
});
}
const filePrefix = "emoji-data-main/data/";
const tar = await parseTarGzip(await file.arrayBuffer(), {
filter(file) {
if (!file.name.startsWith(filePrefix) || !file.name.endsWith(".json")) {
return false;
}
// remove the file prefix
file.name = file.name.slice(filePrefix.length);
return true;
},
});
const promises = [];
for (const entry of tar) {
if (entry.type !== "file") continue;
const normalizedEntryName = entry.name.replace("./", "");
promises.push(c.env.EMOJI_DATA.put(normalizedEntryName, entry.text));
}
c.executionCtx.waitUntil(
Promise.all(promises),
);
return c.json({
message: "Files uploaded",
});
},
);
console.log("started the emoji data setup app");
export default app;