const axios = require('axios'); const fs = require('fs'); const { fromBuffer } = require('file-type'); const qs = require('qs'); const tool = ['removebg', 'enhance', 'upscale', 'restore', 'colorize']; const pxpic = { upload: async (imageUrl) => { try { // 1. Unduh gambar dari URL const response = await axios.get(imageUrl, { responseType: 'arraybuffer' }); const buffer = Buffer.from(response.data); // 2. Dapatkan ekstensi dan MIME type dari gambar const { ext, mime } = (await fromBuffer(buffer)) || {}; if (!ext) throw new Error('Gagal menentukan tipe file.'); const fileName = Date.now() + "." + ext; const folder = "uploads"; // 3. Dapatkan signed URL untuk upload const responsej = await axios.post("https://pxpic.com/getSignedUrl", { folder, fileName }, { headers: { "Content-Type": "application/json" } }); const { presignedUrl } = responsej.data; // 4. Unggah gambar ke server await axios.put(presignedUrl, buffer, { headers: { "Content-Type": mime } }); // 5. Buat URL yang bisa digunakan untuk pemrosesan const cdnDomain = "https://files.fotoenhancer.com/uploads/"; return cdnDomain + fileName; } catch (error) { console.error('Error saat upload:', error.message); return null; } }, create: async (imageUrl, tools) => { if (!tool.includes(tools)) { return `Pilih salah satu dari tools ini: ${tool.join(', ')}`; } // Upload gambar terlebih dahulu const uploadedUrl = await pxpic.upload(imageUrl); if (!uploadedUrl) { return 'Gagal mengunggah gambar.'; } let data = qs.stringify({ 'imageUrl': uploadedUrl, 'targetFormat': 'png', 'needCompress': 'no', 'imageQuality': '100', 'compressLevel': '6', 'fileOriginalExtension': 'png', 'aiFunction': tools, 'upscalingLevel': '' }); let config = { method: 'POST', url: 'https://pxpic.com/callAiFunction', headers: { 'User-Agent': 'Mozilla/5.0 (Android 10; Mobile; rv:131.0) Gecko/131.0 Firefox/131.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,*/*;q=0.8', 'Content-Type': 'application/x-www-form-urlencoded', 'accept-language': 'id-ID' }, data: data }; const api = await axios.request(config); return api.data; } }; module.exports = { pxpic };