forked from patheticGeek/torrent-aio-bot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (78 loc) · 3.03 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
91
92
93
94
95
96
97
98
99
const express = require("express");
const compression = require("compression");
const bodyParser = require("body-parser");
const serveIndex = require("serve-index");
const humanTime = require("./utils/humanTime");
const keepalive = require("./utils/keepalive");
const diskinfo = require("./utils/diskinfo");
const status = require("./utils/status");
const { getFiles, sendFileStream, getAuthURL, getAuthToken } = require("./utils/gdrive");
const search = require("./routes/search");
const details = require("./routes/details");
const torrent = require("./routes/torrent");
const dev = process.env.NODE_ENV !== "production";
const allowWeb = !process.env.DISABLE_WEB;
const PORT = parseInt(process.env.PORT, 10) || 3000;
const server = express();
keepalive();
server.use(compression());
server.use(bodyParser.json());
server.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
next();
});
server.get("/ping", (req, res) => res.send("pong"));
server.get("/logs", (req, res) => res.sendFile("logs.txt", { root: __dirname }));
server.use("/downloads", express.static("downloads"), serveIndex("downloads", { icons: true }));
server.use("/api/v1/drive/folder", async (req, res) => {
const folderId = req.query.id;
res.send(await getFiles(folderId));
});
server.use("/api/v1/drive/file/:id", sendFileStream);
server.use("/api/v1/drive/getAuthURL", (req, res) => {
const CLIENT_ID = req.query.clientId;
const CLIENT_SECRET = req.query.clientSecret;
if (!CLIENT_ID || !CLIENT_SECRET) {
res.send(JSON.stringify({ error: "Client Id and secret are required" }));
} else {
const authURL = getAuthURL(CLIENT_ID, CLIENT_SECRET);
res.send(JSON.stringify({ error: "", authURL }));
}
});
server.use("/api/v1/drive/getAuthToken", async (req, res) => {
const CLIENT_ID = req.query.clientId;
const CLIENT_SECRET = req.query.clientSecret;
const AUTH_CODE = req.query.authCode;
if (!CLIENT_ID || !CLIENT_SECRET || !AUTH_CODE) {
res.send(JSON.stringify({ error: "Client Id and secret and auth code are required" }));
} else {
const token = await getAuthToken(CLIENT_ID, CLIENT_SECRET, AUTH_CODE);
res.send(JSON.stringify({ token, error: "" }));
}
});
server.use("/api/v1/torrent", torrent);
server.use("/api/v1/search", search);
server.use("/api/v1/details", details);
server.get("/api/v1/uptime", async (req, res) => {
res.send({ uptime: humanTime(process.uptime() * 1000) });
});
server.get("/api/v1/diskinfo", async (req, res) => {
const path = req.query.path;
const info = await diskinfo(path);
res.send(info);
});
server.get("/api/v1/status", async (req, res) => {
const currStatus = await status();
res.send(currStatus);
});
if (allowWeb) {
console.log("web allowed");
server.use("/static", express.static("web/build/static"));
server.all("*", (req, res) => res.sendFile("web/build/index.html", { root: __dirname }));
} else {
console.log("web disabled");
}
server.listen(PORT, () => {
console.log(`> Running on http://localhost:${PORT}`);
});