2020-06-08 12:29:24 +02:00
|
|
|
import apiClient from '../../../utils/apiClient';
|
2020-06-08 11:46:38 +02:00
|
|
|
import getConfig from '../../../utils/getConfig';
|
2016-07-19 22:49:46 +02:00
|
|
|
import { createFile } from './files';
|
2018-05-31 00:23:32 +02:00
|
|
|
import { TEXT_FILE_REGEX } from '../../../../server/utils/fileUtils';
|
2016-07-16 01:05:18 +02:00
|
|
|
|
2020-06-08 11:46:38 +02:00
|
|
|
const s3BucketHttps = getConfig('S3_BUCKET_URL_BASE') ||
|
|
|
|
`https://s3-${getConfig('AWS_REGION')}.amazonaws.com/${getConfig('S3_BUCKET')}/`;
|
2016-10-25 21:51:44 +02:00
|
|
|
const MAX_LOCAL_FILE_SIZE = 80000; // bytes, aka 80 KB
|
2016-07-16 01:05:18 +02:00
|
|
|
|
2016-08-25 06:18:28 +02:00
|
|
|
function localIntercept(file, options = {}) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!options.readType) {
|
|
|
|
// const mime = file.type;
|
|
|
|
// const textType = a(_textTypes).any(type => {
|
|
|
|
// const re = new RegExp(type);
|
|
|
|
// return re.test(mime);
|
|
|
|
// });
|
|
|
|
// options.readType = textType ? 'readAsText' : 'readAsDataURL';
|
|
|
|
options.readType = 'readAsText'; // eslint-disable-line
|
|
|
|
}
|
|
|
|
const reader = new window.FileReader();
|
|
|
|
reader.onload = () => {
|
|
|
|
resolve(reader.result);
|
|
|
|
};
|
|
|
|
reader.onerror = () => {
|
|
|
|
reject(reader.result);
|
|
|
|
};
|
|
|
|
|
|
|
|
// run the reader
|
|
|
|
reader[options.readType](file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-06 20:34:14 +02:00
|
|
|
export function dropzoneAcceptCallback(userId, file, done) {
|
2016-12-14 02:08:10 +01:00
|
|
|
return () => {
|
2018-05-31 00:23:32 +02:00
|
|
|
// if a user would want to edit this file as text, local interceptor
|
|
|
|
if (file.name.match(TEXT_FILE_REGEX) && file.size < MAX_LOCAL_FILE_SIZE) {
|
2017-02-22 20:29:35 +01:00
|
|
|
localIntercept(file).then((result) => {
|
2016-08-25 06:18:28 +02:00
|
|
|
file.content = result; // eslint-disable-line
|
2016-10-24 22:33:16 +02:00
|
|
|
done('Uploading plaintext file locally.');
|
2016-08-25 06:18:28 +02:00
|
|
|
})
|
2018-05-05 02:22:39 +02:00
|
|
|
.catch((result) => {
|
|
|
|
done(`Failed to download file ${file.name}: ${result}`);
|
|
|
|
console.warn(file);
|
|
|
|
});
|
2016-08-25 06:18:28 +02:00
|
|
|
} else {
|
|
|
|
file.postData = []; // eslint-disable-line
|
2020-06-08 12:29:24 +02:00
|
|
|
apiClient.post('/S3/sign', {
|
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
size: file.size,
|
|
|
|
userId
|
2016-08-25 06:18:28 +02:00
|
|
|
// _csrf: document.getElementById('__createPostToken').value
|
2020-06-08 12:29:24 +02:00
|
|
|
})
|
2018-05-05 02:22:39 +02:00
|
|
|
.then((response) => {
|
2018-08-31 01:35:03 +02:00
|
|
|
file.custom_status = 'ready'; // eslint-disable-line
|
|
|
|
file.postData = response.data; // eslint-disable-line
|
|
|
|
file.s3 = response.data.key; // eslint-disable-line
|
|
|
|
file.previewTemplate.className += ' uploading'; // eslint-disable-line
|
2018-05-05 02:22:39 +02:00
|
|
|
done();
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-09-26 21:06:43 +02:00
|
|
|
file.custom_status = 'rejected'; // eslint-disable-line
|
|
|
|
if (response.data && response.data.responseText && response.data.responseText.message) {
|
2018-05-05 02:22:39 +02:00
|
|
|
done(response.data.responseText.message);
|
|
|
|
}
|
2019-09-26 21:06:43 +02:00
|
|
|
done('Error: Reached upload limit.');
|
2018-05-05 02:22:39 +02:00
|
|
|
});
|
2016-08-25 06:18:28 +02:00
|
|
|
}
|
2016-07-19 22:49:46 +02:00
|
|
|
};
|
2016-07-16 01:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function dropzoneSendingCallback(file, xhr, formData) {
|
2016-07-19 22:49:46 +02:00
|
|
|
return () => {
|
2018-05-31 00:23:32 +02:00
|
|
|
if (!file.name.match(TEXT_FILE_REGEX) || file.size >= MAX_LOCAL_FILE_SIZE) {
|
2017-02-22 20:29:35 +01:00
|
|
|
Object.keys(file.postData).forEach((key) => {
|
2016-08-25 06:18:28 +02:00
|
|
|
formData.append(key, file.postData[key]);
|
|
|
|
});
|
|
|
|
}
|
2016-07-19 22:49:46 +02:00
|
|
|
};
|
2016-07-16 01:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function dropzoneCompleteCallback(file) {
|
2016-07-19 22:49:46 +02:00
|
|
|
return (dispatch, getState) => { // eslint-disable-line
|
2018-05-31 00:23:32 +02:00
|
|
|
if ((!file.name.match(TEXT_FILE_REGEX) || file.size >= MAX_LOCAL_FILE_SIZE) && file.status !== 'error') {
|
2016-08-25 06:18:28 +02:00
|
|
|
let inputHidden = '<input type="hidden" name="attachments[]" value="';
|
|
|
|
const json = {
|
2016-09-09 04:15:29 +02:00
|
|
|
url: `${s3BucketHttps}${file.postData.key}`,
|
2016-08-25 06:18:28 +02:00
|
|
|
originalFilename: file.name
|
|
|
|
};
|
2016-09-09 04:02:42 +02:00
|
|
|
// console.log(json, JSON.stringify(json), JSON.stringify(json).replace('"', '\\"'));
|
2016-08-25 06:18:28 +02:00
|
|
|
inputHidden += `${window.btoa(JSON.stringify(json))}" />`;
|
|
|
|
// document.getElementById('uploader').appendChild(inputHidden);
|
|
|
|
document.getElementById('uploader').innerHTML += inputHidden;
|
2016-07-19 22:49:46 +02:00
|
|
|
|
2016-08-25 06:18:28 +02:00
|
|
|
const formParams = {
|
|
|
|
name: file.name,
|
2016-09-09 04:02:42 +02:00
|
|
|
url: `${s3BucketHttps}${file.postData.key}`
|
2016-08-25 06:18:28 +02:00
|
|
|
};
|
|
|
|
createFile(formParams)(dispatch, getState);
|
2016-10-25 03:15:32 +02:00
|
|
|
} else if (file.content !== undefined) {
|
2016-08-25 06:18:28 +02:00
|
|
|
const formParams = {
|
|
|
|
name: file.name,
|
|
|
|
content: file.content
|
|
|
|
};
|
|
|
|
createFile(formParams)(dispatch, getState);
|
|
|
|
}
|
2016-07-16 01:05:18 +02:00
|
|
|
};
|
|
|
|
}
|