-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpicture.ts
More file actions
84 lines (71 loc) · 2.35 KB
/
picture.ts
File metadata and controls
84 lines (71 loc) · 2.35 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
72
73
74
75
76
77
78
79
80
81
82
83
84
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { bodyLimit } from "hono/body-limit";
import { z } from "zod";
import * as chat from "../database/chat";
import * as relation from "../database/matches";
import * as storage from "../database/picture";
import { getUserId } from "../firebase/auth/db";
import { getGUID } from "../firebase/auth/lib";
import { compressImage } from "../functions/img/compress";
import { error } from "../lib/error";
import * as hashing from "../lib/hash";
const largeLimit = bodyLimit({
maxSize: 50 * 1024 * 1024, // 50mb
onError: (c) => {
return c.text("overflow :(", 413);
},
});
const router = new Hono()
/* General Pictures in chat */
.post(
"/to/:userId",
zValidator("param", z.object({ userId: z.coerce.number() })),
largeLimit,
async (c) => {
const sender = await getUserId(c);
const recv = c.req.valid("param").userId;
const rel = await relation.getRelation(sender, recv);
if (rel.status !== "MATCHED") error("not matched", 401);
const buf = new Buffer(await c.req.arrayBuffer());
const hash = hashing.sha256(buf.toString("base64"));
const passkey = hashing.sha256(crypto.randomUUID());
return storage.uploadPic(hash, buf, passkey).then(async (url) => {
await chat.createImageMessage(sender, rel.id, url);
return c.text(url);
});
},
)
.get(
"/:id",
zValidator("param", z.object({ id: z.string() })),
zValidator("query", z.object({ key: z.string() })),
async (c) => {
const hash = c.req.valid("param").id;
const key = c.req.valid("query").key;
if (!key) error("key is required", 400);
return storage.getPic(hash, String(key)).then((buf) => {
if (buf) {
return c.body(buf);
}
});
},
)
/* Profile Pictures */
.get(
"/profile/:guid",
zValidator("param", z.object({ guid: z.string() })),
async (c) => {
const guid = c.req.valid("param").guid;
const result = await storage.getProf(guid);
return c.body(result);
},
)
.post("/profile", largeLimit, async (c) => {
const guid = await getGUID(c);
const buf = await compressImage(new Buffer(await c.req.arrayBuffer()));
const url = await storage.setProf(guid, buf);
c.status(201);
return c.text(url);
});
export default router;