Browse Source

higher resolution for certain GIFs, fix file not found on upload

Mike L 4 years ago
parent
commit
69aa546da2
4 changed files with 16 additions and 15 deletions
  1. 5 5
      dist/mirai.js
  2. 3 3
      dist/webshot.js
  3. 5 4
      src/mirai.ts
  4. 3 3
      src/webshot.ts

+ 5 - 5
dist/mirai.js

@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
 Object.defineProperty(exports, "__esModule", { value: true });
 exports.Message = void 0;
 const axios_1 = require("axios");
+const fs_1 = require("fs");
 const mirai_ts_1 = require("mirai-ts");
 const message_1 = require("mirai-ts/dist/message");
 const temp = require("temp");
@@ -59,11 +60,10 @@ class default_1 {
                 }
                 temp.track();
                 try {
-                    const tempFileStream = temp.createWriteStream();
-                    tempFileStream.write(img.url.split(',')[1], 'base64');
-                    tempFileStream.end();
-                    if (typeof (tempFileStream.path) === 'string')
-                        imgFile = tempFileStream.path;
+                    const tempFile = temp.openSync();
+                    fs_1.writeSync(tempFile.fd, Buffer.from(img.url.split(',')[1], 'base64'));
+                    fs_1.closeSync(tempFile.fd);
+                    imgFile = tempFile.path;
                 }
                 catch (error) {
                     logger.error(error);

+ 3 - 3
dist/webshot.js

@@ -196,10 +196,10 @@ class Webshot extends CallableInstance {
                 case 'png':
                     return { mimetype: 'image/png', data };
                 case 'mp4':
-                    const dims = url.match(/\/(\d+)x(\d+)\//).slice(1).map(Number);
-                    const factor = dims.some(x => x >= 960) ? 0.375 : 0.5;
+                    const [width, height] = url.match(/\/(\d+)x(\d+)\//).slice(1).map(Number);
+                    const factor = width + height > 1600 ? 0.375 : 0.5;
                     try {
-                        return { mimetype: 'image/gif', data: yield gifski_1.default(data, dims[0] * factor) };
+                        return { mimetype: 'image/gif', data: yield gifski_1.default(data, width * factor) };
                     }
                     catch (err) {
                         throw Error(err);

+ 5 - 4
src/mirai.ts

@@ -1,4 +1,5 @@
 import axios from 'axios';
+import { closeSync, writeSync } from 'fs';
 import Mirai, { MessageType } from 'mirai-ts';
 import MiraiMessage from 'mirai-ts/dist/message';
 import * as temp from 'temp';
@@ -66,10 +67,10 @@ export default class {
       }
       temp.track();
       try {
-        const tempFileStream = temp.createWriteStream();
-        tempFileStream.write(img.url.split(',')[1], 'base64');
-        tempFileStream.end();
-        if (typeof(tempFileStream.path) === 'string') imgFile = tempFileStream.path;
+        const tempFile = temp.openSync();
+        writeSync(tempFile.fd, Buffer.from(img.url.split(',')[1], 'base64'));
+        closeSync(tempFile.fd);
+        imgFile = tempFile.path;
       } catch (error) {
         logger.error(error);
       }

+ 3 - 3
src/webshot.ts

@@ -210,10 +210,10 @@ extends CallableInstance<
           case 'png':
             return {mimetype: 'image/png', data};
           case 'mp4':
-            const dims: number[] = url.match(/\/(\d+)x(\d+)\//).slice(1).map(Number);
-            const factor = dims.some(x => x >= 960) ? 0.375 : 0.5;
+            const [ width, height ] = url.match(/\/(\d+)x(\d+)\//).slice(1).map(Number);
+            const factor = width + height > 1600 ? 0.375 : 0.5;
             try {
-              return {mimetype: 'image/gif', data: await gifski(data, dims[0] * factor)};
+              return {mimetype: 'image/gif', data: await gifski(data, width * factor)};
             } catch (err) {
               throw Error(err);
             }