From a507d16843ab39a08adf3ae18348a6f1db464e40 Mon Sep 17 00:00:00 2001 From: night0721 Date: Sat, 3 Feb 2024 14:14:20 +0000 Subject: [PATCH] add server code --- server/.gitignore | 3 +++ server/package.json | 20 ++++++++++++++++++++ server/src/download.ts | 24 ++++++++++++++++++++++++ server/src/index.ts | 17 +++++++++++++++++ server/tsconfig.json | 11 +++++++++++ 5 files changed, 75 insertions(+) create mode 100644 server/.gitignore create mode 100644 server/package.json create mode 100644 server/src/download.ts create mode 100644 server/src/index.ts create mode 100644 server/tsconfig.json diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..93663d4 --- /dev/null +++ b/server/.gitignore @@ -0,0 +1,3 @@ +package-lock.json +node_modules +dist \ No newline at end of file diff --git a/server/package.json b/server/package.json new file mode 100644 index 0000000..b400b7b --- /dev/null +++ b/server/package.json @@ -0,0 +1,20 @@ +{ + "name": "service-api", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "devDependencies": { + "@types/cors": "^2.8.12", + "@types/express": "^4.17.17", + "typescript": "^4.5.5" + }, + "dependencies": { + "cors": "^2.8.5", + "express": "^4.18.2", + "ytdl-core": "npm:@distube/ytdl-core@^4.11.9" + }, + "scripts": { + "build": "tsc", + "start": "node ./dist/index.js" + } +} diff --git a/server/src/download.ts b/server/src/download.ts new file mode 100644 index 0000000..2cff414 --- /dev/null +++ b/server/src/download.ts @@ -0,0 +1,24 @@ +import { Router } from "express"; +const ytdl = require("ytdl-core"); + +const router = Router(); + +router.post("/download", async (req, res) => { + try { + const url = req.body.url; + const type = req.body.type; + if (!ytdl.validateURL(url)) + return res.status(400).json({ message: "Invalid URL", code: 400 }); + if (type !== "mp3" && type !== "mp4") + return res.status(400).json({ message: "Invalid type", code: 400 }); + res.setHeader("Content-Type", type === "mp3" ? "audio/mpeg" : "video/mp4"); + ytdl(url, { + format: type, + filter: type == "mp4" ? "videoandaudio" : "audioonly", + }).pipe(res); + } catch (e) { + console.log(e); + } +}); + +module.exports = router; diff --git a/server/src/index.ts b/server/src/index.ts new file mode 100644 index 0000000..1d746ab --- /dev/null +++ b/server/src/index.ts @@ -0,0 +1,17 @@ +import express from "express"; +import cors from "cors"; + +const app = express(); +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); +app.use(cors()); + +app.use("/api", require("./download")); + +app.listen(852, () => { + console.log("Server running on port 852"); +}); + +process.on("uncaughtException", err => { + console.log(err); +}); diff --git a/server/tsconfig.json b/server/tsconfig.json new file mode 100644 index 0000000..4e45815 --- /dev/null +++ b/server/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "outDir": "./dist", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true + } +}