/*** @ Base: https://snapedit.app/ @ Author: Shannz @ Note: Image upscale and remove background. ***/ import axios from 'axios'; import FormData from 'form-data'; import fs from 'fs'; import jwt from 'jsonwebtoken'; import crypto from 'crypto'; const CONFIG = { SECRET_KEY_B64: "HBmQJoIurA0HVLyUaCiFlxF+JJc14eHmZNttilecFGQ=", URL_ENHANCE: 'https://be-prod-web.snapedit.app/api/enhance/v1', URL_RMBG: 'https://be-prod-web.snapedit.app/api/rmbg/v1', HEADERS: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', 'Origin': 'https://snapedit.app', 'Referer': 'https://snapedit.app/', 'Accept': 'application/json' } }; const generateUtils = () => { const secretKeyBuffer = Buffer.from(CONFIG.SECRET_KEY_B64, 'base64'); const now = Math.floor(Date.now() / 1000); const payload = { sub: "ignore", platform: "web", is_pro: false, is_premium: false, pricing_plan: "v2", iat: now, exp: now + 300 }; const token = jwt.sign(payload, secretKeyBuffer, { algorithm: 'HS256', header: { typ: 'JWT', alg: 'HS256' } }); const fingerprint = crypto.randomBytes(16).toString('hex'); return { token, fingerprint }; }; export const snapedit = { upscale: async (inputPath) => { if (!fs.existsSync(inputPath)) { return 'mana gambar nya?'; } try { const { token, fingerprint } = generateUtils(); const form = new FormData(); form.append('input_image', fs.createReadStream(inputPath)); form.append('zoom_factor', '2'); form.append('face_model_ids', '0'); const response = await axios.post(CONFIG.URL_ENHANCE, form, { headers: { ...CONFIG.HEADERS, 'Authorization': `Bearer ${token}`, 'x-device-fingerprint': fingerprint, 'x-service': 'ENHANCE_V2', ...form.getHeaders() } }); const json = response.data; if (json.output_images && json.output_images.length > 0) { const buffer = Buffer.from(json.output_images[0], 'base64'); fs.writeFileSync('hasil_upscale.webp', buffer); return 'cek -> hasil_upscale.webp'; } else { console.debug(json); } } catch (error) { console.error(`emror upscale: ${error.message}`); if(error.response) console.error(error.response.data); } }, removebg: async (inputPath) => { if (!fs.existsSync(inputPath)) { return 'mana gambar nya?'; } try { const { token, fingerprint } = generateUtils(); const form = new FormData(); form.append('input_image', fs.createReadStream(inputPath)); const response = await axios.post(CONFIG.URL_RMBG, form, { headers: { ...CONFIG.HEADERS, 'Authorization': `Bearer ${token}`, 'x-device-fingerprint': fingerprint, 'x-service': 'REMOVE_BG', ...form.getHeaders() } }); const json = response.data; if (json.output) { const buffer = Buffer.from(json.output, 'base64'); fs.writeFileSync('hasil_rmbg.png', buffer); return 'cek -> hasil_rmbg.png'; } else { console.debug(json); } } catch (error) { console.error(`emror rmbg: ${error.message}`); if(error.response) console.error(error.response.data); } } };