canvas-backend/src/api/upload/upload.route.ts
2025-02-05 15:24:06 +06:00

60 lines
No EOL
1.6 KiB
TypeScript

import { Elysia, t } from "elysia";
import { deletePhoto, getAllPhoto, uploadPhoto } from "./upload.controller";
import { verifyAuth } from "../../middlewares/auth.middlewares";
export const uploadRoutes = new Elysia({
prefix: "/uploads",
tags: ["Uploads"],
detail: {
description: "Routes for uploading and managing photos",
}
}).derive(async ({ cookie }) => {
const authData = await verifyAuth(cookie);
return { authData }; // Inject into context
});
uploadRoutes.post("/add", async ({ body, authData }) => {
if (authData.status !== 200)
return authData;
else {
const token = authData?.token;
const user_id: String | any = authData?.userId;
const { id: project_id, file } = body;
const response = await uploadPhoto(file, project_id, user_id, token);
return response;
}
}, {
body: t.Object({
file: t.File(),
id: t.String(),
})
});
uploadRoutes.delete("/delete", async ({ query, authData }) => {
if (authData.status !== 200)
return authData;
else {
const token = authData?.token;
const { url } = query;
const response = await deletePhoto(url, token);
return response;
}
}, {
query: t.Object({
url: t.String(),
})
});
uploadRoutes.get("/getAll/:id", async ({ params: { id }, authData }) => {
if (authData.status !== 200)
return authData;
else {
const token = authData?.token;
const response = await getAllPhoto(id, token);
return response;
}
}, {
params: t.Object({
id: t.String()
})
});