-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathuploader.js
More file actions
112 lines (104 loc) · 3.91 KB
/
uploader.js
File metadata and controls
112 lines (104 loc) · 3.91 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
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
import { TEXT_FILE_REGEX } from '../../../../server/utils/fileUtils';
import { apiClient } from '../../../utils/apiClient';
import { getConfig } from '../../../utils/getConfig';
import { isTestEnvironment } from '../../../utils/checkTestEnv';
import { handleCreateFile } from './files';
import { showErrorModal } from './ide';
const s3BucketUrlBase = getConfig('S3_BUCKET_URL_BASE');
const awsRegion = getConfig('AWS_REGION');
const s3Bucket = getConfig('S3_BUCKET');
if (!isTestEnvironment && !s3BucketUrlBase && !(awsRegion && s3Bucket)) {
throw new Error(`S3 bucket address not configured.
Configure either S3_BUCKET_URL_BASE or both AWS_REGION & S3_BUCKET in env vars`);
}
export const s3BucketHttps =
s3BucketUrlBase || `https://s3-${awsRegion}.amazonaws.com/${s3Bucket}/`;
const MAX_LOCAL_FILE_SIZE = 80000; // bytes, aka 80 KB
function isS3Upload(file) {
return !TEXT_FILE_REGEX.test(file.name) || file.size >= MAX_LOCAL_FILE_SIZE;
}
export async function dropzoneAcceptCallback(userId, file, done, dispatch) {
// if a user would want to edit this file as text, local interceptor
if (!isS3Upload(file)) {
try {
// eslint-disable-next-line no-param-reassign
file.content = await file.text();
// Make it an error so that it won't be sent to S3, but style as a success.
done('Uploading plaintext file locally.');
file.previewElement.classList.remove('dz-error');
file.previewElement.classList.add('dz-success');
file.previewElement.classList.add('dz-processing');
file.previewElement.querySelector('.dz-upload').style.width = '100%';
} catch (error) {
done(`Failed to download file ${file.name}: ${error}`);
console.warn(file);
}
} else {
try {
const response = await apiClient.post('/S3/sign', {
name: file.name,
type: file.type,
size: file.size,
userId
// _csrf: document.getElementById('__createPostToken').value
});
// eslint-disable-next-line no-param-reassign
file.postData = response.data;
done();
} catch (error) {
if (error?.response?.status === 403) {
if (dispatch) {
dispatch(showErrorModal('uploadLimit'));
}
done('Upload limit reached.');
return;
}
done(
error?.response?.data?.responseText?.message ||
error?.message ||
'Error: Reached upload limit.'
);
}
}
}
export function dropzoneSendingCallback(file, xhr, formData) {
if (isS3Upload(file)) {
Object.keys(file.postData).forEach((key) => {
formData.append(key, file.postData[key]);
});
}
}
export function dropzoneCompleteCallback(file) {
return (dispatch) => {
if (isS3Upload(file) && file.postData && file.status !== 'error') {
const formParams = {
name: file.name,
url: `${s3BucketHttps}${file.postData.key}`
};
dispatch(handleCreateFile(formParams, false));
} else if (file.content !== undefined) {
const formParams = {
name: file.name,
content: file.content
};
dispatch(handleCreateFile(formParams, false));
} else if (file.status === 'error' || file.xhr.status >= 400) {
let uploadFileErrorMessage = 'Uploading file to AWS failed.';
if (file.xhr?.response) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(file.xhr.response, 'text/xml');
const message = xmlDoc.getElementsByTagName('Message')[0]?.textContent;
const code = xmlDoc.getElementsByTagName('Code')[0]?.textContent;
uploadFileErrorMessage = `${code}: ${message}`;
}
file.previewElement.classList.add('dz-error');
file.previewElement.classList.remove('dz-success');
const dzErrorMessageElement = file.previewElement?.querySelector(
'[data-dz-errormessage]'
);
if (dzErrorMessageElement) {
dzErrorMessageElement.textContent = uploadFileErrorMessage;
}
}
};
}