-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.ts
More file actions
59 lines (49 loc) · 1.94 KB
/
upload.ts
File metadata and controls
59 lines (49 loc) · 1.94 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
import { Express, Request, Response } from "express";
import { storage } from "../storage";
export function registerUploadRoutes(app: Express) {
// File upload route
app.post("/api/upload", async (req: Request, res: Response) => {
try {
// Verify user authentication
const userId = req.session.userId;
if (!userId) {
return res.status(401).json({ success: false, message: "Not authenticated" });
}
const { mode } = req.body;
// In a real implementation, this would handle file uploads to a service like Vercel Blob
// For this example, we'll generate a mock URL that simulates the upload result
const fileId = Math.random().toString(36).substring(2, 15);
const fileName = (req.body.fileName || `document-${Date.now()}`).replace(/\s+/g, '-');
let fileUrl;
if (mode === 'live') {
// Check if user has quota for LIVE mode
const user = await storage.getUser(userId);
if (!user) {
return res.status(404).json({ success: false, message: "User not found" });
}
if (user.tier === "free") {
return res.status(403).json({
success: false,
message: "Free tier does not support LIVE mode uploads"
});
}
// In a real implementation, this would upload to Vercel Blob or similar service
// and return the actual URL
fileUrl = `https://storage.example.com/${userId}/${fileId}/${fileName}`;
} else {
// For PREVIEW mode, generate a temporary URL
fileUrl = `https://preview.storage.example.com/${userId}/${fileId}/${fileName}`;
}
res.json({
success: true,
fileUrl,
mode
});
} catch (error: any) {
res.status(500).json({
success: false,
message: error.message || "An error occurred during file upload"
});
}
});
}