Skip to content

Commit

Permalink
Remove ImageSizes array and fix minimum image size
Browse files Browse the repository at this point in the history
Checking for 2^n and a bounds check is cleaner than hand-writing all the possible sizes

Also changed the image size range to 16-1024. Thanks to @Alpacatty for pointing this out
  • Loading branch information
abalabahaha committed Mar 30, 2018
1 parent 09e420c commit af2619c
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 16 deletions.
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ declare module "eris" {
interface Constants {
DefaultAvatarHashes: string[];
ImageFormats: string[];
ImageSizes: number[];
GatewayOPCodes: {[key: string]: number};
GATEWAY_VERSION: number;
Permissions: {[key: string]: number};
Expand Down
7 changes: 4 additions & 3 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ class Client extends EventEmitter {
this.options.compress = false; // zlib does not like Blobs, Pako is not here
}
if(!~Constants.ImageFormats.indexOf(this.options.defaultImageFormat.toLowerCase())) {
this.options.defaultImageFormat = "jpg";
throw new TypeError("Invalid default image format: " + this.options.defaultImageFormat);
}
if(!~Constants.ImageSizes.indexOf(this.options.defaultImageSize)) {
this.options.defaultImageSize = 128;
var defaultImageSize = this.options.defaultImageSize;
if(defaultImageSize < 16 || defaultImageSize > 1024 || (defaultImageSize & (defaultImageSize - 1))) {
throw new TypeError("Invalid default image size: " + defaultImageSize);
}

this.token = token;
Expand Down
8 changes: 0 additions & 8 deletions lib/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ module.exports.ImageFormats = [
"gif"
];

module.exports.ImageSizes = [
128,
256,
512,
1024,
2048
];

module.exports.GatewayOPCodes = {
EVENT: 0,
HEARTBEAT: 1,
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/GroupChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class GroupChannel extends PrivateChannel { // (╯°□°)╯︵ ┻━┻
if(!format || !~Constants.ImageFormats.indexOf(format.toLowerCase())) {
format = this._client.options.defaultImageFormat;
}
if(!size || !~Constants.ImageSizes.indexOf(size)) {
if(size < 16 || size > 1024 || (size & (size - 1))) {
size = this._client.options.defaultImageSize;
}
return this.icon ? `${Endpoints.CDN_URL}/channel-icons/${this.id}/${this.icon}.${format}?size=${size}` : null;
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class Guild extends Base {
if(!format || !~Constants.ImageFormats.indexOf(format.toLowerCase())) {
format = this.shard.client.options.defaultImageFormat;
}
if(!size || !~Constants.ImageSizes.indexOf(size)) {
if(size < 16 || size > 1024 || (size & (size - 1))) {
size = this.shard.client.options.defaultImageSize;
}
return this.icon ? `${CDN_URL}/icons/${this.id}/${this.icon}.${format}?size=${size}` : null;
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ class User extends Base {
if (!this.avatar) {
return this.defaultAvatarURL;
}

if(!format || !~Constants.ImageFormats.indexOf(format.toLowerCase())) {
format = this.avatar.startsWith("a_") ? "gif" : this._client.options.defaultImageFormat;
}
if(!size || !~Constants.ImageSizes.indexOf(size)) {
if(size < 16 || size > 1024 || (size & (size - 1))) {
size = this._client.options.defaultImageSize;
}
return `${CDN_URL}/avatars/${this.id}/${this.avatar}.${format}?size=${size}`;
Expand Down

0 comments on commit af2619c

Please sign in to comment.