Skip to content

Commit 3b5b33b

Browse files
committed
Update Tags
- Update Tags -> Use the server's max attachment size instead of the default 8mb when attaching something
1 parent dc7c778 commit 3b5b33b

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/api/raw.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,7 @@ export async function utilitiesFetchData(
19981998
options: RestOptions.UtilitiesFetchData,
19991999
): Promise<Response> {
20002000
const query = {
2001+
max_file_size: options.maxFileSize,
20012002
url: options.url,
20022003
};
20032004
return request(context, {
@@ -2016,6 +2017,7 @@ export async function utilitiesFetchImage(
20162017
options: RestOptions.UtilitiesFetchImage,
20172018
): Promise<Response> {
20182019
const query = {
2020+
max_file_size: options.maxFileSize,
20192021
url: options.url,
20202022
};
20212023
return request(context, {
@@ -2034,6 +2036,7 @@ export async function utilitiesFetchMedia(
20342036
options: RestOptions.UtilitiesFetchMedia,
20352037
): Promise<Response> {
20362038
const query = {
2039+
max_file_size: options.maxFileSize,
20372040
url: options.url,
20382041
};
20392042
return request(context, {
@@ -2052,6 +2055,7 @@ export async function utilitiesFetchText(
20522055
options: RestOptions.UtilitiesFetchText,
20532056
): Promise<Response> {
20542057
const query = {
2058+
max_file_size: options.maxFileSize,
20552059
url: options.url,
20562060
};
20572061
return request(context, {

src/api/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,22 @@ export namespace RestOptions {
383383

384384

385385
export interface UtilitiesFetchData {
386+
maxFileSize?: number,
386387
url: string,
387388
}
388389

389390
export interface UtilitiesFetchImage {
391+
maxFileSize?: number,
390392
url: string,
391393
}
392394

393395
export interface UtilitiesFetchMedia {
396+
maxFileSize?: number,
394397
url: string,
395398
}
396399

397400
export interface UtilitiesFetchText {
401+
maxFileSize?: number,
398402
url: string,
399403
}
400404

src/utils/tags.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { runInNewContext } from 'vm';
22

33
import { Command, Interaction, Structures } from 'detritus-client';
4+
import { MAX_ATTACHMENT_SIZE } from 'detritus-client/lib/constants';
45
import { Markup } from 'detritus-client/lib/utils';
56

67
import { utilitiesFetchMedia, utilitiesFetchText } from '../api';
@@ -36,6 +37,7 @@ export const ATTACHMENT_EXTENSIONS_IMAGE = [
3637
];
3738

3839
export const ATTACHMENT_EXTENSIONS_MEDIA = [
40+
'flac',
3941
'mov',
4042
'mp3',
4143
'mp4',
@@ -46,6 +48,8 @@ export const ATTACHMENT_EXTENSIONS_MEDIA = [
4648

4749
export const ATTACHMENT_EXTENSIONS = [...ATTACHMENT_EXTENSIONS_IMAGE, ...ATTACHMENT_EXTENSIONS_MEDIA];
4850

51+
export const FILE_SIZE_BUFFER = 10 * 1024; // 10 kb
52+
4953
export const MAX_ITERATIONS = 150;
5054
export const MAX_NETWORK_REQUESTS = 10;
5155
export const MAX_REGEX_TIME = 25;
@@ -63,6 +67,7 @@ export const REGEX_ARGUMENT_SPLITTER_ESCAPE_REPLACEMENT = new RegExp(`\\\\\\${Ta
6367

6468

6569
export enum PrivateVariables {
70+
FILE_SIZE = '__fileSize',
6671
ITERATIONS_REMAINING = '__iterationsRemaining',
6772
NETWORK_REQUESTS = '__networkRequests',
6873
}
@@ -234,6 +239,7 @@ export const TagFunctionsToString = Object.freeze({
234239

235240

236241
export interface TagVariables {
242+
[PrivateVariables.FILE_SIZE]: number,
237243
[PrivateVariables.ITERATIONS_REMAINING]: number,
238244
[PrivateVariables.NETWORK_REQUESTS]: number,
239245
[key: string]: number | string,
@@ -260,6 +266,9 @@ export async function parse(
260266
if (!(PrivateVariables.NETWORK_REQUESTS in variables)) {
261267
variables[PrivateVariables.NETWORK_REQUESTS] = 0;
262268
}
269+
if (!(PrivateVariables.FILE_SIZE in variables)) {
270+
variables[PrivateVariables.FILE_SIZE] = 0;
271+
}
263272
const tag: TagResult = {files: [], text: '', variables};
264273
tag.variables[PrivateVariables.ITERATIONS_REMAINING]--;
265274

@@ -491,14 +500,21 @@ const ScriptTags = Object.freeze({
491500

492501
tag.variables[PrivateVariables.NETWORK_REQUESTS]++;
493502
try {
494-
const response = await utilitiesFetchMedia(context, {url});
503+
const maxFileSize = ((context.guild) ? context.guild.maxAttachmentSize : MAX_ATTACHMENT_SIZE) - FILE_SIZE_BUFFER;
504+
const response = await utilitiesFetchMedia(context, {maxFileSize, url});
495505
const filename = (response.headers.get('content-disposition') || '').split(';').pop()!.split('filename=').pop()!.slice(1, -1) || 'unknown.lmao';
496506

497507
let data: Buffer | string = await response.buffer();
498508
if ((response.headers.get('content-type') || '').startsWith('text/')) {
499509
data = data.toString();
500510
}
501511

512+
const currentFileSize = tag.variables[PrivateVariables.FILE_SIZE];
513+
if (maxFileSize <= currentFileSize + data.length) {
514+
throw new Error(`Attachments surpassed max file size of ${maxFileSize} bytes`);
515+
}
516+
tag.variables[PrivateVariables.FILE_SIZE] += data.length;
517+
502518
tag.files.push({
503519
buffer: data,
504520
filename,
@@ -658,7 +674,8 @@ const ScriptTags = Object.freeze({
658674
tag.variables[PrivateVariables.NETWORK_REQUESTS]++;
659675

660676
try {
661-
const response = await utilitiesFetchText(context, {url});
677+
const maxFileSize = (context.guild) ? context.guild.maxAttachmentSize : undefined;
678+
const response = await utilitiesFetchText(context, {maxFileSize, url});
662679
tag.text += await response.text();
663680
} catch(error) {
664681
console.log(error);

0 commit comments

Comments
 (0)