-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
83 lines (71 loc) · 2.55 KB
/
server.ts
File metadata and controls
83 lines (71 loc) · 2.55 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
import express from "express";
import path from "path";
import { createServer as createViteServer } from "vite";
// Setup server and express config
const PORT = 3000;
const app = express();
app.use(express.json({ limit: '10mb' })); // Allows direct base64 uploads
// Map of file attachments stored in-memory
const filesStore = new Map<string, { buffer: Buffer; contentType: string; fileName: string }>();
// REST API endpoints
app.get("/api/health", (req, res) => {
res.json({ status: "ok" });
});
// Handle custom in-memory file image/attachment upload
app.post("/api/upload", (req, res) => {
try {
const { fileName, fileType, base64Data } = req.body;
if (!base64Data || !fileType) {
return res.status(400).json({ error: "Missing uploaded data structures" });
}
const cleanBase64 = base64Data.replace(/^data:image\/\w+;base64,/, "");
const buffer = Buffer.from(cleanBase64, 'base64');
const fileId = "file-" + Math.random().toString(36).substring(2, 11);
// Limit image size to ~10MB
if (buffer.length > 10 * 1024 * 1024) {
return res.status(400).json({ error: "File exceeds max capacity of 10MB" });
}
filesStore.set(fileId, {
buffer,
contentType: fileType,
fileName: fileName || "upload"
});
const fileUrl = `/api/files/${fileId}`;
res.json({ fileUrl });
} catch (error) {
console.error("Upload handler failed:", error);
res.status(500).json({ error: "Failed to convert file" });
}
});
// Stream uploaded file back with custom headers
app.get("/api/files/:id", (req, res) => {
const file = filesStore.get(req.params.id);
if (!file) {
return res.status(404).send("Attachment directory mismatch or expired link");
}
res.setHeader("Content-Type", file.contentType);
res.setHeader("Cache-Control", "public, max-age=86400"); // Cache it
res.send(file.buffer);
});
// Setup dev server with Vite, or default static bundle path for output
async function startServer() {
if (process.env.NODE_ENV !== "production") {
// Run with Vite Dev Server Middleware
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
// Production serving static files of optimized build
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running securely on port ${PORT}`);
});
}
startServer();