-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupastore.js
More file actions
51 lines (44 loc) · 1.56 KB
/
Copy pathsupastore.js
File metadata and controls
51 lines (44 loc) · 1.56 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
const fs = require("fs");
const path = require("path");
const { v4: uuidv4 } = require("uuid");
const UPLOAD_DIR = path.join(__dirname, "uploads");
if (!fs.existsSync(UPLOAD_DIR)) fs.mkdirSync(UPLOAD_DIR, { recursive: true });
class SupaStore {
static async upload(base64Data, mediaType, userId) {
try {
const extMap = { video: "mp4", audio: "webm", gif: "gif", image: "jpg" };
const ext = extMap[mediaType] || "jpg";
const fileId = uuidv4();
const fileName = `${userId}_${fileId}.${ext}`;
const filePath = path.join(UPLOAD_DIR, fileName);
const base64Clean = base64Data.includes(",")
? base64Data.split(",")[1]
: base64Data;
fs.writeFileSync(filePath, Buffer.from(base64Clean, "base64"));
const url = `${process.env.BASE_URL || "http://localhost:5000"}/uploads/${fileName}`;
console.log("✅ SupaStore: Saved locally", fileName);
return { url, fileName };
} catch (err) {
console.error("SupaStore upload error:", err);
throw err;
}
}
static async delete(fileName) {
try {
const filePath = path.join(UPLOAD_DIR, fileName);
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
console.log("🗑️ SupaStore: Deleted", fileName);
} catch (err) {
console.error("SupaStore delete error:", err);
}
}
static async stats() {
try {
const files = fs.readdirSync(UPLOAD_DIR);
return { totalFiles: files.length, storage: "Local" };
} catch {
return { totalFiles: 0, storage: "Local" };
}
}
}
module.exports = SupaStore;