From cada8763a94c6698dbe362421b478324c78482fc Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Sat, 24 Jun 2017 17:32:48 -0500 Subject: [PATCH] clean up cdn resources (#1597) * Create Constants.js * Update Constants.js --- src/util/Constants.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/util/Constants.js b/src/util/Constants.js index 9bee754b0..98b359b4c 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -107,6 +107,11 @@ const AllowedImageSizes = [ 2048, ]; +function checkImage({ size, format }) { + if (format && !AllowedImageFormats.includes(format)) throw new Error(`Invalid image format: ${format}`); + if (size && !AllowedImageSizes.includes(size)) throw new RangeError(`Invalid size: ${size}`); +} + exports.Endpoints = { CDN(root) { return { @@ -115,30 +120,27 @@ exports.Endpoints = { DefaultAvatar: number => `${root}/embed/avatars/${number}.png`, Avatar: (userID, hash, format = 'default', size) => { if (format === 'default') format = hash.startsWith('a_') ? 'gif' : 'webp'; - if (!AllowedImageFormats.includes(format)) throw new Error(`Invalid image format: ${format}`); - if (size && !AllowedImageSizes.includes(size)) throw new RangeError(`Invalid size: ${size}`); + checkImage({ size, format }); return `${root}/avatars/${userID}/${hash}.${format}${size ? `?size=${size}` : ''}`; }, - Icon: (guildID, hash, format = 'default', size) => { - if (format === 'default') format = 'webp'; - if (!AllowedImageFormats.includes(format)) throw new Error(`Invalid image format: ${format}`); - if (size && !AllowedImageSizes.includes(size)) throw new RangeError(`Invalid size: ${size}`); + Icon: (guildID, hash, format = 'webp', size) => { + checkImage({ size, format }); return `${root}/icons/${guildID}/${hash}.${format}${size ? `?size=${size}` : ''}`; }, - AppIcon: (clientID, hash, format = 'default', size) => { - if (format === 'default') format = 'webp'; - if (!AllowedImageFormats.includes(format)) throw new Error(`Invalid image format: ${format}`); - if (size && !AllowedImageSizes.includes(size)) throw new RangeError(`Invalid size: ${size}`); + AppIcon: (clientID, hash, format = 'webp', size) => { + checkImage({ size, format }); return `${root}/app-icons/${clientID}/${hash}.${format}${size ? `?size=${size}` : ''}`; }, - Splash: (guildID, hash) => `${root}/splashes/${guildID}/${hash}.jpg`, + Splash: (guildID, hash, format = 'webp', size) => { + checkImage({ size, format }); + return `${root}/splashes/${guildID}/${hash}.${format}${size ? `?size=${size}` : ''}`; + }, }; }, invite: code => `https://discord.gg/${code}`, botGateway: '/gateway/bot', }; - /** * The current status of the client. Here are the available statuses: * - READY