-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
90 lines (69 loc) · 2.6 KB
/
index.js
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
const express = require("express");
const path = require("path");
const fs = require("fs");
const ytdl = require("ytdl-core");
const os = require("os");
const Utils = require("./src/utils");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 4000;
const dataPath = path.join(os.tmpdir(), "data");
// const dataPath = path.join(__dirname, "data");
Utils.createDir(dataPath);
app.use("/data", express.static(dataPath));
app.use(express.static(__dirname + "/public"));
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
/**
* Downloads and converts song with selected bitrate.
* Every download request creates session directory that gets deleted after a minute.
*/
app.get("/download", async (req, res) => {
console.log("download started");
const sessionDir = path.join("data", req.query.sessionID);
const fullSessionDirPath = path.join(dataPath, req.query.sessionID);
setTimeout(() => {
fs.rmSync(sessionDir, { recursive: true });
}, 180 * 1000);
let info = Utils.decodeUrlsInObject(req.query);
info["endpointSongPath"] = path.join(sessionDir, info.filename);
info["songPath"] = path.join(fullSessionDirPath, info.filename);
info["fullSessionDirPath"] = fullSessionDirPath;
await Utils.downloadSong(info, res);
});
/**
* Downloads song info and sends it back to front (thumbnail image, author, song titile).
* Parses youtube title to author and song title.
*/
app.get("/getInfo", async (req, res) => {
const info = await Utils.getInfo(req.query.url);
res.setHeader("Access-Control-Allow-Origin", "*");
res.json(info);
});
app.get("/clearBucket", async (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
if(req.headers.clear_bucket_password != process.env.CLEAR_BUCKET_PASSWORD){
res.json({error: `Wrong password: ${req.headers.clear_bucket_password}`});
return
}
const keys = await Utils.clearBucket()
console.log('Deleted files:', keys)
res.json({'Deleted files': keys});
});
app.get("/bucketItem", async (req, res) => {
const urls = await Utils.getSignedUrlForDownload(req.query.itemBucketPath);
console.log(urls)
res.setHeader("Access-Control-Allow-Origin", "*");
res.json({url: signedUrl});
})
app.get("/getStats", async (req, res) => {
const stats = await Utils.getStats();
res.setHeader("Access-Control-Allow-Origin", "*");
res.json(stats);
});
app.get("/updateVisitStats", async (req, res) => {
const stats = await Utils.updateVisitStats();
res.setHeader("Access-Control-Allow-Origin", "*");
res.json(stats);
});