-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
111 lines (95 loc) · 2.93 KB
/
server.js
File metadata and controls
111 lines (95 loc) · 2.93 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict";
import dotenv from 'dotenv';
import express from 'express';
import pgPkg from 'pg';
import cors from 'cors';
dotenv.config(); // Load environment variables from .env
const { Client } = pgPkg;
const app = express();
const PORT = process.env.PORT || 3001;
app.use(cors());
app.use(express.json({ limit: "500mb" }));
// Configure PostgreSQL connection using environment variables
const pgClient = new Client({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
});
pgClient.connect()
.then(() => {
console.log("Connected to PostgreSQL");
})
.catch((error) => {
console.error("Error connecting to PostgreSQL:", error);
});
app.post("/upload", async (req, res) => {
try {
let { fileName, fileType, fileData } = req.body;
if (!fileName || !fileType || !fileData) {
return res.status(400).json({ error: "Missing required file information" });
}
const prefixRegex = /^data:.*;base64,/;
fileData = fileData.replace(prefixRegex, "");
const fileBuffer = Buffer.from(fileData, "base64");
const query = `
INSERT INTO media_files (file_name, file_type, file_date)
VALUES ($1, $2, $3)
RETURNING id;
`;
const values = [fileName, fileType, fileBuffer];
const result = await pgClient.query(query, values);
return res.json({
message: "File stored successfully",
fileId: result.rows[0].id
});
} catch (error) {
console.error("Error during file upload:", error);
return res.status(500).json({ error: "Error storing file" });
}
});
// List all videos
app.get("/videos", async (req, res) => {
try {
const { rows } = await pgClient.query(`
SELECT
id,
file_name AS name,
file_type AS type,
created_date AS uploadedAt
FROM media_files
ORDER BY created_date DESC
`);
res.json(rows);
} catch (err) {
console.error("List videos error:", err);
res.status(500).json({ error: "Could not list videos" });
}
});
// Stream video by ID
app.get("/videos/:id/stream", async (req, res) => {
try {
const id = parseInt(req.params.id, 10);
const { rows } = await pgClient.query(
`SELECT file_type, file_date FROM media_files WHERE id = $1`,
[id]
);
if (!rows.length) {
return res.status(404).send("Not found");
}
const { file_type, file_date } = rows[0];
const buf = file_date;
res.writeHead(200, {
"Content-Type": file_type,
"Content-Length": buf.length,
});
res.end(buf);
} catch (err) {
console.error("Stream error:", err);
res.status(500).send("Stream failed");
}
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Express server listening on port ${PORT}`);
});