Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -147,12 +147,15 @@ app.post('/api/toanime', async (req, res) => {
|
|
147 |
res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
148 |
}
|
149 |
})
|
150 |
-
app.post('/api/
|
151 |
try {
|
152 |
console.log(req.body)
|
153 |
-
const { images, format, status } = req.body
|
154 |
if (!images) return res.json({ success: false, message: 'Required an images!' })
|
|
|
|
|
155 |
if (!format) return res.json({ success: false, message: 'Required an format size!' })
|
|
|
156 |
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
157 |
|
158 |
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
@@ -162,11 +165,11 @@ app.post('/api/waifu2x', async (req, res) => {
|
|
162 |
url: images,
|
163 |
responseType: "arraybuffer"
|
164 |
})
|
165 |
-
const response = await
|
166 |
res.setHeader('Content-Type', 'image/jpg')
|
167 |
res.send(response)
|
168 |
} else if (images && typeof images == 'string' && isBase64(images)) {
|
169 |
-
const response = await
|
170 |
res.setHeader('Content-Type', 'image/jpg')
|
171 |
res.send(response)
|
172 |
} else {
|
@@ -390,63 +393,62 @@ function toPDF(urls) {
|
|
390 |
}
|
391 |
})
|
392 |
}
|
393 |
-
async function waifu2x(image, formats) {
|
394 |
-
// data
|
395 |
-
//let img = await axios.get(urls, { responseType: "arraybuffer"})
|
396 |
-
let random_numbers = Math.floor(Math.random() * 1000)
|
397 |
-
let format
|
398 |
-
if(formats == "Medium") {
|
399 |
-
format = "1"
|
400 |
-
} else if(formats == "High") {
|
401 |
-
format = "2"
|
402 |
-
} else if(!formats) {
|
403 |
-
format = "0"
|
404 |
-
}
|
405 |
-
// memasukan data api
|
406 |
-
const formData = new FormData()
|
407 |
-
formData.append("denoise", format)
|
408 |
-
formData.append("scale", "true")
|
409 |
-
formData.append("file", image, {
|
410 |
-
filename: "images_" + random_numbers.toString().padStart(3, '0') + ".jpeg",
|
411 |
-
contentType: "image/jpeg"
|
412 |
-
})
|
413 |
|
414 |
-
|
415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
method: "POST",
|
417 |
-
url: "https://api.alcaamado.es/api/v1/
|
418 |
data: formData,
|
|
|
419 |
headers: {
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
|
|
|
|
|
|
425 |
|
426 |
-
|
427 |
method: "GET",
|
428 |
-
url:
|
|
|
|
|
|
|
|
|
429 |
headers: {
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
|
|
437 |
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
|
446 |
-
},
|
447 |
-
responseType: "arraybuffer"
|
448 |
-
})
|
449 |
-
return images.data
|
450 |
}
|
451 |
async function check_nsfw(buffer) {
|
452 |
let _model = await load_model()
|
|
|
147 |
res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
148 |
}
|
149 |
})
|
150 |
+
app.post('/api/upcaler', async (req, res) => {
|
151 |
try {
|
152 |
console.log(req.body)
|
153 |
+
const { images, denoise, scale, type, format, type, status } = req.body
|
154 |
if (!images) return res.json({ success: false, message: 'Required an images!' })
|
155 |
+
if (!denoise) return res.json({ success: false, message: 'Required an denoise!' })
|
156 |
+
if (!scale) return res.json({ success: false, message: 'Required an images!' })
|
157 |
if (!format) return res.json({ success: false, message: 'Required an format size!' })
|
158 |
+
if (!type) return res.json({ success: false, message: 'Required an images!' })
|
159 |
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
160 |
|
161 |
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
|
|
165 |
url: images,
|
166 |
responseType: "arraybuffer"
|
167 |
})
|
168 |
+
const response = await processImage(data_img.data, denoise, scale, type, format, type)
|
169 |
res.setHeader('Content-Type', 'image/jpg')
|
170 |
res.send(response)
|
171 |
} else if (images && typeof images == 'string' && isBase64(images)) {
|
172 |
+
const response = await processImage(Buffer.from(images, "base64"), denoise, scale, type, format, type)
|
173 |
res.setHeader('Content-Type', 'image/jpg')
|
174 |
res.send(response)
|
175 |
} else {
|
|
|
393 |
}
|
394 |
})
|
395 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
396 |
|
397 |
+
async function processImage(image, denoise, scale, format, type) {
|
398 |
+
return new Promise(async (resolve, reject) => {
|
399 |
+
try {
|
400 |
+
let random_numbers = Math.floor(Math.random() * 1000);
|
401 |
+
|
402 |
+
const formData = new FormData();
|
403 |
+
formData.append("denoise", denoise);
|
404 |
+
formData.append("scale", scale);
|
405 |
+
formData.append("format", format);
|
406 |
+
formData.append("type", type);
|
407 |
+
formData.append("file", fs.readFileSync(image), {
|
408 |
+
filename:
|
409 |
+
"images_" + random_numbers.toString().padStart(3, "0") + ".jpeg",
|
410 |
+
contentType: "image/jpeg",
|
411 |
+
});
|
412 |
+
|
413 |
+
const response = await axios.request({
|
414 |
method: "POST",
|
415 |
+
url: "https://api.alcaamado.es/ns-api-waifu2x/v1/convert",
|
416 |
data: formData,
|
417 |
+
debug: true,
|
418 |
headers: {
|
419 |
+
Authority: "api.alcaamado.es",
|
420 |
+
Accept: "application/json",
|
421 |
+
Referer: "https://waifu2x.pro/",
|
422 |
+
Origin: "https://waifu2x.pro",
|
423 |
+
"User-Agent":
|
424 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
|
425 |
+
},
|
426 |
+
});
|
427 |
|
428 |
+
const images = await axios.request({
|
429 |
method: "GET",
|
430 |
+
url:
|
431 |
+
"https://api.alcaamado.es/api/v2/waifu2x/get?hash=" +
|
432 |
+
response.data.hash +
|
433 |
+
"&type=" +
|
434 |
+
format,
|
435 |
headers: {
|
436 |
+
Accept: "image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
|
437 |
+
"Content-Type": "image/jpg",
|
438 |
+
Referer: "https://waifu2x.pro/",
|
439 |
+
"User-Agent":
|
440 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
|
441 |
+
},
|
442 |
+
responseType: "arraybuffer",
|
443 |
+
});
|
444 |
|
445 |
+
// Mengonversi arraybuffer ke Buffer
|
446 |
+
//const buffer = Buffer.from(images.data);
|
447 |
+
resolve(images.data);
|
448 |
+
} catch (error) {
|
449 |
+
reject(error);
|
450 |
+
}
|
451 |
+
});
|
|
|
|
|
|
|
|
|
|
|
452 |
}
|
453 |
async function check_nsfw(buffer) {
|
454 |
let _model = await load_model()
|