Repository navigation
Expand file tree
/
Copy pathattachment.ts
More file actions
108 lines (90 loc) · 2.63 KB
/
Copy pathattachment.ts
File metadata and controls
108 lines (90 loc) · 2.63 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { BeforeRemove, Column, Entity, ManyToOne } from "typeorm";
import { z } from "zod";
import { createLogger } from "../util/log.js";
import { deleteFile } from "../util/storage/index.js";
import { BaseModel } from "./basemodel.js";
import type { Message } from "./message.js";
import { LocalUpload } from "./upload.js";
const Log = createLogger("attachments");
@Entity("attachments")
export class Attachment extends BaseModel {
/** user set name of this attachment */
@Column()
name: string;
/** ID of this attachment in storage provider */
@Column()
hash: string;
/** mime type */
@Column()
type: string;
@Column()
size: number;
@Column({ type: Number, nullable: true })
width: number | null;
@Column({ type: Number, nullable: true })
height: number | null;
@ManyToOne("messages", (obj: Message) => obj.files, { onDelete: "CASCADE" })
message: Message;
public toPublic(): PublicAttachment {
return {
name: this.name,
type: this.type,
hash: this.hash,
size: this.size,
width: this.width,
height: this.height,
};
}
@BeforeRemove()
public on_delete() {
void deleteFile(this.message.channel, this.hash)
.catch((e) => Log.error("Failed to delete attachment", e))
.then(() => {
LocalUpload.delete({
hash: this.hash,
channel: { id: this.message.channel.id },
}).catch(() => Log.error("Failed to delete LocalUpload for attachment"));
});
}
}
export const PublicAttachment = z
.object({
name: z.string(),
hash: z.string(),
type: z.string(),
size: z.number(),
width: z.number().nullable().optional(),
height: z.number().nullable().optional(),
})
.openapi("PublicAttachment");
export type PublicAttachment = z.infer<typeof PublicAttachment>;
export const AttachmentInitRequest = z
.object({
id: z
.string()
.describe(
"Client defined ID for cross referencing attachments to output endpoints. Can be any value. Must be unique",
),
name: z.string().describe("User defined file name"),
md5: z.string(), // md5 of the uploaded image
mime: z.string(), // mime type
size: z.number().describe("Size in bytes"), // bytes
// we trust the client here, but only because we require the md5 hash and size
// that should be good enough
// I'm sure it'll bite me later, though
width: z.number().optional(),
height: z.number().optional(),
})
.array()
.max(5) // TODO: config
.refine((d) => new Set(d.map((x) => x.id)).size === d.length, "Attachment IDs must be unique")
.openapi("AttachmentInitRequest");
export const AttachmentsResponse = z
.array(
z.object({
id: z.string(),
hash: z.string(),
url: z.string(),
}),
)
.openapi("AttachmentInitResponse");