From ecb4281d1e2d9a0a427605f75352cbf74ffb2d7c Mon Sep 17 00:00:00 2001 From: DD Date: Fri, 6 Jan 2023 17:23:11 +0200 Subject: [PATCH] fix(RequestManager): inference of image/apng (#9014) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/rest/src/lib/RequestManager.ts | 11 +++++++++-- packages/rest/src/lib/utils/constants.ts | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/rest/src/lib/RequestManager.ts b/packages/rest/src/lib/RequestManager.ts index 18d03ae3d657..24853ad8cf0f 100644 --- a/packages/rest/src/lib/RequestManager.ts +++ b/packages/rest/src/lib/RequestManager.ts @@ -9,7 +9,7 @@ import { FormData, type RequestInit, type BodyInit, type Dispatcher, type Agent import type { RESTOptions, RestEvents, RequestOptions } from './REST.js'; import type { IHandler } from './handlers/IHandler.js'; import { SequentialHandler } from './handlers/SequentialHandler.js'; -import { DefaultRestOptions, DefaultUserAgent, RESTEvents } from './utils/constants.js'; +import { DefaultRestOptions, DefaultUserAgent, OverwrittenMimeTypes, RESTEvents } from './utils/constants.js'; import { resolveBody } from './utils/utils.js'; // Make this a lazy dynamic import as file-type is a pure ESM package @@ -419,7 +419,14 @@ export class RequestManager extends EventEmitter { if (Buffer.isBuffer(file.data)) { // Try to infer the content type from the buffer if one isn't passed const { fileTypeFromBuffer } = await getFileType(); - const contentType = file.contentType ?? (await fileTypeFromBuffer(file.data))?.mime; + let contentType = file.contentType; + if (!contentType) { + const parsedType = (await fileTypeFromBuffer(file.data))?.mime; + if (parsedType) { + contentType = OverwrittenMimeTypes[parsedType as keyof typeof OverwrittenMimeTypes] ?? parsedType; + } + } + formData.append(fileKey, new Blob([file.data], { type: contentType }), file.name); } else { formData.append(fileKey, new Blob([`${file.data}`], { type: file.contentType }), file.name); diff --git a/packages/rest/src/lib/utils/constants.ts b/packages/rest/src/lib/utils/constants.ts index 9664242164e1..2285a4b4c6eb 100644 --- a/packages/rest/src/lib/utils/constants.ts +++ b/packages/rest/src/lib/utils/constants.ts @@ -49,3 +49,8 @@ export const ALLOWED_SIZES = [16, 32, 64, 128, 256, 512, 1_024, 2_048, 4_096] as export type ImageExtension = typeof ALLOWED_EXTENSIONS[number]; export type StickerExtension = typeof ALLOWED_STICKER_EXTENSIONS[number]; export type ImageSize = typeof ALLOWED_SIZES[number]; + +export const OverwrittenMimeTypes = { + // https://github.com/discordjs/discord.js/issues/8557 + 'image/apng': 'image/png', +} as const satisfies Readonly>;