Преглед на файлове

fix accessing non-existent .mka file

Mike L преди 4 години
родител
ревизия
1cb23eaa22
променени са 4 файла, в които са добавени 21 реда и са изтрити 19 реда
  1. 10 8
      dist/gifski.js
  2. 2 2
      dist/webshot.js
  3. 7 7
      src/gifski.ts
  4. 2 2
      src/webshot.ts

+ 10 - 8
dist/gifski.js

@@ -16,7 +16,8 @@ const loggers_1 = require("./loggers");
 const logger = loggers_1.getLogger('gifski');
 const sizeLimit = 10 * Math.pow(2, 20);
 const roundToEven = (n) => Math.ceil(n / 2) * 2;
-const isEmpty = (path) => fs_1.statSync(path).size === 0;
+const safeStatSync = (path) => !fs_1.existsSync(path) ? null : fs_1.statSync(path);
+const isEmptyNullable = (path) => !fs_1.existsSync(path) ? null : fs_1.statSync(path).size === 0;
 function default_1(data, targetWidth) {
     return __awaiter(this, void 0, void 0, function* () {
         const outputFilePath = temp.path({ suffix: '.gif' });
@@ -32,11 +33,11 @@ function default_1(data, targetWidth) {
                 '-vn',
                 inputFile.path + '.mka',
             ]);
-            if (fs_1.statSync(inputFile.path + '.mka').size === 0) {
-                fs_1.unlinkSync(inputFile.path + '.mka');
-            }
-            else {
-                logger.info(`extracted audio to ${inputFile.path + '.mka'}`);
+            switch (isEmptyNullable(inputFile.path + '.mka')) {
+                case true:
+                    fs_1.unlinkSync(inputFile.path + '.mka');
+                    break;
+                case false: logger.info(`extracted audio to ${inputFile.path + '.mka'}`);
             }
             logger.info(`saved video file to ${inputFile.path}, starting gif conversion...`);
             const args = [
@@ -56,7 +57,8 @@ function default_1(data, targetWidth) {
             const gifskiSpawn = child_process_1.spawn('gifski', args);
             const gifskiResult = new Promise((resolve, reject) => {
                 const sizeChecker = setInterval(() => {
-                    if (fs_1.existsSync(outputFilePath) && fs_1.statSync(outputFilePath).size > sizeLimit)
+                    var _a;
+                    if (((_a = safeStatSync(outputFilePath)) === null || _a === void 0 ? void 0 : _a.size) > sizeLimit)
                         gifskiSpawn.kill();
                 }, 5000);
                 gifskiSpawn.on('exit', () => {
@@ -71,7 +73,7 @@ function default_1(data, targetWidth) {
                         '-c', 'copy',
                         outputFilePath + '.mkv',
                     ]);
-                    if (isEmpty(outputFilePath + '.mkv'))
+                    if (isEmptyNullable(outputFilePath + '.mkv'))
                         reject('remux to mkv failed');
                     logger.info(`mkv remuxing succeeded, file path: ${outputFilePath}.mkv`);
                     resolve(fs_1.readFileSync(outputFilePath + '.mkv').buffer);

+ 2 - 2
dist/webshot.js

@@ -381,10 +381,10 @@ class Webshot extends CallableInstance {
                                         '-ar', '24000',
                                         '-',
                                     ], { stdio: 'pipe', maxBuffer: 16 * 1024 * 1024, input: input() });
-                                    if (!imgReturns.stdout)
+                                    if (!imgReturns.stdout.byteLength)
                                         throw Error(imgReturns.stderr.toString());
                                     base64url = `data:image/gif;base64,${imgReturns.stdout.toString('base64')}`;
-                                    if (voiceReturns.stdout) {
+                                    if (voiceReturns.stdout.byteLength) {
                                         logger.info('video has an audio track, trying to convert it to voice...');
                                         temp.track();
                                         const inputFile = temp.openSync();

+ 7 - 7
src/gifski.ts

@@ -8,7 +8,8 @@ const logger = getLogger('gifski');
 
 const sizeLimit = 10 * 2 ** 20;
 const roundToEven = (n: number) => Math.ceil(n / 2) * 2;
-const isEmpty = (path: PathLike) => statSync(path).size === 0;
+const safeStatSync = (path: PathLike) => !existsSync(path) ? null : statSync(path);
+const isEmptyNullable = (path: PathLike) => !existsSync(path) ? null : statSync(path).size === 0;
 
 export default async function (data: ArrayBuffer, targetWidth?: number) {
     const outputFilePath = temp.path({suffix: '.gif'});
@@ -24,10 +25,9 @@ export default async function (data: ArrayBuffer, targetWidth?: number) {
         '-vn',
         inputFile.path + '.mka',
       ]);
-      if (statSync(inputFile.path + '.mka').size === 0) {
-        unlinkSync(inputFile.path + '.mka');
-      } else {
-        logger.info(`extracted audio to ${inputFile.path + '.mka'}`);
+      switch (isEmptyNullable(inputFile.path + '.mka')) {
+        case true: unlinkSync(inputFile.path + '.mka'); break;
+        case false: logger.info(`extracted audio to ${inputFile.path + '.mka'}`);
       }
       logger.info(`saved video file to ${inputFile.path}, starting gif conversion...`);
       const args = [
@@ -47,7 +47,7 @@ export default async function (data: ArrayBuffer, targetWidth?: number) {
       const gifskiSpawn = spawn('gifski', args);
       const gifskiResult = new Promise<ArrayBufferLike>((resolve, reject) => {
         const sizeChecker = setInterval(() => {
-          if (existsSync(outputFilePath) && statSync(outputFilePath).size > sizeLimit) gifskiSpawn.kill();
+          if (safeStatSync(outputFilePath)?.size > sizeLimit) gifskiSpawn.kill();
         }, 5000);
         gifskiSpawn.on('exit', () => {
           clearInterval(sizeChecker);
@@ -60,7 +60,7 @@ export default async function (data: ArrayBuffer, targetWidth?: number) {
             '-c', 'copy',
             outputFilePath + '.mkv',
           ]);
-          if (isEmpty(outputFilePath + '.mkv')) reject('remux to mkv failed');
+          if (isEmptyNullable(outputFilePath + '.mkv')) reject('remux to mkv failed');
           logger.info(`mkv remuxing succeeded, file path: ${outputFilePath}.mkv`);
           resolve(readFileSync(outputFilePath + '.mkv').buffer);
         });

+ 2 - 2
src/webshot.ts

@@ -399,9 +399,9 @@ extends CallableInstance<
                     '-ar', '24000',
                     '-',
                   ], {stdio: 'pipe', maxBuffer: 16 * 1024 * 1024, input: input()});
-                  if (!imgReturns.stdout) throw Error(imgReturns.stderr.toString());
+                  if (!imgReturns.stdout.byteLength) throw Error(imgReturns.stderr.toString());
                   base64url = `data:image/gif;base64,${imgReturns.stdout.toString('base64')}`;
-                  if (voiceReturns.stdout) {
+                  if (voiceReturns.stdout.byteLength) {
                     logger.info('video has an audio track, trying to convert it to voice...');
                     temp.track();
                     const inputFile = temp.openSync();