Nexchan commited on
Commit
af96034
·
verified ·
1 Parent(s): 9374051

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +56 -19
index.js CHANGED
@@ -11,13 +11,14 @@ import { fileURLToPath } from 'url';
11
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
12
  import puppeteer from "puppeteer"
13
  import sharp from 'sharp';
14
-
15
  const require = createRequire(import.meta.url);
16
  const PORT = process.env.PORT || 7860;
17
  const app = express();
18
  const writeFileAsync = promisify(fs.writeFile);
19
  const fss = fs.promises;
20
-
 
21
 
22
  app.use('/static', express.static(os.tmpdir()));
23
 
@@ -220,18 +221,25 @@ async function komiku_download(url) {
220
  }
221
 
222
  async function downloadImage(image, tempDir, instanceID) {
223
- const response = await axios.get(image.path, {
224
- responseType: 'arraybuffer',
225
- headers: {
226
- 'User-Agent': getRandomUserAgent() || 'Mozilla/5.0 (Linux; Android 6.0.1; SM-N916S Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/106.0.5249.126 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/389.0.0.42.111;]',
227
- 'Referer': image.path,
228
- 'X-Forwarded-For': generateRandomIP()
229
- }
230
- });
231
- const imagePath = path.join(tempDir, `image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpeg`);
232
- await writeFileAsync(imagePath, response.data);
233
-
234
- return imagePath;
 
 
 
 
 
 
 
235
  }
236
 
237
  async function downloadImages(imgList, tempDir, instanceID) {
@@ -244,15 +252,33 @@ async function downloadImages(imgList, tempDir, instanceID) {
244
  }
245
 
246
  async function createPDF(imagePaths, instanceID, tempDir) {
247
- const pdfPath = path.join(os.tmpdir(), `${instanceID}.pdf`);
248
  const doc = new PDFDocument({ autoFirstPage: false });
249
 
250
  doc.pipe(fs.createWriteStream(pdfPath));
251
 
252
  for (const imagePath of imagePaths) {
253
- const { width, height } = await getImageDimensions(imagePath);
254
- doc.addPage({ size: [width, height] });
255
- doc.image(imagePath, 0, 0, { width: width, height: height });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  }
257
 
258
  doc.end();
@@ -261,11 +287,22 @@ async function createPDF(imagePaths, instanceID, tempDir) {
261
  }
262
 
263
  async function getImageDimensions(imagePath) {
264
- const sizeOf = promisify(require('image-size'));
265
  const dimensions = await sizeOf(imagePath);
266
  return dimensions;
267
  }
268
 
 
 
 
 
 
 
 
 
 
 
 
 
269
  app.get('/download', async (req, res) => {
270
  const { url } = req.query;
271
  if (!url) {
 
11
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
12
  import puppeteer from "puppeteer"
13
  import sharp from 'sharp';
14
+ import { fileTypeFromBuffer } from 'file-type';
15
  const require = createRequire(import.meta.url);
16
  const PORT = process.env.PORT || 7860;
17
  const app = express();
18
  const writeFileAsync = promisify(fs.writeFile);
19
  const fss = fs.promises;
20
+ const sizeOf = promisify(require('image-size'));
21
+ const { exec } = require('child_process');
22
 
23
  app.use('/static', express.static(os.tmpdir()));
24
 
 
221
  }
222
 
223
  async function downloadImage(image, tempDir, instanceID) {
224
+ try {
225
+ // Fetch the image data
226
+ const response = await axios.get(image.path, {
227
+ responseType: 'arraybuffer',
228
+ headers: {
229
+ 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0.1; SM-N916S Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/106.0.5249.126 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/389.0.0.42.111;]',
230
+ 'Referer': image.path,
231
+ }
232
+ });
233
+ const buffer = Buffer.from(response.data, 'binary');
234
+ const mimesd = await fileTypeFromBuffer(buffer);
235
+ const ext = mimesd ? mimesd.ext : 'jpeg';
236
+ const imagePath = `${tempDir}/image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.${ext}`;
237
+ await writeFileAsync(imagePath, buffer);
238
+ return imagePath;
239
+ } catch (error) {
240
+ console.error('Error downloading image:', error);
241
+ throw error;
242
+ }
243
  }
244
 
245
  async function downloadImages(imgList, tempDir, instanceID) {
 
252
  }
253
 
254
  async function createPDF(imagePaths, instanceID, tempDir) {
255
+ const pdfPath = path.join(tempDir, `${instanceID}.pdf`);
256
  const doc = new PDFDocument({ autoFirstPage: false });
257
 
258
  doc.pipe(fs.createWriteStream(pdfPath));
259
 
260
  for (const imagePath of imagePaths) {
261
+ try {
262
+ // Check if image needs conversion
263
+ const imageExt = path.extname(imagePath).toLowerCase();
264
+ const supportedExtensions = ['.jpg', '.jpeg', '.png'];
265
+ let convertedImagePath = imagePath;
266
+
267
+ if (!supportedExtensions.includes(imageExt)) {
268
+ convertedImagePath = await convertImage(imagePath);
269
+ }
270
+
271
+ const { width, height } = await getImageDimensions(convertedImagePath);
272
+ doc.addPage({ size: [width, height] });
273
+ doc.image(convertedImagePath, 0, 0, { width: width, height: height });
274
+
275
+ // Remove converted images if they were created
276
+ if (convertedImagePath !== imagePath) {
277
+ fs.unlinkSync(convertedImagePath);
278
+ }
279
+ } catch (error) {
280
+ console.error(`Error processing image ${imagePath}:`, error.message);
281
+ }
282
  }
283
 
284
  doc.end();
 
287
  }
288
 
289
  async function getImageDimensions(imagePath) {
 
290
  const dimensions = await sizeOf(imagePath);
291
  return dimensions;
292
  }
293
 
294
+ async function convertImage(imagePath) {
295
+ return new Promise((resolve, reject) => {
296
+ const outputImagePath = imagePath.replace(/\.\w+$/, '.jpg');
297
+ exec(`convert "${imagePath}" "${outputImagePath}"`, (error, stdout, stderr) => {
298
+ if (error) {
299
+ console.error('ImageMagick Error:', stderr);
300
+ return reject(error);
301
+ }
302
+ resolve(outputImagePath);
303
+ });
304
+ });
305
+
306
  app.get('/download', async (req, res) => {
307
  const { url } = req.query;
308
  if (!url) {