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

:zap: exit if too large

LI JIAHAO преди 6 години
родител
ревизия
7efda8b9c8
променени са 4 файла, в които са добавени 30 реда и са изтрити 6 реда
  1. 5 0
      dist/command.js
  2. 10 3
      dist/webshot.js
  3. 6 0
      src/command.ts
  4. 9 3
      src/webshot.ts

+ 5 - 0
dist/command.js

@@ -2,6 +2,9 @@
 Object.defineProperty(exports, "__esModule", { value: true });
 const fs = require("fs");
 const path = require("path");
+const log4js = require("log4js");
+const logger = log4js.getLogger('command');
+logger.level = 'info';
 function sub(chat, args, lock, lockfile) {
     if (args.length === 0) {
         return '找不到要订阅的链接。';
@@ -42,6 +45,7 @@ https://twitter.com/rikakomoe/lists/lovelive`;
     });
     if (!flag)
         lock.threads[link].subscribers.push(chat);
+    logger.warn(`chat ${JSON.stringify(chat)} has subscribed ${link}`);
     fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
     return `已为此聊天订阅 ${link}`;
 }
@@ -63,6 +67,7 @@ function unsub(chat, args, lock, lockfile) {
     });
     if (flag) {
         fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
+        logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
         return `已为此聊天退订 ${link}`;
     }
     return '您没有订阅此链接。\n' + list(chat, args, lock);

+ 10 - 3
dist/webshot.js

@@ -32,7 +32,7 @@ function renderWebshot(url, height) {
                     break;
                 }
             }
-            if (boundary != null) {
+            if (boundary !== null) {
                 logger.info(`found boundary at ${boundary}, cropping image`);
                 this.data = this.data.slice(0, (this.width * boundary) << 2);
                 this.height = boundary;
@@ -41,15 +41,22 @@ function renderWebshot(url, height) {
                     resolve({ data, boundary });
                 });
             }
+            else if (height >= 8 * 1920) {
+                logger.warn(`too large, consider as a bug, returning`);
+                read(this.pack(), 'base64').then(data => {
+                    logger.info(`finished webshot for ${url}`);
+                    resolve({ data, boundary: 0 });
+                });
+            }
             else {
-                logger.warn(`unable to found boundary, try shooting a larger image`);
+                logger.info(`unable to found boundary, try shooting a larger image`);
                 resolve({ data: '', boundary });
             }
         });
     });
     return promise.then(data => {
         if (data.boundary === null)
-            return renderWebshot(url, height * 2);
+            return renderWebshot(url, height + 1920);
         else
             return data.data;
     });

+ 6 - 0
src/command.ts

@@ -1,5 +1,9 @@
 import * as fs from 'fs';
 import * as path from 'path';
+import * as log4js from "log4js";
+
+const logger = log4js.getLogger('command');
+logger.level = 'info';
 
 function sub(chat: IChat, args: string[], lock: ILock, lockfile: string): string {
   if (args.length === 0) {
@@ -35,6 +39,7 @@ https://twitter.com/rikakomoe/lists/lovelive`;
     if (c.chatID === chat.chatID && c.chatType === chat.chatType) flag = true;
   });
   if (!flag) lock.threads[link].subscribers.push(chat);
+  logger.warn(`chat ${JSON.stringify(chat)} has subscribed ${link}`);
   fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
   return `已为此聊天订阅 ${link}`;
 }
@@ -56,6 +61,7 @@ function unsub(chat: IChat, args: string[], lock: ILock, lockfile: string): stri
   });
   if (flag) {
     fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
+    logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed ${link}`);
     return `已为此聊天退订 ${link}`;
   }
   return '您没有订阅此链接。\n' + list(chat, args, lock);

+ 9 - 3
src/webshot.ts

@@ -32,7 +32,7 @@ function renderWebshot(url: string, height: number): Promise<string> {
             break;
           }
         }
-        if (boundary != null) {
+        if (boundary !== null) {
           logger.info(`found boundary at ${boundary}, cropping image`);
           this.data = this.data.slice(0, (this.width * boundary) << 2);
           this.height = boundary;
@@ -40,14 +40,20 @@ function renderWebshot(url: string, height: number): Promise<string> {
             logger.info(`finished webshot for ${url}`);
             resolve({ data, boundary });
           });
+        } else if (height >= 8 * 1920) {
+          logger.warn(`too large, consider as a bug, returning`);
+          read(this.pack(), 'base64').then(data => {
+            logger.info(`finished webshot for ${url}`);
+            resolve({ data, boundary: 0 });
+          });
         } else {
-          logger.warn(`unable to found boundary, try shooting a larger image`);
+          logger.info(`unable to found boundary, try shooting a larger image`);
           resolve({ data: '', boundary });
         }
       });
   });
   return promise.then(data => {
-    if (data.boundary === null) return renderWebshot(url, height * 2);
+    if (data.boundary === null) return renderWebshot(url, height + 1920);
     else return data.data;
   })
 }