CoolFace
Apppublic

Jonell01/SpotifyDL-Py-CC

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
index.js76 linesDownload Raw Back to root
1const express = require("express");2const axios = require("axios");3const FormData = require("form-data");4const fs = require("fs");5const path = require("path");6 7const app = express();8const PORT = process.env.PORT || 7860;9 10app.use(express.urlencoded({ extended: true }));11app.use(express.json());12 13app.post("/upload", async (req, res) => {14    const contentType = req.headers["content-type"];15    if (!contentType || !contentType.startsWith("multipart/form-data")) {16        return res.status(400).json({ error: "Content-Type must be multipart/form-data" });17    }18 19    const boundary = contentType.split("boundary=")[1];20    let data = Buffer.from([]);21    req.on("data", chunk => data = Buffer.concat([data, chunk]));22    req.on("end", async () => {23        const parts = data.toString().split(`--${boundary}`);24        const filePart = parts.find(p => p.includes("filename="));25        if (!filePart) return res.status(400).json({ error: "No file uploaded" });26 27        const filenameMatch = filePart.match(/filename="(.+?)"/);28        const filename = filenameMatch ? filenameMatch[1] : `file_${Date.now()}`;29        const ext = path.extname(filename);30        const timestamp = Date.now();31        const finalName = `${timestamp}${ext}`;32        const contentIndex = filePart.indexOf("\r\n\r\n") + 4;33        const fileContent = filePart.substring(contentIndex, filePart.lastIndexOf("\r\n"));34        const buffer = Buffer.from(fileContent, "binary");35        const savePath = path.join(__dirname, "uploads", finalName);36        if (!fs.existsSync(path.join(__dirname, "uploads"))) fs.mkdirSync(path.join(__dirname, "uploads"));37 38        fs.writeFileSync(savePath, buffer);39 40        const form = new FormData();41        form.append("reqtype", "fileupload");42        form.append("userhash", "");43        form.append("fileToUpload", fs.createReadStream(savePath));44 45        try {46            const uploadResponse = await axios.post("https://catbox.moe/user/api.php", form, {47                headers: {48                    ...form.getHeaders(),49                    "User-Agent": "Mozilla/5.0",50                    "Accept": "application/json",51                    "Accept-Encoding": "gzip, deflate, br, zstd",52                    "sec-ch-ua-platform": '"Android"',53                    "cache-control": "no-cache",54                    "sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',55                    "sec-ch-ua-mobile": "?1",56                    "x-requested-with": "XMLHttpRequest",57                    "dnt": "1",58                    "origin": "https://catbox.moe",59                    "sec-fetch-site": "same-origin",60                    "sec-fetch-mode": "cors",61                    "sec-fetch-dest": "empty",62                    "referer": "https://catbox.moe/",63                    "accept-language": "en-US,en;q=0.9",64                    "priority": "u=1, i"65                }66            });67 68            fs.unlinkSync(savePath);69            res.json({ fileUrl: uploadResponse.data });70        } catch (err) {71            res.status(500).json({ error: "Failed to upload to Catbox", details: err.message });72        }73    });74});75 76app.listen(PORT, () => console.log(`Server running on port ${PORT}`));