-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmedia.ts
More file actions
38 lines (37 loc) · 1.22 KB
/
media.ts
File metadata and controls
38 lines (37 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { type 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)) {
return { success: true };
}
if (maxFileSize && maxFileSize > -1 && file.size > maxFileSize) {
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 === '*') {
return { success: true };
}
const allowedMime = allowList.replaceAll(' ', '').split(',');
const normalizedMime = file.mime?.toLowerCase();
if (normalizedMime && allowedMime.includes(normalizedMime)) {
return { success: true };
}
const wildCardGlob = '/*';
const wildCards = allowedMime.filter((item: string) => item.indexOf(wildCardGlob) > 0);
if (normalizedMime && wildCards.includes(normalizedMime.replace(/(\/.*)$/, wildCardGlob))) {
return { success: true };
}
return { success: false, error: 'error-invalid-file-type' };
};