2022-01-12 12:54:04 +00:00
|
|
|
import { IAttachment } from '../views/ShareView/interfaces';
|
|
|
|
|
|
|
|
export const canUploadFile = (
|
|
|
|
file: IAttachment,
|
|
|
|
allowList: string,
|
|
|
|
maxFileSize: number,
|
|
|
|
permissionToUploadFile: boolean
|
|
|
|
): { success: boolean; error?: string } => {
|
2019-07-29 18:26:18 +00:00
|
|
|
if (!(file && file.path)) {
|
2019-09-24 20:16:59 +00:00
|
|
|
return { success: true };
|
2019-07-29 18:26:18 +00:00
|
|
|
}
|
2020-06-26 20:22:56 +00:00
|
|
|
if (maxFileSize > -1 && file.size > maxFileSize) {
|
2019-09-24 20:16:59 +00:00
|
|
|
return { success: false, error: 'error-file-too-large' };
|
2019-07-29 18:26:18 +00:00
|
|
|
}
|
2021-12-13 16:27:01 +00:00
|
|
|
if (!permissionToUploadFile) {
|
|
|
|
return { success: false, error: 'error-not-permission-to-upload-file' };
|
|
|
|
}
|
2019-07-29 18:26:18 +00:00
|
|
|
// if white list is empty, all media types are enabled
|
2020-06-26 20:22:56 +00:00
|
|
|
if (!allowList || allowList === '*') {
|
2019-09-24 20:16:59 +00:00
|
|
|
return { success: true };
|
2019-07-29 18:26:18 +00:00
|
|
|
}
|
2020-06-26 20:22:56 +00:00
|
|
|
const allowedMime = allowList.split(',');
|
2022-01-12 12:54:04 +00:00
|
|
|
if (allowedMime.includes(file.mime!)) {
|
2019-09-24 20:16:59 +00:00
|
|
|
return { success: true };
|
2019-07-29 18:26:18 +00:00
|
|
|
}
|
|
|
|
const wildCardGlob = '/*';
|
2022-01-12 12:54:04 +00:00
|
|
|
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-07-29 18:26:18 +00:00
|
|
|
}
|
2019-09-24 20:16:59 +00:00
|
|
|
return { success: false, error: 'error-invalid-file-type' };
|
2019-07-29 18:26:18 +00:00
|
|
|
};
|