Closure-RI commited on
Commit
5866d93
·
verified ·
1 Parent(s): 07e590f

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +8 -2
index.js CHANGED
@@ -436,6 +436,10 @@ async function createPDF(imagePaths, instanceID, tempDir) {
436
  const imageExt = path.extname(imagePath).toLowerCase();
437
  const supportedExtensions = ['.jpg', '.jpeg', '.png'];
438
  let convertedImagePath = imagePath;
 
 
 
 
439
 
440
  if (!supportedExtensions.includes(imageExt)) {
441
  convertedImagePath = await convertImage(imagePath);
@@ -539,7 +543,7 @@ async function nhentai(url) {
539
  if (isWebp) {
540
  const convertedImagePath = downloadedImagePath.replace('.webp', '.jpg');
541
  await convertImageV2(downloadedImagePath, convertedImagePath);
542
- fs.unlinkSync(downloadedImagePath); // Hapus file asli (.webp)
543
  imagePaths.push(convertedImagePath);
544
  } else {
545
  imagePaths.push(downloadedImagePath);
@@ -563,10 +567,12 @@ async function nhentai(url) {
563
  }
564
 
565
 
 
566
  async function convertImageV2(inputPath, outputPath) {
567
  try {
568
- await execAsync(`ffmpeg -i "${inputPath}" -q:v 1 "${outputPath}"`);
569
  console.log(`Converted ${inputPath} to ${outputPath}`);
 
570
  } catch (error) {
571
  console.error(`Error converting image: ${error.message}`);
572
  throw new Error(`Failed to convert image ${inputPath} to JPG.`);
 
436
  const imageExt = path.extname(imagePath).toLowerCase();
437
  const supportedExtensions = ['.jpg', '.jpeg', '.png'];
438
  let convertedImagePath = imagePath;
439
+
440
+ if (!fs.existsSync(imagePath) || fs.statSync(imagePath).size === 0) {
441
+ throw new Error(`Invalid or empty image file: ${imagePath}`);
442
+ }
443
 
444
  if (!supportedExtensions.includes(imageExt)) {
445
  convertedImagePath = await convertImage(imagePath);
 
543
  if (isWebp) {
544
  const convertedImagePath = downloadedImagePath.replace('.webp', '.jpg');
545
  await convertImageV2(downloadedImagePath, convertedImagePath);
546
+ fs.unlinkSync(downloadedImagePath); // PERUBAHAN: Hapus file asli .webp
547
  imagePaths.push(convertedImagePath);
548
  } else {
549
  imagePaths.push(downloadedImagePath);
 
567
  }
568
 
569
 
570
+ // PERUBAHAN: Gunakan sharp untuk konversi gambar
571
  async function convertImageV2(inputPath, outputPath) {
572
  try {
573
+ await sharp(inputPath).toFormat('jpeg').toFile(outputPath);
574
  console.log(`Converted ${inputPath} to ${outputPath}`);
575
+ return outputPath;
576
  } catch (error) {
577
  console.error(`Error converting image: ${error.message}`);
578
  throw new Error(`Failed to convert image ${inputPath} to JPG.`);