UNPKG

jer-api

Version:

Scraper social media downloader & stalk

153 lines (133 loc) 4.1 kB
const axios = require("axios"); const FormData = require("form-data"); const { fromBuffer } = require("file-type"); const fetch = require("node-fetch"); const fakeUserAgent = require("fake-useragent"); // Fungsi untuk membuat FormData const createFormData = (content, fieldName, ext) => { const formData = new FormData(); const filename = `upload.${ext || "bin"}`; // Nama default jika ekstensi tidak ditemukan formData.append(fieldName, content, filename); return formData; }; // Fungsi untuk mendapatkan buffer dari URL const fetchBufferFromUrl = async (url) => { const response = await fetch(url); if (!response.ok) throw new Error("Failed to fetch the image from the URL"); return Buffer.from(await response.arrayBuffer()); }; // Fungsi unggah ke Catbox const Catbox = async (mediaPath) => { try { let buffer; if (mediaPath.startsWith("http")) { const response = await axios.get(mediaPath, { responseType: "arraybuffer", }); buffer = Buffer.from(response.data); } else { if (!fs.existsSync(mediaPath)) { throw new Error("File not found"); } buffer = fs.readFileSync(mediaPath); } const { ext, mime } = (await fromBuffer(buffer)) || {}; if (!ext || !mime) { throw new Error("Failed to determine file type."); } const form = new FormData(); form.append("fileToUpload", buffer, { filename: `tmp.${ext}`, contentType: mime, }); form.append("reqtype", "fileupload"); form.append("userhash", ""); const response = await axios.post("https://catbox.moe/user/api.php", form, { headers: form.getHeaders(), }); return response.data; } catch (error) { console.error("Upload to Catbox failed:", error.message); throw error; // Throw error to handle fallback to Ugu } }; const Supaa = async (mediaPath) => { try { let buffer; // Check if the input is a URL if (mediaPath.startsWith("http")) { const response = await axios.get(mediaPath, { responseType: "arraybuffer", // Get the file content as a buffer }); buffer = Buffer.from(response.data); } else { // If it's a local file, read it from the file system if (!fs.existsSync(mediaPath)) { throw new Error("File not found"); } buffer = fs.readFileSync(mediaPath); // Read file into buffer } const { ext, mime } = await fromBuffer(buffer) || {}; // Determine file type if (!ext || !mime) { throw new Error("Failed to determine file type."); } // Prepare the form data for the upload const form = new FormData(); form.append("file", buffer, { filename: `tmp.${ext}`, contentType: mime, }); // Upload to Supaa API const response = await axios.post("https://i.supa.codes/api/upload", form, { headers: form.getHeaders(), }); return response.data.link; // Return the response from Supaa API } catch (error) { console.error("Upload to Supaa failed:", error.message); throw error; // Throw error for further handling or fallback } }; const Ugu = async (input) => { try { const content = Buffer.isBuffer(input) ? input : await fetchBufferFromUrl(input); const { ext, mime } = (await fromBuffer(content)) || {}; const formData = createFormData(content, "files[]", ext); const response = await fetch("https://uguu.se/upload.php", { method: "POST", body: formData, headers: { "User-Agent": fakeUserAgent(), }, }); if (!response.ok) throw new Error("Failed to upload the file to Uguu"); const files = await response.json(); return files.files[0].url; } catch (error) { console.error("Error uploading to Uguu:", error.message); throw error; } }; const UploadImg = async (mediaPath) => { try { return await Supaa(mediaPath); } catch (error) { return await Ugu(mediaPath); try { return await Catbox(mediaPath); } catch (error) { } } }; module.exports = { UploadImg }