This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
file_utils.jsx
114 lines (92 loc) · 3.65 KB
/
file_utils.jsx
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import exif2css from 'exif2css';
import Constants from 'utils/constants';
import * as UserAgent from 'utils/user_agent';
export function canUploadFiles(config) {
const enableFileAttachments = config.EnableFileAttachments === 'true';
const enableMobileFileUpload = config.EnableMobileFileUpload === 'true';
if (!enableFileAttachments) {
return false;
}
if (UserAgent.isMobileApp()) {
return enableMobileFileUpload;
}
return true;
}
export function canDownloadFiles(config) {
if (UserAgent.isMobileApp()) {
return config.EnableMobileFileDownload === 'true';
}
return true;
}
export function trimFilename(filename) {
let trimmedFilename = filename;
if (filename.length > Constants.MAX_FILENAME_LENGTH) {
trimmedFilename = filename.substring(0, Math.min(Constants.MAX_FILENAME_LENGTH, filename.length)) + '...';
}
return trimmedFilename;
}
export function getFileTypeFromMime(mimetype) {
const mimeTypeSplitBySlash = mimetype.split('/');
const mimeTypePrefix = mimeTypeSplitBySlash[0];
const mimeTypeSuffix = mimeTypeSplitBySlash[1];
if (mimeTypePrefix === 'video') {
return 'video';
} else if (mimeTypePrefix === 'audio') {
return 'audio';
} else if (mimeTypePrefix === 'image') {
return 'image';
}
if (mimeTypeSuffix) {
if (mimeTypeSuffix === 'pdf') {
return 'pdf';
} else if (mimeTypeSuffix.includes('vnd.ms-excel') || mimeTypeSuffix.includes('spreadsheetml') || mimeTypeSuffix.includes('vnd.sun.xml.calc') || mimeTypeSuffix.includes('opendocument.spreadsheet')) {
return 'spreadsheet';
} else if (mimeTypeSuffix.includes('vnd.ms-powerpoint') || mimeTypeSuffix.includes('presentationml') || mimeTypeSuffix.includes('vnd.sun.xml.impress') || mimeTypeSuffix.includes('opendocument.presentation')) {
return 'presentation';
} else if ((mimeTypeSuffix === 'msword') || mimeTypeSuffix.includes('vnd.ms-word') || mimeTypeSuffix.includes('officedocument.wordprocessingml') || mimeTypeSuffix.includes('application/x-mswrite')) {
return 'word';
}
}
return 'other';
}
// based on https://stackoverflow.com/questions/7584794/accessing-jpeg-exif-rotation-data-in-javascript-on-the-client-side/32490603#32490603
export function getExifOrientation(data) {
var view = new DataView(data);
if (view.getUint16(0, false) !== 0xFFD8) {
return -2;
}
var length = view.byteLength;
var offset = 2;
while (offset < length) {
var marker = view.getUint16(offset, false);
offset += 2;
if (marker === 0xFFE1) {
if (view.getUint32(offset += 2, false) !== 0x45786966) {
return -1;
}
var little = view.getUint16(offset += 6, false) === 0x4949;
offset += view.getUint32(offset + 4, little);
var tags = view.getUint16(offset, little);
offset += 2;
for (var i = 0; i < tags; i++) {
if (view.getUint16(offset + (i * 12), little) === 0x0112) {
return view.getUint16(offset + (i * 12) + 8, little);
}
}
} else if ((marker & 0xFF00) === 0xFF00) {
offset += view.getUint16(offset, false);
} else {
break;
}
}
return -1;
}
export function getOrientationStyles(orientation) {
const {
transform,
'transform-origin': transformOrigin,
} = exif2css(orientation);
return {transform, transformOrigin};
}