1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-25 11:43:07 +01:00

Add detection for gifs when uploading emojis !!UNTESTED!!

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>
This commit is contained in:
TheArcaneBrony 2023-01-20 00:44:46 +01:00
parent 071cf6c5f2
commit 82e7bf9f96
No known key found for this signature in database
GPG Key ID: 32FC5AAADAD75A22
3 changed files with 55 additions and 2 deletions

View File

@ -29,8 +29,9 @@ import {
User, User,
EmojiCreateSchema, EmojiCreateSchema,
EmojiModifySchema, EmojiModifySchema,
arrayBufferMatchesArr,
} from "@fosscord/util"; } from "@fosscord/util";
import { route } from "@fosscord/api"; import { route, toByteArray } from "@fosscord/api";
const router = Router(); const router = Router();
@ -85,6 +86,27 @@ router.post(
const user = await User.findOneOrFail({ where: { id: req.user_id } }); const user = await User.findOneOrFail({ where: { id: req.user_id } });
body.image = (await handleFile(`/emojis/${id}`, body.image)) as string; body.image = (await handleFile(`/emojis/${id}`, body.image)) as string;
// Rory - 20/01/2023 - Check for animated emojis
let animated = false;
let buffer = toByteArray(body.image);
if (
arrayBufferMatchesArr(
buffer,
[0x47, 0x49, 0x46, 0x38, 0x39, 0x61],
0,
)
)
animated = true; //gif87
if (
arrayBufferMatchesArr(
buffer,
[0x47, 0x49, 0x46, 0x38, 0x37, 0x61],
0,
)
)
animated = true; //gif89
// TODO: identify more formats
const emoji = await Emoji.create({ const emoji = await Emoji.create({
id: id, id: id,
guild_id: guild_id, guild_id: guild_id,
@ -92,7 +114,7 @@ router.post(
require_colons: body.require_colons ?? undefined, // schema allows nulls, db does not require_colons: body.require_colons ?? undefined, // schema allows nulls, db does not
user: user, user: user,
managed: false, managed: false,
animated: false, // TODO: Add support animated emojis animated,
available: true, available: true,
roles: [], roles: [],
}).save(); }).save();

View File

@ -64,3 +64,14 @@ export const bton = (base64: string) => {
return sign ? -number : number; return sign ? -number : number;
}; };
// Rory - 20/01/2023 - Add utility functions to aid with identification of file types in emojis
export const toByteArray = (str: string) => {
let binary_string = atob(str);
let len = binary_string.length;
let bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes;
};

View File

@ -19,3 +19,23 @@
export function containsAll(arr: any[], target: any[]) { export function containsAll(arr: any[], target: any[]) {
return target.every((v) => arr.includes(v)); return target.every((v) => arr.includes(v));
} }
// Rory - 20/01/2023 - Add utility functions to aid with identification of file types in emojis
export function arrayBufferMatchesArr(
haystack: Uint8Array,
needle: number[],
offset: number,
) {
return arrayBufferMatches(haystack, new Uint8Array(needle), 0);
}
export function arrayBufferMatches(
haystack: Uint8Array,
needle: Uint8Array,
offset: number,
) {
for (let i = 0; i < needle.length; i++) {
if (haystack[i + offset] !== needle[i]) return false;
}
return true;
}