Spaces:
Sleeping
Sleeping
| const express = require("express"); | |
| const axios = require("axios"); | |
| const FormData = require("form-data"); | |
| const fs = require("fs"); | |
| const path = require("path"); | |
| const app = express(); | |
| const PORT = process.env.PORT || 7860; | |
| app.use(express.urlencoded({ extended: true })); | |
| app.use(express.json()); | |
| app.post("/upload", async (req, res) => { | |
| const contentType = req.headers["content-type"]; | |
| if (!contentType || !contentType.startsWith("multipart/form-data")) { | |
| return res.status(400).json({ error: "Content-Type must be multipart/form-data" }); | |
| } | |
| const boundary = contentType.split("boundary=")[1]; | |
| let data = Buffer.from([]); | |
| req.on("data", chunk => data = Buffer.concat([data, chunk])); | |
| req.on("end", async () => { | |
| const parts = data.toString().split(`--${boundary}`); | |
| const filePart = parts.find(p => p.includes("filename=")); | |
| if (!filePart) return res.status(400).json({ error: "No file uploaded" }); | |
| const filenameMatch = filePart.match(/filename="(.+?)"/); | |
| const filename = filenameMatch ? filenameMatch[1] : `file_${Date.now()}`; | |
| const ext = path.extname(filename); | |
| const timestamp = Date.now(); | |
| const finalName = `${timestamp}${ext}`; | |
| const contentIndex = filePart.indexOf("\r\n\r\n") + 4; | |
| const fileContent = filePart.substring(contentIndex, filePart.lastIndexOf("\r\n")); | |
| const buffer = Buffer.from(fileContent, "binary"); | |
| const savePath = path.join(__dirname, "uploads", finalName); | |
| if (!fs.existsSync(path.join(__dirname, "uploads"))) fs.mkdirSync(path.join(__dirname, "uploads")); | |
| fs.writeFileSync(savePath, buffer); | |
| const form = new FormData(); | |
| form.append("reqtype", "fileupload"); | |
| form.append("userhash", ""); | |
| form.append("fileToUpload", fs.createReadStream(savePath)); | |
| try { | |
| const uploadResponse = await axios.post("https://catbox.moe/user/api.php", form, { | |
| headers: { | |
| ...form.getHeaders(), | |
| "User-Agent": "Mozilla/5.0", | |
| "Accept": "application/json", | |
| "Accept-Encoding": "gzip, deflate, br, zstd", | |
| "sec-ch-ua-platform": '"Android"', | |
| "cache-control": "no-cache", | |
| "sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"', | |
| "sec-ch-ua-mobile": "?1", | |
| "x-requested-with": "XMLHttpRequest", | |
| "dnt": "1", | |
| "origin": "https://catbox.moe", | |
| "sec-fetch-site": "same-origin", | |
| "sec-fetch-mode": "cors", | |
| "sec-fetch-dest": "empty", | |
| "referer": "https://catbox.moe/", | |
| "accept-language": "en-US,en;q=0.9", | |
| "priority": "u=1, i" | |
| } | |
| }); | |
| fs.unlinkSync(savePath); | |
| res.json({ fileUrl: uploadResponse.data }); | |
| } catch (err) { | |
| res.status(500).json({ error: "Failed to upload to Catbox", details: err.message }); | |
| } | |
| }); | |
| }); | |
| app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); |