rossimo / retrobot

MIT License
281 stars 16 forks source link

Add support for compressed files #32

Closed AlexGustafsson closed 2 years ago

AlexGustafsson commented 2 years ago

Discord only allows small files if you're not a Nitro user.

One workaround is to support files compressed using gunzip or something else.

An example solution, which worked for me to continue testing this tool. But it's obviously too limited to merge as is, hence no PR.

diff --git a/src/index.ts b/src/index.ts
index 62436f0..ff78bfe 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -12,6 +12,7 @@ import {
     ComponentType, MessageActionRowComponentBuilder, GatewayIntentBits, Interaction, Message,
     PermissionsBitField, TextChannel, MessageOptions, SlashCommandBuilder
 } from 'discord.js';
+import { unzipSync } from "node:zlib";

 import { InputState } from './util';
 import { CoreType, emulate } from './emulate';
@@ -21,7 +22,7 @@ import { MAX_WORKERS } from './config';
 const NES = ['nes'];
 const SNES = ['sfc', 'smc'];
 const GB = ['gb', 'gbc'];
-const GBA = ['gba'];
+const GBA = ['gba', 'gba.gz'];

 const ALL = [...NES, ...SNES, ...GB, ...GBA];

@@ -70,18 +71,24 @@ const main = async () => {
         message.channel.sendTyping();

         const { body } = await request(attachment.url);
-        const buffer = Buffer.from(await body.arrayBuffer());
+        let buffer = Buffer.from(await body.arrayBuffer());

         const id = uuid().slice(0, 5);

         const data = path.resolve('data', id);
         shelljs.mkdir('-p', data);

-        const gameFile = path.join(data, attachment.name);
+        
+        const isGunzip = endsWith(toLower(attachment.name), "gz");
+        const gameName = isGunzip ? attachment.name.replace(/\.gz$/, '') : attachment.name;
+        const gameFile = path.join(data, gameName);
+        if (isGunzip) {
+            buffer = unzipSync(buffer)
+        }
         fs.writeFileSync(gameFile, buffer);

         const info: GameInfo = {
-            game: attachment.name,
+            game: gameFile,
             coreType,
             guild: message.guildId,
             channelId: message.channelId,
rossimo commented 2 years ago

Fixed with c0a08dc8d72c412fb4d5767e19f03f0dcbf8067f