|
| 1 | +import express from "express"; |
| 2 | +import cors from "cors"; |
| 3 | +import multer from "multer"; |
| 4 | +import { readFile, unlink } from "node:fs/promises"; |
| 5 | +import { MediaLit } from "medialit"; |
| 6 | + |
| 7 | +const app = express(); |
| 8 | +const upload = multer({ dest: "uploads/" }); |
| 9 | +const client = new MediaLit(); |
| 10 | + |
| 11 | +app.use(cors()); |
| 12 | +app.use(express.json()); |
| 13 | + |
| 14 | +app.get("/health", (_req, res) => { |
| 15 | + res.json({ ok: true }); |
| 16 | +}); |
| 17 | + |
| 18 | +// Upload via server |
| 19 | +app.post("/api/medialit/upload", upload.single("file"), async (req, res) => { |
| 20 | + try { |
| 21 | + if (!req.file) { |
| 22 | + return res.status(400).json({ error: "file is required" }); |
| 23 | + } |
| 24 | + const apiKey = process.env.MEDIALIT_API_KEY; |
| 25 | + if (!apiKey) { |
| 26 | + return res |
| 27 | + .status(500) |
| 28 | + .json({ error: "MEDIALIT_API_KEY is missing" }); |
| 29 | + } |
| 30 | + |
| 31 | + const bytes = await readFile(req.file.path); |
| 32 | + const formData = new FormData(); |
| 33 | + formData.append( |
| 34 | + "file", |
| 35 | + new Blob([bytes], { type: req.file.mimetype }), |
| 36 | + req.file.originalname || req.file.filename, |
| 37 | + ); |
| 38 | + formData.append( |
| 39 | + "access", |
| 40 | + req.body.access === "public" ? "public" : "private", |
| 41 | + ); |
| 42 | + if (req.body.caption) { |
| 43 | + formData.append("caption", req.body.caption); |
| 44 | + } |
| 45 | + if (req.body.group) { |
| 46 | + formData.append("group", req.body.group); |
| 47 | + } |
| 48 | + |
| 49 | + const uploadResponse = await fetch(`${client.endpoint}/media/create`, { |
| 50 | + method: "POST", |
| 51 | + headers: { |
| 52 | + "x-medialit-apikey": apiKey, |
| 53 | + }, |
| 54 | + body: formData, |
| 55 | + }); |
| 56 | + const media = await uploadResponse.json(); |
| 57 | + |
| 58 | + if (!uploadResponse.ok) { |
| 59 | + throw new Error(media.error || "Upload failed"); |
| 60 | + } |
| 61 | + if (!media.mediaId) { |
| 62 | + throw new Error("Upload succeeded but mediaId is missing"); |
| 63 | + } |
| 64 | + |
| 65 | + // Seal uploaded media so it persists and appears in list/get APIs. |
| 66 | + const sealedMedia = await client.seal(media.mediaId); |
| 67 | + |
| 68 | + // Cleanup temp file once uploaded. |
| 69 | + await unlink(req.file.path).catch(() => undefined); |
| 70 | + |
| 71 | + return res.json(sealedMedia); |
| 72 | + } catch (error) { |
| 73 | + return res.status(500).json({ |
| 74 | + error: error instanceof Error ? error.message : "Unknown error", |
| 75 | + }); |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +// List media |
| 80 | +app.get("/api/medialit", async (req, res) => { |
| 81 | + try { |
| 82 | + const page = Number.parseInt(String(req.query.page ?? "1"), 10); |
| 83 | + const limit = Number.parseInt(String(req.query.limit ?? "10"), 10); |
| 84 | + const access = req.query.access; |
| 85 | + const group = req.query.group; |
| 86 | + |
| 87 | + const media = await client.list(page, limit, { |
| 88 | + access: |
| 89 | + access === "public" || access === "private" |
| 90 | + ? access |
| 91 | + : undefined, |
| 92 | + group: typeof group === "string" ? group : undefined, |
| 93 | + }); |
| 94 | + |
| 95 | + return res.json(media); |
| 96 | + } catch (error) { |
| 97 | + return res.status(500).json({ |
| 98 | + error: error instanceof Error ? error.message : "Unknown error", |
| 99 | + }); |
| 100 | + } |
| 101 | +}); |
| 102 | + |
| 103 | +// Get media by id |
| 104 | +app.get("/api/medialit/:id", async (req, res) => { |
| 105 | + try { |
| 106 | + const media = await client.get(req.params.id); |
| 107 | + return res.json(media); |
| 108 | + } catch (error) { |
| 109 | + return res.status(500).json({ |
| 110 | + error: error instanceof Error ? error.message : "Unknown error", |
| 111 | + }); |
| 112 | + } |
| 113 | +}); |
| 114 | + |
| 115 | +// Delete media |
| 116 | +app.delete("/api/medialit/:id", async (req, res) => { |
| 117 | + try { |
| 118 | + await client.delete(req.params.id); |
| 119 | + return res.json({ success: true }); |
| 120 | + } catch (error) { |
| 121 | + return res.status(500).json({ |
| 122 | + error: error instanceof Error ? error.message : "Unknown error", |
| 123 | + }); |
| 124 | + } |
| 125 | +}); |
| 126 | + |
| 127 | +// Count media |
| 128 | +app.get("/api/medialit/count", async (_req, res) => { |
| 129 | + try { |
| 130 | + const count = await client.getCount(); |
| 131 | + return res.json({ count }); |
| 132 | + } catch (error) { |
| 133 | + return res.status(500).json({ |
| 134 | + error: error instanceof Error ? error.message : "Unknown error", |
| 135 | + }); |
| 136 | + } |
| 137 | +}); |
| 138 | + |
| 139 | +// Optional: signature for browser direct upload |
| 140 | +app.post("/api/medialit/signature", async (req, res) => { |
| 141 | + try { |
| 142 | + const signature = await client.getSignature({ |
| 143 | + group: req.body.group, |
| 144 | + }); |
| 145 | + |
| 146 | + return res.json({ |
| 147 | + endpoint: client.endpoint, |
| 148 | + signature, |
| 149 | + }); |
| 150 | + } catch (error) { |
| 151 | + return res.status(500).json({ |
| 152 | + error: error instanceof Error ? error.message : "Unknown error", |
| 153 | + }); |
| 154 | + } |
| 155 | +}); |
| 156 | + |
| 157 | +const port = Number(process.env.PORT ?? 4000); |
| 158 | +app.listen(port, () => { |
| 159 | + console.log(`Server running on http://localhost:${port}`); |
| 160 | +}); |
0 commit comments