HerzaJ commited on
Commit
fe2d125
·
verified ·
1 Parent(s): 253bafd

Update plugins/flux.js

Browse files
Files changed (1) hide show
  1. plugins/flux.js +37 -4
plugins/flux.js CHANGED
@@ -1,4 +1,6 @@
1
  const axios = require('axios');
 
 
2
 
3
  async function fluxImage(prompt, width = 1024, height = 1024, server = "NSFW-Core: Uncensored Server 2") {
4
  try {
@@ -41,6 +43,31 @@ async function fluxImage(prompt, width = 1024, height = 1024, server = "NSFW-Cor
41
  }
42
  }
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  const handler = async (req, res) => {
45
  try {
46
  const { prompt, key, width, height, server } = req.query;
@@ -65,9 +92,9 @@ const handler = async (req, res) => {
65
  const imgHeight = height ? parseInt(height) : 1024;
66
  const selectedServer = server || "NSFW-Core: Uncensored Server 2";
67
 
68
- const result = await fluxImage(prompt, imgWidth, imgHeight, selectedServer);
69
 
70
- if (!result) {
71
  return res.status(500).json({
72
  author: "Herza",
73
  success: false,
@@ -75,11 +102,17 @@ const handler = async (req, res) => {
75
  });
76
  }
77
 
 
 
 
 
 
 
78
  res.json({
79
  author: "Herza",
80
  success: true,
81
  data: {
82
- result: result
83
  }
84
  });
85
 
@@ -99,7 +132,7 @@ module.exports = {
99
  routes: ['api/AI/Flux'],
100
  tags: ['ai', 'image', 'flux', 'generator'],
101
  main: ['AI'],
102
- parameters: ['prompt', 'width', 'height', 'server', 'key'],
103
  enabled: true,
104
  handler
105
  };
 
1
  const axios = require('axios');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
 
5
  async function fluxImage(prompt, width = 1024, height = 1024, server = "NSFW-Core: Uncensored Server 2") {
6
  try {
 
43
  }
44
  }
45
 
46
+ async function downloadAndSaveImage(imageUrl, fileId) {
47
+ const tmpDir = path.join(process.cwd(), 'tmp');
48
+
49
+ if (!fs.existsSync(tmpDir)) {
50
+ fs.mkdirSync(tmpDir, { recursive: true });
51
+ }
52
+
53
+ const filePath = path.join(tmpDir, `flux-${fileId}.webp`);
54
+
55
+ const response = await axios.get(imageUrl, {
56
+ responseType: 'arraybuffer',
57
+ timeout: 30000
58
+ });
59
+
60
+ fs.writeFileSync(filePath, Buffer.from(response.data));
61
+
62
+ setTimeout(() => {
63
+ if (fs.existsSync(filePath)) {
64
+ fs.unlinkSync(filePath);
65
+ }
66
+ }, 60000);
67
+
68
+ return filePath;
69
+ }
70
+
71
  const handler = async (req, res) => {
72
  try {
73
  const { prompt, key, width, height, server } = req.query;
 
92
  const imgHeight = height ? parseInt(height) : 1024;
93
  const selectedServer = server || "NSFW-Core: Uncensored Server 2";
94
 
95
+ const imageUrl = await fluxImage(prompt, imgWidth, imgHeight, selectedServer);
96
 
97
+ if (!imageUrl) {
98
  return res.status(500).json({
99
  author: "Herza",
100
  success: false,
 
102
  });
103
  }
104
 
105
+ const fileId = Date.now() + '_' + Math.random().toString(36).substring(7);
106
+ await downloadAndSaveImage(imageUrl, fileId);
107
+
108
+ const baseUrl = `${req.protocol}://${req.get('host')}`;
109
+ const localUrl = `${baseUrl}/tmp/flux-${fileId}.webp`;
110
+
111
  res.json({
112
  author: "Herza",
113
  success: true,
114
  data: {
115
+ result: localUrl
116
  }
117
  });
118
 
 
132
  routes: ['api/AI/Flux'],
133
  tags: ['ai', 'image', 'flux', 'generator'],
134
  main: ['AI'],
135
+ parameters: ['prompt', 'key', 'width', 'height', 'server'],
136
  enabled: true,
137
  handler
138
  };