mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-21 18:02:33 +01:00
Add detection for gifs when uploading emojis !!UNTESTED!!
Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>
This commit is contained in:
parent
071cf6c5f2
commit
82e7bf9f96
@ -29,8 +29,9 @@ import {
|
||||
User,
|
||||
EmojiCreateSchema,
|
||||
EmojiModifySchema,
|
||||
arrayBufferMatchesArr,
|
||||
} from "@fosscord/util";
|
||||
import { route } from "@fosscord/api";
|
||||
import { route, toByteArray } from "@fosscord/api";
|
||||
|
||||
const router = Router();
|
||||
|
||||
@ -85,6 +86,27 @@ router.post(
|
||||
const user = await User.findOneOrFail({ where: { id: req.user_id } });
|
||||
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({
|
||||
id: id,
|
||||
guild_id: guild_id,
|
||||
@ -92,7 +114,7 @@ router.post(
|
||||
require_colons: body.require_colons ?? undefined, // schema allows nulls, db does not
|
||||
user: user,
|
||||
managed: false,
|
||||
animated: false, // TODO: Add support animated emojis
|
||||
animated,
|
||||
available: true,
|
||||
roles: [],
|
||||
}).save();
|
||||
|
@ -64,3 +64,14 @@ export const bton = (base64: string) => {
|
||||
|
||||
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;
|
||||
};
|
||||
|
@ -19,3 +19,23 @@
|
||||
export function containsAll(arr: any[], target: any[]) {
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user