Skip to content

Commit

Permalink
chore: fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto committed May 2, 2022
1 parent 168378b commit 79718f0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/v1/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ API.v1.addRoute(
{
post() {
const { assetName, refreshAllClients } = this.bodyParams;
const isValidAsset = Object.keys(RocketChatAssets.assets).includes(assetName);
const isValidAsset = Object.keys(RocketChatAssets.assets).includes(assetName as string);
if (!isValidAsset) {
throw new Meteor.Error('error-invalid-asset', 'Invalid asset');
}
Expand Down
58 changes: 31 additions & 27 deletions apps/meteor/app/assets/server/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import _ from 'underscore';
import sizeOf from 'image-size';
import sharp from 'sharp';
import { Request, Response, NextFunction } from 'express';
import type { IRocketChatAssets, IRocketChatAsset } from '@rocket.chat/core-typings';
import { IRocketChatAssets, IRocketChatAsset } from '@rocket.chat/core-typings';

import { settings, settingsRegistry } from '../../settings/server';
import { getURL } from '../../utils/lib/getURL';
import { mime } from '../../utils/lib/mimeTypes';
import { hasPermission } from '../../authorization';
import { hasPermission } from '../../authorization/server';
import { RocketChatFile } from '../../file';
import { Settings } from '../../models/server';

Expand Down Expand Up @@ -188,6 +188,10 @@ const assets: IRocketChatAssets = {
},
};

function getAssetByKey(key: string): IRocketChatAsset {
return assets[key as keyof IRocketChatAssets];
}

class RocketChatAssetsClass {
get mime(): any {
return mime;
Expand All @@ -198,29 +202,29 @@ class RocketChatAssetsClass {
}

public setAsset(binaryContent: BufferEncoding, contentType: string, asset: string): void {
if (!assets[asset]) {
if (!getAssetByKey(asset)) {
throw new Meteor.Error('error-invalid-asset', 'Invalid asset', {
function: 'RocketChat.Assets.setAsset',
});
}

const extension = mime.extension(contentType);
if (assets[asset].constraints.extensions.includes(extension) === false) {
if (getAssetByKey(asset).constraints.extensions.includes(extension as string) === false) {
throw new Meteor.Error(contentType, `Invalid file type: ${contentType}`, {
function: 'RocketChat.Assets.setAsset',
errorTitle: 'error-invalid-file-type',
});
}

const file = Buffer.from(binaryContent, 'binary');
if (assets[asset].constraints.width || assets[asset].constraints.height) {
if (getAssetByKey(asset).constraints.width || getAssetByKey(asset).constraints.height) {
const dimensions = sizeOf(file);
if (assets[asset].constraints.width && assets[asset].constraints.width !== dimensions.width) {
if (getAssetByKey(asset).constraints.width && getAssetByKey(asset).constraints.width !== dimensions.width) {
throw new Meteor.Error('error-invalid-file-width', 'Invalid file width', {
function: 'Invalid file width',
});
}
if (assets[asset].constraints.height && assets[asset].constraints.height !== dimensions.height) {
if (getAssetByKey(asset).constraints.height && getAssetByKey(asset).constraints.height !== dimensions.height) {
throw new Meteor.Error('error-invalid-file-height');
}
}
Expand All @@ -236,7 +240,7 @@ class RocketChatAssetsClass {
const key = `Assets_${asset}`;
const value = {
url: `assets/${asset}.${extension}`,
defaultUrl: assets[asset].defaultUrl,
defaultUrl: getAssetByKey(asset).defaultUrl,
};

Settings.updateValueById(key, value);
Expand All @@ -250,7 +254,7 @@ class RocketChatAssetsClass {
}

public unsetAsset(asset: string): void {
if (!assets[asset]) {
if (!getAssetByKey(asset)) {
throw new Meteor.Error('error-invalid-asset', 'Invalid asset', {
function: 'RocketChat.Assets.unsetAsset',
});
Expand All @@ -259,7 +263,7 @@ class RocketChatAssetsClass {
RocketChatAssetsInstance.deleteFile(asset);
const key = `Assets_${asset}`;
const value = {
defaultUrl: assets[asset].defaultUrl,
defaultUrl: getAssetByKey(asset).defaultUrl,
};

Settings.updateValueById(key, value);
Expand All @@ -279,7 +283,7 @@ class RocketChatAssetsClass {
}

const assetKey = settingKey.replace(/^Assets_/, '');
const assetValue = assets[assetKey];
const assetValue = getAssetByKey(assetKey);

if (!assetValue) {
return;
Expand Down Expand Up @@ -318,7 +322,7 @@ class RocketChatAssetsClass {
}

public getURL(assetName: string, options = { cdn: false, full: true }): string {
const asset = settings.get(assetName);
const asset = settings.get<Record<string, any>>(assetName);
const url = asset.url || asset.defaultUrl;

return getURL(url, options);
Expand Down Expand Up @@ -351,19 +355,19 @@ function addAssetToSetting(asset: string, value: IRocketChatAsset): void {
asset,
public: true,
wizard: value.wizard,
},
} as Record<string, any>,
);

const currentValue = settings.get(key);
const currentValue = settings.get<Record<string, any>>(key);

if (typeof currentValue === 'object' && currentValue.defaultUrl !== assets[asset].defaultUrl) {
currentValue.defaultUrl = assets[asset].defaultUrl;
if (typeof currentValue === 'object' && currentValue.defaultUrl !== getAssetByKey(asset).defaultUrl) {
currentValue.defaultUrl = getAssetByKey(asset).defaultUrl;
Settings.updateValueById(key, currentValue);
}
}

for (const key of Object.keys(assets)) {
const value = assets[key];
const value = getAssetByKey(key);
addAssetToSetting(key, value);
}

Expand All @@ -381,7 +385,7 @@ const { calculateClientHash } = WebAppHashing;

WebAppHashing.calculateClientHash = function (manifest: Record<string, any>, includeFilter: Function, runtimeConfigOverride: any): string {
for (const key of Object.keys(assets)) {
const value = assets[key];
const value = getAssetByKey(key);
if (!value.cache && !value.defaultUrl) {
continue;
}
Expand All @@ -399,7 +403,7 @@ WebAppHashing.calculateClientHash = function (manifest: Record<string, any>, inc
hash: value.cache.hash,
};
} else {
const extension = value.defaultUrl.split('.').pop();
const extension = value.defaultUrl?.split('.').pop();
cache = {
path: `assets/${key}.${extension}`,
cacheable: false,
Expand Down Expand Up @@ -434,7 +438,7 @@ Meteor.methods({
});
}

const _hasPermission = hasPermission(Meteor.userId(), 'manage-assets');
const _hasPermission = hasPermission(Meteor.userId() as string, 'manage-assets');
if (!_hasPermission) {
throw new Meteor.Error('error-action-not-allowed', 'Managing assets not allowed', {
method: 'refreshClients',
Expand All @@ -452,7 +456,7 @@ Meteor.methods({
});
}

const _hasPermission = hasPermission(Meteor.userId(), 'manage-assets');
const _hasPermission = hasPermission(Meteor.userId() as string, 'manage-assets');
if (!_hasPermission) {
throw new Meteor.Error('error-action-not-allowed', 'Managing assets not allowed', {
method: 'unsetAsset',
Expand All @@ -470,7 +474,7 @@ Meteor.methods({
});
}

const _hasPermission = hasPermission(Meteor.userId(), 'manage-assets');
const _hasPermission = hasPermission(Meteor.userId() as string, 'manage-assets');
if (!_hasPermission) {
throw new Meteor.Error('error-action-not-allowed', 'Managing assets not allowed', {
method: 'setAsset',
Expand All @@ -489,20 +493,20 @@ WebApp.connectHandlers.use(
asset: decodeURIComponent(req.url.replace(/^\//, '').replace(/\?.*$/, '')).replace(/\.[^.]*$/, ''),
};

const file = assets[params.asset] && assets[params.asset].cache;
const file = getAssetByKey(params.asset) && getAssetByKey(params.asset).cache;

const format = req.url.replace(/.*\.([a-z]+)(?:$|\?.*)/i, '$1');

if (
assets[params.asset] &&
Array.isArray(assets[params.asset].constraints.extensions) &&
!assets[params.asset].constraints.extensions.includes(format)
getAssetByKey(params.asset) &&
Array.isArray(getAssetByKey(params.asset).constraints.extensions) &&
!getAssetByKey(params.asset).constraints.extensions.includes(format)
) {
res.writeHead(403);
return res.end();
}
if (!file) {
const defaultUrl = assets[params.asset] && assets[params.asset].defaultUrl;
const defaultUrl = getAssetByKey(params.asset) && getAssetByKey(params.asset).defaultUrl;
if (defaultUrl) {
const assetUrl = format && ['png', 'svg'].includes(format) ? defaultUrl.replace(/(svg|png)$/, format) : defaultUrl;
req.url = `/${assetUrl}`;
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/definition/externals/meteor/webapp-hashing.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'meteor/webapp-hashing' {
namespace WebAppHashing {
function calculateClientHash(manifest: Record<string, any>, includeFilter: Function, runtimeConfigOverride: any): string;
}
}
1 change: 1 addition & 0 deletions apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@types/bad-words": "^3.0.1",
"@types/bcrypt": "^5.0.0",
"@types/body-parser": "^1.19.0",
"@types/busboy": "^1.5.0",
"@types/chai": "^4.2.22",
"@types/chai-datetime": "0.0.37",
"@types/chai-dom": "0.0.11",
Expand Down
16 changes: 16 additions & 0 deletions packages/core-typings/src/IRocketChatAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,27 @@ export interface IRocketChatAssetWizard {
order: number;
}

export interface IRocketChatAssetCache {
path: string;
cacheable: boolean;
where: string;
type: string;
content: Buffer;
extension: string;
url: string;
size: number;
uploadDate: Date;
contentType: string;
hash: string;
sourceMapUrl?: string;
}

export interface IRocketChatAsset {
label: string;
constraints: IRocketChatAssetConstraint;
defaultUrl?: string;
wizard?: IRocketChatAssetWizard;
cache?: IRocketChatAssetCache;
}

export interface IRocketChatAssets {
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3837,6 +3837,7 @@ __metadata:
"@types/bad-words": ^3.0.1
"@types/bcrypt": ^5.0.0
"@types/body-parser": ^1.19.0
"@types/busboy": ^1.5.0
"@types/chai": ^4.2.22
"@types/chai-datetime": 0.0.37
"@types/chai-dom": 0.0.11
Expand Down Expand Up @@ -5693,6 +5694,15 @@ __metadata:
languageName: node
linkType: hard

"@types/busboy@npm:^1.5.0":
version: 1.5.0
resolution: "@types/busboy@npm:1.5.0"
dependencies:
"@types/node": "*"
checksum: ffa7bf25c0395f6927526b7d97e70cd2df789e4ca0d231e41855fb08542fa236891ce457d83cc50cac6e5cef6be092ab80597070dcf1413f736462690a23e987
languageName: node
linkType: hard

"@types/caseless@npm:*":
version: 0.12.2
resolution: "@types/caseless@npm:0.12.2"
Expand Down

0 comments on commit 79718f0

Please sign in to comment.