|
| 1 | +import { TEXT_FILE_REGEX } from '../../../../server/utils/fileUtils'; |
1 | 2 | import apiClient from '../../../utils/apiClient';
|
2 | 3 | import getConfig from '../../../utils/getConfig';
|
3 | 4 | import { handleCreateFile } from './files';
|
4 |
| -import { TEXT_FILE_REGEX } from '../../../../server/utils/fileUtils'; |
5 | 5 |
|
6 |
| -const s3BucketHttps = |
| 6 | +export const s3BucketHttps = |
7 | 7 | getConfig('S3_BUCKET_URL_BASE') ||
|
8 | 8 | `https://s3-${getConfig('AWS_REGION')}.amazonaws.com/${getConfig(
|
9 | 9 | 'S3_BUCKET'
|
10 | 10 | )}/`;
|
11 | 11 | const MAX_LOCAL_FILE_SIZE = 80000; // bytes, aka 80 KB
|
12 | 12 |
|
13 |
| -function localIntercept(file, options = {}) { |
14 |
| - return new Promise((resolve, reject) => { |
15 |
| - if (!options.readType) { |
16 |
| - // const mime = file.type; |
17 |
| - // const textType = a(_textTypes).any(type => { |
18 |
| - // const re = new RegExp(type); |
19 |
| - // return re.test(mime); |
20 |
| - // }); |
21 |
| - // options.readType = textType ? 'readAsText' : 'readAsDataURL'; |
22 |
| - options.readType = 'readAsText'; // eslint-disable-line |
23 |
| - } |
24 |
| - const reader = new window.FileReader(); |
25 |
| - reader.onload = () => { |
26 |
| - resolve(reader.result); |
27 |
| - }; |
28 |
| - reader.onerror = () => { |
29 |
| - reject(reader.result); |
30 |
| - }; |
31 |
| - |
32 |
| - // run the reader |
33 |
| - reader[options.readType](file); |
34 |
| - }); |
35 |
| -} |
36 |
| - |
37 |
| -function toBinary(string) { |
38 |
| - const codeUnits = new Uint16Array(string.length); |
39 |
| - for (let i = 0; i < codeUnits.length; i += 1) { |
40 |
| - codeUnits[i] = string.charCodeAt(i); |
41 |
| - } |
42 |
| - return String.fromCharCode(...new Uint8Array(codeUnits.buffer)); |
| 13 | +function isS3Upload(file) { |
| 14 | + return !TEXT_FILE_REGEX.test(file.name) || file.size >= MAX_LOCAL_FILE_SIZE; |
43 | 15 | }
|
44 | 16 |
|
45 |
| -export function dropzoneAcceptCallback(userId, file, done) { |
46 |
| - return () => { |
47 |
| - // if a user would want to edit this file as text, local interceptor |
48 |
| - if (file.name.match(TEXT_FILE_REGEX) && file.size < MAX_LOCAL_FILE_SIZE) { |
49 |
| - localIntercept(file) |
50 |
| - .then((result) => { |
51 |
| - file.content = result; // eslint-disable-line |
52 |
| - done('Uploading plaintext file locally.'); |
53 |
| - file.previewElement.classList.remove('dz-error'); |
54 |
| - file.previewElement.classList.add('dz-success'); |
55 |
| - file.previewElement.classList.add('dz-processing'); |
56 |
| - file.previewElement.querySelector('.dz-upload').style.width = '100%'; |
57 |
| - }) |
58 |
| - .catch((result) => { |
59 |
| - done(`Failed to download file ${file.name}: ${result}`); |
60 |
| - console.warn(file); |
61 |
| - }); |
62 |
| - } else { |
63 |
| - file.postData = []; // eslint-disable-line |
64 |
| - apiClient |
65 |
| - .post('/S3/sign', { |
66 |
| - name: file.name, |
67 |
| - type: file.type, |
68 |
| - size: file.size, |
69 |
| - userId |
70 |
| - // _csrf: document.getElementById('__createPostToken').value |
71 |
| - }) |
72 |
| - .then((response) => { |
73 |
| - file.custom_status = 'ready'; // eslint-disable-line |
74 |
| - file.postData = response.data; // eslint-disable-line |
75 |
| - file.s3 = response.data.key; // eslint-disable-line |
76 |
| - file.previewTemplate.className += ' uploading'; // eslint-disable-line |
77 |
| - done(); |
78 |
| - }) |
79 |
| - .catch((error) => { |
80 |
| - const { response } = error; |
81 |
| - file.custom_status = 'rejected'; // eslint-disable-line |
82 |
| - if ( |
83 |
| - response.data && |
84 |
| - response.data.responseText && |
85 |
| - response.data.responseText.message |
86 |
| - ) { |
87 |
| - done(response.data.responseText.message); |
88 |
| - } |
89 |
| - done('Error: Reached upload limit.'); |
90 |
| - }); |
| 17 | +export async function dropzoneAcceptCallback(userId, file, done) { |
| 18 | + // if a user would want to edit this file as text, local interceptor |
| 19 | + if (!isS3Upload(file)) { |
| 20 | + try { |
| 21 | + // eslint-disable-next-line no-param-reassign |
| 22 | + file.content = await file.text(); |
| 23 | + // Make it an error so that it won't be sent to S3, but style as a success. |
| 24 | + done('Uploading plaintext file locally.'); |
| 25 | + file.previewElement.classList.remove('dz-error'); |
| 26 | + file.previewElement.classList.add('dz-success'); |
| 27 | + file.previewElement.classList.add('dz-processing'); |
| 28 | + file.previewElement.querySelector('.dz-upload').style.width = '100%'; |
| 29 | + } catch (error) { |
| 30 | + done(`Failed to download file ${file.name}: ${error}`); |
| 31 | + console.warn(file); |
91 | 32 | }
|
92 |
| - }; |
| 33 | + } else { |
| 34 | + try { |
| 35 | + const response = await apiClient.post('/S3/sign', { |
| 36 | + name: file.name, |
| 37 | + type: file.type, |
| 38 | + size: file.size, |
| 39 | + userId |
| 40 | + // _csrf: document.getElementById('__createPostToken').value |
| 41 | + }); |
| 42 | + // eslint-disable-next-line no-param-reassign |
| 43 | + file.postData = response.data; |
| 44 | + done(); |
| 45 | + } catch (error) { |
| 46 | + done( |
| 47 | + error?.response?.data?.responseText?.message || |
| 48 | + error?.message || |
| 49 | + 'Error: Reached upload limit.' |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
93 | 53 | }
|
94 | 54 |
|
95 | 55 | export function dropzoneSendingCallback(file, xhr, formData) {
|
96 |
| - return () => { |
97 |
| - if (!file.name.match(TEXT_FILE_REGEX) || file.size >= MAX_LOCAL_FILE_SIZE) { |
98 |
| - Object.keys(file.postData).forEach((key) => { |
99 |
| - formData.append(key, file.postData[key]); |
100 |
| - }); |
101 |
| - } |
102 |
| - }; |
| 56 | + if (isS3Upload(file)) { |
| 57 | + Object.keys(file.postData).forEach((key) => { |
| 58 | + formData.append(key, file.postData[key]); |
| 59 | + }); |
| 60 | + } |
103 | 61 | }
|
104 | 62 |
|
105 | 63 | export function dropzoneCompleteCallback(file) {
|
106 |
| - return (dispatch) => { // eslint-disable-line |
107 |
| - if ( |
108 |
| - (!file.name.match(TEXT_FILE_REGEX) || file.size >= MAX_LOCAL_FILE_SIZE) && |
109 |
| - file.status !== 'error' |
110 |
| - ) { |
111 |
| - let inputHidden = '<input type="hidden" name="attachments[]" value="'; |
112 |
| - const json = { |
113 |
| - url: `${s3BucketHttps}${file.postData.key}`, |
114 |
| - originalFilename: file.name |
115 |
| - }; |
116 |
| - |
117 |
| - let jsonStr = JSON.stringify(json); |
118 |
| - |
119 |
| - // convert the json string to binary data so that btoa can encode it |
120 |
| - jsonStr = toBinary(jsonStr); |
121 |
| - inputHidden += `${window.btoa(jsonStr)}" />`; |
122 |
| - document.getElementById('uploader').innerHTML += inputHidden; |
123 |
| - |
| 64 | + return (dispatch) => { |
| 65 | + if (isS3Upload(file) && file.postData && file.status !== 'error') { |
124 | 66 | const formParams = {
|
125 | 67 | name: file.name,
|
126 | 68 | url: `${s3BucketHttps}${file.postData.key}`
|
|
0 commit comments