Update plugins/flux2.js
Browse files- 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
|
| 4 |
-
const
|
| 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)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
let data = await uploadRes.json();
|
| 45 |
|
| 46 |
res.json({
|
| 47 |
author: "Herza",
|
| 48 |
success: true,
|
| 49 |
data: {
|
| 50 |
-
img_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 |
|