verdnatura-chat/app/lib/methods/helpers/media.ts

38 lines
1.1 KiB
TypeScript
Raw Normal View History

import { IShareAttachment } from '../../../definitions';
export const canUploadFile = ({
file,
allowList,
maxFileSize,
permissionToUploadFile
}: {
file: IShareAttachment;
allowList?: string;
maxFileSize?: number;
permissionToUploadFile: boolean;
}): { success: boolean; error?: string } => {
if (!(file && file.path)) {
2019-09-24 20:16:59 +00:00
return { success: true };
}
if (maxFileSize && maxFileSize > -1 && file.size > maxFileSize) {
2019-09-24 20:16:59 +00:00
return { success: false, error: 'error-file-too-large' };
}
if (!permissionToUploadFile) {
return { success: false, error: 'error-not-permission-to-upload-file' };
}
// if white list is empty, all media types are enabled
if (!allowList || allowList === '*') {
2019-09-24 20:16:59 +00:00
return { success: true };
}
const allowedMime = allowList.split(',');
if (allowedMime.includes(file.mime!)) {
2019-09-24 20:16:59 +00:00
return { success: true };
}
const wildCardGlob = '/*';
const wildCards = allowedMime.filter((item: string) => item.indexOf(wildCardGlob) > 0);
2019-09-24 20:16:59 +00:00
if (file.mime && wildCards.includes(file.mime.replace(/(\/.*)$/, wildCardGlob))) {
return { success: true };
}
2019-09-24 20:16:59 +00:00
return { success: false, error: 'error-invalid-file-type' };
};