Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // upload.js
- const fs = require('fs').promises;
- const crypto = require('crypto');
- class AceImageUploader {
- constructor() {
- this.baseUrl = 'https://api.aceimg.com/api/upload';
- this.cdnBaseUrl = 'https://cdn.aceimg.com';
- }
- generateVisitorId() {
- return crypto.randomUUID();
- }
- extractFileName(link) {
- const match = link.match(/f=([^&]+)/);
- return match ? match[1] : null;
- }
- async uploadImage(filePath) {
- const fileData = await fs.readFile(filePath);
- const fileName = crypto.randomBytes(8).toString('hex') + '.jpg';
- const visitorId = this.generateVisitorId();
- const boundary = '----WebKitFormBoundary' + crypto.randomBytes(16).toString('hex');
- const body = Buffer.concat([
- Buffer.from(`--${boundary}\r\n`),
- Buffer.from(`Content-Disposition: form-data; name="file"; filename="${fileName}"\r\n`),
- Buffer.from(`Content-Type: image/jpeg\r\n\r\n`),
- fileData,
- Buffer.from(`\r\n--${boundary}--\r\n`)
- ]);
- const headers = {
- 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Mobile Safari/537.36',
- 'Content-Type': `multipart/form-data; boundary=${boundary}`
- };
- const url = `${this.baseUrl}?visitorId=${visitorId}`;
- try {
- const response = await fetch(url, {
- method: 'POST',
- headers: headers,
- body: body
- });
- const result = await response.json();
- if (result.status && result.link) {
- const fileName = this.extractFileName(result.link);
- if (fileName) {
- result.cdnUrl = `${this.cdnBaseUrl}/${fileName}`;
- }
- }
- return result;
- } catch (error) {
- return {
- status: false,
- error: error.message
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment