File size: 3,461 Bytes
d145b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const FormData = require("form-data");
const axios = require("axios");
const fs = require('fs');
const qs = require('qs');

const tool = [ 'removebg', 'enhance', 'upscale', 'restore', 'colorize' ];

const pxpic = {
 upload: async (filePath) => {
   const {
    fileTypeFromBuffer: fromBuffer
} = await import( 'file-type')
  const buffer = fs.readFileSync(filePath);
  const { ext, mime } = (await fromBuffer(buffer)) || {};
  const fileName = Date.now() + "." + ext;

  const folder = "uploads";
  const responsej = await axios.post("https://pxpic.com/getSignedUrl", { folder, fileName }, {
    headers: {
      "Content-Type": "application/json",
    },
  });

  const { presignedUrl } = responsej.data;

  await axios.put(presignedUrl, buffer, {
    headers: {
      "Content-Type": mime,
    },
  });

  const cdnDomain = "https://files.fotoenhancer.com/uploads/";
  const sourceFileUrl = cdnDomain + fileName;

  return sourceFileUrl;
  },
  create: async (filePath, tools) => {
    if (!tool.includes(tools)) { 
        return `Pilih salah satu dari tools ini: ${tool.join(', ')}`; 
    }
    const url = await pxpic.upload(filePath);
    let data = qs.stringify({
      'imageUrl': url,
      '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;
  }
}


const domain = {
  efek: "https://effect-api.visual-paradigm.com",
  ai: "https://ai-services.visual-paradigm.com"
}


const paradigm = {
    async rmbg(buffer) {
      
      const { fileTypeFromBuffer } = await import('file-type');
        const fileType = await fileTypeFromBuffer(buffer);

        if (!fileType || !['image/jpeg', 'image/png', 'image/gif'].includes(fileType.mime)) throw new Error("File yang diunggah bukan gambar. Hanya file gambar yang diperbolehkan.");

        const form = new FormData();
        form.append("file", buffer, `anjuc.jpg`);

        const { data } = await axios.post(`${domain.efek}/remove-bg`, form, {
            headers: form.getHeaders(),
            responseType: "arraybuffer"
        }).catch(e => e.response);

        return data;
    },
    async enhance(buffer) {
      const { fileTypeFromBuffer } = await import('file-type');
      
      const fileType = await fileTypeFromBuffer(buffer);

        if (!fileType || !['image/jpeg', 'image/png', 'image/gif'].includes(fileType.mime)) throw new Error("File yang diunggah bukan gambar. Hanya file gambar yang diperbolehkan.");

        const form = new FormData();
        form.append("file", buffer, `${Date.now()}.jpg`);

        const { data } = await axios.post(`${domain.ai}/api/super-resolution/file`, form, {
            headers: form.getHeaders(),
            responseType: "arraybuffer"
        }).catch(e => e.response);

        return data;
    }
};

module.exports = { enhance: paradigm.enhance, pxpic };