feat : update i guess
This commit is contained in:
@@ -22,13 +22,13 @@ export async function POST(request: Request, context: RouteContext<"/api/commiss
|
||||
if (!request.headers.get("content-type")?.startsWith("multipart/form-data;"))
|
||||
throw new HttpError(415, "expected-multipart");
|
||||
return await withUploadSlot(async () => {
|
||||
const form = await boundedBody(request, 6 * 1024 * 1024 + 64 * 1024).formData();
|
||||
const form = await boundedBody(request, 10 * 10 * 1024 * 1024 + 64 * 1024).formData();
|
||||
const value = form.get("text");
|
||||
const body = typeof value === "string" ? value.trim() : "";
|
||||
const image = form.get("image");
|
||||
const images = form.getAll("image");
|
||||
const replyValue = form.get("replyToId");
|
||||
const replyToId = replyValue === null || replyValue === "" ? null : replyValue;
|
||||
if (body.length > 4000 || (image && !(image instanceof File)))
|
||||
if (body.length > 4000 || images.length > 10 || images.some((image) => !(image instanceof File)))
|
||||
throw new HttpError(400, "invalid-message");
|
||||
if (replyToId !== null) {
|
||||
if (typeof replyToId !== "string" || !z.uuid().safeParse(replyToId).success)
|
||||
@@ -37,28 +37,34 @@ export async function POST(request: Request, context: RouteContext<"/api/commiss
|
||||
.where(and(eq(commissionMessages.id, replyToId), eq(commissionMessages.ticketId, id))).limit(1);
|
||||
if (!parent) throw new HttpError(404, "reply-not-found");
|
||||
}
|
||||
if (!body && !image) throw new HttpError(400, "empty-message");
|
||||
let imageObjectKey: string | null = null;
|
||||
if (image instanceof File) {
|
||||
if (image.size === 0 || image.size > 6 * 1024 * 1024 ||
|
||||
!["image/png", "image/jpeg", "image/webp"].includes(image.type))
|
||||
throw new HttpError(415, "invalid-message-image");
|
||||
const bytes = new Uint8Array(await image.arrayBuffer());
|
||||
await inspectImage(bytes, image.type as "image/png" | "image/jpeg" | "image/webp");
|
||||
imageObjectKey = `commission/messages/${crypto.randomUUID()}`;
|
||||
await (await getMediaStorage()).write(imageObjectKey, bytes, { type: image.type, acl: "public-read" });
|
||||
}
|
||||
if (!body && !images.length) throw new HttpError(400, "empty-message");
|
||||
const imageFiles = images as File[];
|
||||
if (imageFiles.some((image) => image.size === 0 || image.size > 10 * 1024 * 1024 ||
|
||||
!["image/png", "image/jpeg", "image/webp"].includes(image.type)))
|
||||
throw new HttpError(415, "invalid-message-image");
|
||||
const imageObjectKeys: string[] = [];
|
||||
let messageId: string;
|
||||
try {
|
||||
const storage = imageFiles.length ? await getMediaStorage() : null;
|
||||
for (const image of imageFiles) {
|
||||
const bytes = new Uint8Array(await image.arrayBuffer());
|
||||
await inspectImage(bytes, image.type as "image/png" | "image/jpeg" | "image/webp");
|
||||
const key = `commission/messages/${crypto.randomUUID()}`;
|
||||
imageObjectKeys.push(key);
|
||||
await storage!.write(key, bytes, { type: image.type, acl: "public-read" });
|
||||
}
|
||||
messageId = await getDb().transaction(async (tx) => {
|
||||
const [message] = await tx.insert(commissionMessages).values({ ticketId: id, authorId: user.id, replyToId,
|
||||
text: body || null, imageObjectKey, imageMimeType: image instanceof File ? image.type : null })
|
||||
text: body || null, imageObjectKeys })
|
||||
.returning({ id: commissionMessages.id });
|
||||
await tx.update(commissionTickets).set({ updatedAt: new Date() }).where(eq(commissionTickets.id, id));
|
||||
return message.id;
|
||||
});
|
||||
} catch (cause) {
|
||||
if (imageObjectKey) await (await getMediaStorage()).delete(imageObjectKey).catch(() => undefined);
|
||||
if (imageObjectKeys.length) {
|
||||
const storage = await getMediaStorage();
|
||||
await Promise.all(imageObjectKeys.map((key) => storage.delete(key).catch(() => undefined)));
|
||||
}
|
||||
throw cause;
|
||||
}
|
||||
const message: CommissionMessageEvent = { messageId, ticketId: id,
|
||||
|
||||
Reference in New Issue
Block a user