-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
49 lines (42 loc) · 1.36 KB
/
server.ts
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
import ytdl, { getInfo, VideoStream } from "ytdl";
import ID from "./id.ts";
import { opine, serveStatic, json } from "opine";
const port = parseInt(Deno.env.get("PORT") || "8080");
const app = opine();
app.use((_, res, next) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
next();
})
app.use(json());
app.get("/", (_, res) => res.redirect("/public/index.html"));
app.use("/public", serveStatic("./dist"));
app.get("/getInfo/:url", async (req, res) => {
const url = req.params["url"];
if (typeof url !== "string") {
return res.setStatus(400).send({ message: "URL is not a string" });
}
try {
res.send(await getInfo(ID(url)));
} catch (e) {
res.setStatus(500).send({ message: e.message });
}
});
app.post("/download/:url", async (req, res) => {
const url = req.params["url"];
const formatInfo: VideoStream["format"] = req.body;
if (typeof url !== "string") {
return res.setStatus(400).send({ message: "URL is not a string" });
}
try {
const stream = await ytdl(ID(url), {
format: formatInfo
});
res.send(stream);
} catch (e) {
res.setStatus(500).send({ message: e.message });
}
});
app.listen({ port }, () => {
console.log(`Listening at ${port}/`);
});