2019-07-29 18:26:18 +00:00
|
|
|
export const canUploadFile = (file, serverInfo) => {
|
|
|
|
const { FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize } = serverInfo;
|
|
|
|
if (!(file && file.path)) {
|
2019-09-24 20:16:59 +00:00
|
|
|
return { success: true };
|
2019-07-29 18:26:18 +00:00
|
|
|
}
|
2019-09-24 20:16:59 +00:00
|
|
|
if (FileUpload_MaxFileSize > -1 && file.size > FileUpload_MaxFileSize) {
|
|
|
|
return { success: false, error: 'error-file-too-large' };
|
2019-07-29 18:26:18 +00:00
|
|
|
}
|
|
|
|
// if white list is empty, all media types are enabled
|
|
|
|
if (!FileUpload_MediaTypeWhiteList) {
|
2019-09-24 20:16:59 +00:00
|
|
|
return { success: true };
|
2019-07-29 18:26:18 +00:00
|
|
|
}
|
|
|
|
const allowedMime = FileUpload_MediaTypeWhiteList.split(',');
|
|
|
|
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 = '/*';
|
|
|
|
const wildCards = allowedMime.filter(item => 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
|
|
|
};
|