This commit is contained in:
Your Name committed 2026-06-15 01:51:38 -04:00
1 parent 7cb65bf5e7
commit 512a988d17
21 files changed
+516 -159

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
import { Elysia } from "elysia";
import prisma from "../lib/prisma";
export const mediaRoute = new Elysia({ prefix: "/media" })
.get("/", async () => {
const media = await prisma.media.findMany({
orderBy: { createdAt: "desc" }
});
const BACKEND_PUBLIC_URL = process.env.BACKEND_PUBLIC_URL || "http://localhost:3051";
return media.map(m => ({
...m,
url: `${BACKEND_PUBLIC_URL}${m.path}`
}));
})
.delete("/:id", async ({ params, set }) => {
try {
const mediaId = parseInt(params.id);
const media = await prisma.media.findUnique({
where: { id: mediaId }
});
if (!media) {
set.status = 404;
return { error: "Media not found" };
}
await prisma.media.delete({
where: { id: mediaId }
});
// We are not removing the physical file right now, but could be added later if needed.
return { success: true, message: "Media deleted successfully" };
} catch (err) {
console.error("[Media] Delete Error:", err);
set.status = 500;
return { error: "Failed to delete media" };
}
});
+18 -3
View File
@@ -1,6 +1,7 @@
import { Elysia, t } from "elysia";
import { join } from "path";
import { mkdir } from "fs/promises";
import prisma from "../lib/prisma";
export const uploadRoute = new Elysia({ prefix: "/upload" })
.post("/", async ({ body, set }) => {
@@ -32,15 +33,29 @@ export const uploadRoute = new Elysia({ prefix: "/upload" })
// Write file using Bun.write
await Bun.write(filePath, await file.arrayBuffer());
const APP_URL = process.env.APP_URL || "http://localhost:3000";
const publicUrl = `${APP_URL}/uploads/${filename}`;
const relativePath = `/uploads/${filename}`;
const mimeType = file.type;
const sizeBytes = file.size;
await prisma.media.create({
data: {
filename,
path: relativePath,
mimeType,
sizeBytes,
}
});
const BACKEND_PUBLIC_URL = process.env.BACKEND_PUBLIC_URL || "http://localhost:3051";
const publicUrl = `${BACKEND_PUBLIC_URL}${relativePath}`;
console.log(`[Upload] Success: ${filename} -> ${publicUrl}`);
return {
success: true,
url: publicUrl,
filename
filename,
path: relativePath
};
} catch (err) {
console.error("[Upload] Error:", err);