Spaces:
Sleeping
Sleeping
File size: 3,250 Bytes
226df4c 02ba1f4 226df4c | 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 | 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}`)); |