HerzaJ commited on
Commit
e861a27
·
verified ·
1 Parent(s): 874bd5e

Update plugins/flux2.js

Browse files
Files changed (1) hide show
  1. plugins/flux2.js +17 -18
plugins/flux2.js CHANGED
@@ -1,7 +1,7 @@
1
  const axios = require('axios');
2
  const sharp = require('sharp');
3
- const FormData = require('form-data');
4
- const fetch = require('node-fetch');
5
 
6
  const handler = async (req, res) => {
7
  try {
@@ -27,27 +27,26 @@ const handler = async (req, res) => {
27
  .jpeg({ quality: 90 })
28
  .toBuffer();
29
 
30
- const randomName = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
31
- let bodyForm = new FormData();
32
- bodyForm.append("fileToUpload", jpegBuffer, randomName + ".jpg");
33
- bodyForm.append("reqtype", "fileupload");
34
-
35
- let uploadRes = await fetch("https://cdnme.idnet.my.id/upload", {
36
- method: "POST",
37
- body: bodyForm,
38
- });
39
-
40
- if (!uploadRes.ok) {
41
- throw new Error(`Upload failed: ${uploadRes.status} ${uploadRes.statusText}`);
42
- }
43
-
44
- let data = await uploadRes.json();
45
 
46
  res.json({
47
  author: "Herza",
48
  success: true,
49
  data: {
50
- img_url: data.url
51
  }
52
  });
53
 
 
1
  const axios = require('axios');
2
  const sharp = require('sharp');
3
+ const fs = require('fs');
4
+ const path = require('path');
5
 
6
  const handler = async (req, res) => {
7
  try {
 
27
  .jpeg({ quality: 90 })
28
  .toBuffer();
29
 
30
+ const randomName = `flux-${Math.random().toString(36).substring(2, 15)}.jpg`;
31
+ const tmpPath = path.join('/tmp', randomName);
32
+
33
+ fs.writeFileSync(tmpPath, jpegBuffer);
34
+
35
+ setTimeout(() => {
36
+ if (fs.existsSync(tmpPath)) {
37
+ fs.unlinkSync(tmpPath);
38
+ }
39
+ }, 5 * 60 * 1000);
40
+
41
+ const host = req.headers.host || req.get('host');
42
+ const protocol = req.headers['x-forwarded-proto'] || req.protocol || 'https';
43
+ const imageUrl = `${protocol}://${host}/tmp/${randomName}`;
 
44
 
45
  res.json({
46
  author: "Herza",
47
  success: true,
48
  data: {
49
+ img_url: imageUrl
50
  }
51
  });
52