Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -263,7 +263,6 @@ app.get('/nhentai', async (req, res) => {
|
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
-
|
| 267 |
// Fungsi untuk mendownload dari URL
|
| 268 |
async function downloadFromUrl(url, filePath) {
|
| 269 |
const writer = fs.createWriteStream(filePath);
|
|
@@ -441,28 +440,107 @@ app.get('/r34/info', async (req, res) => {
|
|
| 441 |
|
| 442 |
// Endpoint untuk download
|
| 443 |
app.get('/r34/download', async (req, res) => {
|
| 444 |
-
const { url } = req.query;
|
| 445 |
if (!url) {
|
| 446 |
return res.status(400).send('URL is required');
|
| 447 |
}
|
|
|
|
| 448 |
try {
|
| 449 |
-
const result = await Rules34(url);
|
| 450 |
const filePath = result.downloadPath;
|
| 451 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
if (err) {
|
| 453 |
-
|
| 454 |
-
|
| 455 |
} else {
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
|
|
|
|
|
|
| 463 |
}
|
| 464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
} catch (error) {
|
|
|
|
| 466 |
res.status(500).send('Error processing request');
|
| 467 |
}
|
| 468 |
});
|
|
|
|
| 263 |
|
| 264 |
|
| 265 |
|
|
|
|
| 266 |
// Fungsi untuk mendownload dari URL
|
| 267 |
async function downloadFromUrl(url, filePath) {
|
| 268 |
const writer = fs.createWriteStream(filePath);
|
|
|
|
| 440 |
|
| 441 |
// Endpoint untuk download
|
| 442 |
app.get('/r34/download', async (req, res) => {
|
| 443 |
+
const { url, type = 'download' } = req.query;
|
| 444 |
if (!url) {
|
| 445 |
return res.status(400).send('URL is required');
|
| 446 |
}
|
| 447 |
+
|
| 448 |
try {
|
| 449 |
+
const result = await Rules34(url); // Mendapatkan jalur unduhan dari modul Rules34
|
| 450 |
const filePath = result.downloadPath;
|
| 451 |
+
|
| 452 |
+
// Cek apakah file benar-benar ada sebelum mengirim
|
| 453 |
+
if (!fs.existsSync(filePath)) {
|
| 454 |
+
return res.status(404).send('File not found');
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
const fileName = path.basename(filePath);
|
| 458 |
+
const mimeType = 'video/mp4';
|
| 459 |
+
res.setHeader('Content-Type', mimeType);
|
| 460 |
+
|
| 461 |
+
if (type === 'stream') {
|
| 462 |
+
const stat = fs.statSync(filePath);
|
| 463 |
+
const fileSize = stat.size;
|
| 464 |
+
const range = req.headers.range;
|
| 465 |
+
|
| 466 |
+
if (range) {
|
| 467 |
+
const parts = range.replace(/bytes=/, "").split("-");
|
| 468 |
+
const start = parseInt(parts[0], 10);
|
| 469 |
+
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
|
| 470 |
+
|
| 471 |
+
if (start >= fileSize) {
|
| 472 |
+
res.status(416).send('Requested range not satisfiable\n' + start + ' >= ' + fileSize);
|
| 473 |
+
return;
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
const chunksize = (end - start) + 1;
|
| 477 |
+
const file = fs.createReadStream(filePath, { start, end });
|
| 478 |
+
const head = {
|
| 479 |
+
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
| 480 |
+
'Accept-Ranges': 'bytes',
|
| 481 |
+
'Content-Length': chunksize,
|
| 482 |
+
'Content-Type': mimeType,
|
| 483 |
+
};
|
| 484 |
+
|
| 485 |
+
res.writeHead(206, head);
|
| 486 |
+
file.pipe(res).on('finish', () => {
|
| 487 |
+
fs.unlink(filePath, (err) => {
|
| 488 |
+
if (err) {
|
| 489 |
+
console.error(`Error deleting file: ${err.message}`);
|
| 490 |
+
} else {
|
| 491 |
+
console.log(`File deleted: ${filePath}`);
|
| 492 |
+
}
|
| 493 |
+
});
|
| 494 |
+
});
|
| 495 |
+
} else {
|
| 496 |
+
res.setHeader('Content-Length', fileSize);
|
| 497 |
+
const file = fs.createReadStream(filePath);
|
| 498 |
+
file.pipe(res).on('finish', () => {
|
| 499 |
+
fs.unlink(filePath, (err) => {
|
| 500 |
+
if (err) {
|
| 501 |
+
console.error(`Error deleting file: ${err.message}`);
|
| 502 |
+
} else {
|
| 503 |
+
console.log(`File deleted: ${filePath}`);
|
| 504 |
+
}
|
| 505 |
+
});
|
| 506 |
+
});
|
| 507 |
+
}
|
| 508 |
+
} else if (type === 'buffer') {
|
| 509 |
+
fs.readFile(filePath, (err, data) => {
|
| 510 |
if (err) {
|
| 511 |
+
console.error('Gagal membaca file:', err);
|
| 512 |
+
res.status(500).send('Gagal membaca file: \n' + err);
|
| 513 |
} else {
|
| 514 |
+
res.setHeader('Content-Length', data.length);
|
| 515 |
+
res.send(data);
|
| 516 |
+
fs.unlink(filePath, (err) => {
|
| 517 |
+
if (err) {
|
| 518 |
+
console.error(`Error deleting file: ${err.message}`);
|
| 519 |
+
} else {
|
| 520 |
+
console.log(`File deleted: ${filePath}`);
|
| 521 |
+
}
|
| 522 |
+
});
|
| 523 |
}
|
| 524 |
+
});
|
| 525 |
+
} else { // Default to 'download'
|
| 526 |
+
res.setHeader('Content-Type', 'video/mp4');
|
| 527 |
+
res.download(filePath, fileName, (err) => {
|
| 528 |
+
if (err) {
|
| 529 |
+
console.error('Gagal mengirim file:', err);
|
| 530 |
+
res.status(500).send('Gagal mengirim file: \n' + err);
|
| 531 |
+
} else {
|
| 532 |
+
fs.unlink(filePath, (err) => {
|
| 533 |
+
if (err) {
|
| 534 |
+
console.error(`Error deleting file: ${err.message}`);
|
| 535 |
+
} else {
|
| 536 |
+
console.log(`File deleted: ${filePath}`);
|
| 537 |
+
}
|
| 538 |
+
});
|
| 539 |
+
}
|
| 540 |
+
});
|
| 541 |
+
}
|
| 542 |
} catch (error) {
|
| 543 |
+
console.error('Error processing request:', error);
|
| 544 |
res.status(500).send('Error processing request');
|
| 545 |
}
|
| 546 |
});
|