2015-06-23 10:16:53 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react/addons';
|
2015-07-17 15:41:09 +02:00
|
|
|
import Raven from 'raven-js';
|
2015-06-23 10:16:53 +02:00
|
|
|
|
2015-06-29 11:01:45 +02:00
|
|
|
import { getCookie } from '../../utils/fetch_api_utils';
|
2015-07-08 10:15:58 +02:00
|
|
|
import { getLangText } from '../../utils/lang_utils';
|
|
|
|
|
2015-06-30 15:41:39 +02:00
|
|
|
import S3Fetcher from '../../fetchers/s3_fetcher';
|
2015-06-23 10:16:53 +02:00
|
|
|
|
|
|
|
import fineUploader from 'fineUploader';
|
|
|
|
import FileDragAndDrop from './file_drag_and_drop';
|
|
|
|
|
2015-06-25 11:12:40 +02:00
|
|
|
import GlobalNotificationModel from '../../models/global_notification_model';
|
|
|
|
import GlobalNotificationActions from '../../actions/global_notification_actions';
|
|
|
|
|
2015-07-16 18:17:45 +02:00
|
|
|
import AppConstants from '../../constants/application_constants';
|
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
var ReactS3FineUploader = React.createClass({
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
keyRoutine: React.PropTypes.shape({
|
|
|
|
url: React.PropTypes.string,
|
2015-06-29 10:00:26 +02:00
|
|
|
fileClass: React.PropTypes.string,
|
2015-07-10 14:15:22 +02:00
|
|
|
pieceId: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.number
|
|
|
|
])
|
2015-06-23 10:16:53 +02:00
|
|
|
}),
|
2015-06-23 13:55:05 +02:00
|
|
|
createBlobRoutine: React.PropTypes.shape({
|
2015-06-29 17:21:14 +02:00
|
|
|
url: React.PropTypes.string,
|
2015-07-10 14:15:22 +02:00
|
|
|
pieceId: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.number
|
|
|
|
])
|
2015-06-23 13:55:05 +02:00
|
|
|
}),
|
2015-06-29 11:44:16 +02:00
|
|
|
submitKey: React.PropTypes.func,
|
2015-06-23 10:16:53 +02:00
|
|
|
autoUpload: React.PropTypes.bool,
|
|
|
|
debug: React.PropTypes.bool,
|
|
|
|
objectProperties: React.PropTypes.shape({
|
|
|
|
acl: React.PropTypes.string
|
|
|
|
}),
|
|
|
|
request: React.PropTypes.shape({
|
|
|
|
endpoint: React.PropTypes.string,
|
|
|
|
accessKey: React.PropTypes.string,
|
|
|
|
params: React.PropTypes.shape({
|
|
|
|
csrfmiddlewaretoken: React.PropTypes.string
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
signature: React.PropTypes.shape({
|
|
|
|
endpoint: React.PropTypes.string
|
2015-07-02 16:51:22 +02:00
|
|
|
}).isRequired,
|
2015-06-23 10:16:53 +02:00
|
|
|
uploadSuccess: React.PropTypes.shape({
|
|
|
|
method: React.PropTypes.string,
|
|
|
|
endpoint: React.PropTypes.string,
|
|
|
|
params: React.PropTypes.shape({
|
|
|
|
isBrowserPreviewCapable: React.PropTypes.any, // maybe fix this later
|
|
|
|
bitcoin_ID_noPrefix: React.PropTypes.string
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
cors: React.PropTypes.shape({
|
|
|
|
expected: React.PropTypes.bool
|
|
|
|
}),
|
|
|
|
chunking: React.PropTypes.shape({
|
|
|
|
enabled: React.PropTypes.bool
|
|
|
|
}),
|
|
|
|
resume: React.PropTypes.shape({
|
|
|
|
enabled: React.PropTypes.bool
|
|
|
|
}),
|
|
|
|
deleteFile: React.PropTypes.shape({
|
|
|
|
enabled: React.PropTypes.bool,
|
|
|
|
method: React.PropTypes.string,
|
2015-06-29 10:01:54 +02:00
|
|
|
endpoint: React.PropTypes.string,
|
|
|
|
customHeaders: React.PropTypes.object
|
2015-07-02 16:51:22 +02:00
|
|
|
}).isRequired,
|
2015-06-23 10:16:53 +02:00
|
|
|
session: React.PropTypes.shape({
|
2015-07-10 14:15:22 +02:00
|
|
|
customHeaders: React.PropTypes.object,
|
|
|
|
endpoint: React.PropTypes.string,
|
|
|
|
params: React.PropTypes.object,
|
|
|
|
refreshOnRequests: React.PropTypes.bool
|
2015-06-23 10:16:53 +02:00
|
|
|
}),
|
|
|
|
validation: React.PropTypes.shape({
|
|
|
|
itemLimit: React.PropTypes.number,
|
|
|
|
sizeLimit: React.PropTypes.string
|
|
|
|
}),
|
|
|
|
messages: React.PropTypes.shape({
|
|
|
|
unsupportedBrowser: React.PropTypes.string
|
|
|
|
}),
|
|
|
|
formatFileName: React.PropTypes.func,
|
|
|
|
multiple: React.PropTypes.bool,
|
|
|
|
retry: React.PropTypes.shape({
|
|
|
|
enableAuto: React.PropTypes.bool
|
2015-06-29 11:44:16 +02:00
|
|
|
}),
|
2015-06-30 09:20:20 +02:00
|
|
|
setIsUploadReady: React.PropTypes.func,
|
2015-06-30 13:53:02 +02:00
|
|
|
isReadyForFormSubmission: React.PropTypes.func,
|
2015-07-01 12:13:05 +02:00
|
|
|
areAssetsDownloadable: React.PropTypes.bool,
|
2015-07-14 10:33:15 +02:00
|
|
|
areAssetsEditable: React.PropTypes.bool,
|
|
|
|
defaultErrorMessage: React.PropTypes.string
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
2015-06-29 10:00:26 +02:00
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
autoUpload: true,
|
|
|
|
debug: false,
|
|
|
|
objectProperties: {
|
|
|
|
acl: 'public-read',
|
|
|
|
bucket: 'ascribe0'
|
|
|
|
},
|
|
|
|
request: {
|
|
|
|
endpoint: 'https://ascribe0.s3.amazonaws.com',
|
|
|
|
accessKey: 'AKIAIVCZJ33WSCBQ3QDA'
|
|
|
|
},
|
|
|
|
uploadSuccess: {
|
|
|
|
params: {
|
|
|
|
isBrowserPreviewCapable: fineUploader.supportedFeatures.imagePreviews
|
|
|
|
}
|
|
|
|
},
|
2015-07-02 18:54:47 +02:00
|
|
|
cors: {
|
|
|
|
expected: true,
|
|
|
|
sendCredentials: true
|
|
|
|
},
|
2015-06-29 10:00:26 +02:00
|
|
|
chunking: {
|
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
resume: {
|
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
retry: {
|
|
|
|
enableAuto: false
|
|
|
|
},
|
|
|
|
session: {
|
|
|
|
endpoint: null
|
|
|
|
},
|
|
|
|
messages: {
|
|
|
|
unsupportedBrowser: '<h3>Upload is not functional in IE7 as IE7 has no support for CORS!</h3>'
|
|
|
|
},
|
|
|
|
formatFileName: function(name){// fix maybe
|
|
|
|
if (name !== undefined && name.length > 26) {
|
|
|
|
name = name.slice(0, 15) + '...' + name.slice(-15);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
},
|
2015-07-14 10:33:15 +02:00
|
|
|
multiple: false,
|
|
|
|
defaultErrorMessage: 'Unexpected error. Please contact us if this happens repeatedly.'
|
2015-06-29 10:00:26 +02:00
|
|
|
};
|
|
|
|
},
|
2015-06-29 11:01:45 +02:00
|
|
|
|
2015-06-23 13:55:05 +02:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
filesToUpload: [],
|
2015-07-02 16:51:22 +02:00
|
|
|
uploader: new fineUploader.s3.FineUploaderBasic(this.propsToConfig()),
|
2015-07-16 18:17:45 +02:00
|
|
|
csrfToken: getCookie(AppConstants.csrftoken)
|
2015-06-23 13:55:05 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-07-02 16:51:22 +02:00
|
|
|
// since the csrf header is defined in this component's props,
|
2015-07-08 10:15:58 +02:00
|
|
|
// everytime the csrf cookie is changed we'll need to reinitalize
|
2015-07-02 16:51:22 +02:00
|
|
|
// fineuploader and update the actual csrf token
|
2015-07-08 10:15:58 +02:00
|
|
|
componentWillUpdate() {
|
2015-07-16 18:17:45 +02:00
|
|
|
let potentiallyNewCSRFToken = getCookie(AppConstants.csrftoken);
|
2015-07-02 16:51:22 +02:00
|
|
|
if(this.state.csrfToken !== potentiallyNewCSRFToken) {
|
|
|
|
this.setState({
|
|
|
|
uploader: new fineUploader.s3.FineUploaderBasic(this.propsToConfig()),
|
|
|
|
csrfToken: potentiallyNewCSRFToken
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-14 20:10:41 +02:00
|
|
|
componentWillUnmount() {
|
|
|
|
// Without this method, fineuploader will continue to try to upload artworks
|
|
|
|
// even though this component is not mounted any more.
|
|
|
|
// Therefore we cancel all uploads
|
|
|
|
this.state.uploader.cancelAll();
|
|
|
|
},
|
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
propsToConfig() {
|
|
|
|
let objectProperties = this.props.objectProperties;
|
2015-06-23 13:55:05 +02:00
|
|
|
objectProperties.key = this.requestKey;
|
2015-06-23 10:16:53 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
autoUpload: this.props.autoUpload,
|
|
|
|
debug: this.props.debug,
|
|
|
|
objectProperties: objectProperties, // do a special key handling here
|
|
|
|
request: this.props.request,
|
|
|
|
signature: this.props.signature,
|
|
|
|
uploadSuccess: this.props.uploadSuccess,
|
|
|
|
cors: this.props.cors,
|
|
|
|
chunking: this.props.chunking,
|
|
|
|
resume: this.props.resume,
|
|
|
|
deleteFile: this.props.deleteFile,
|
|
|
|
session: this.props.session,
|
|
|
|
validation: this.props.validation,
|
|
|
|
messages: this.props.messages,
|
|
|
|
formatFileName: this.props.formatFileName,
|
|
|
|
multiple: this.props.multiple,
|
|
|
|
retry: this.props.retry,
|
|
|
|
callbacks: {
|
|
|
|
onComplete: this.onComplete,
|
2015-06-29 10:01:54 +02:00
|
|
|
onCancel: this.onCancel,
|
2015-06-23 10:16:53 +02:00
|
|
|
onProgress: this.onProgress,
|
2015-06-30 15:41:39 +02:00
|
|
|
onDeleteComplete: this.onDeleteComplete,
|
2015-07-14 10:33:15 +02:00
|
|
|
onSessionRequestComplete: this.onSessionRequestComplete,
|
2015-07-15 16:50:32 +02:00
|
|
|
onError: this.onError,
|
|
|
|
onValidate: this.onValidate
|
2015-06-23 10:16:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2015-06-23 13:55:05 +02:00
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
requestKey(fileId) {
|
2015-07-14 10:36:37 +02:00
|
|
|
let defer = new fineUploader.Promise();
|
2015-06-23 10:16:53 +02:00
|
|
|
let filename = this.state.uploader.getName(fileId);
|
2015-07-20 18:59:32 +02:00
|
|
|
let uuid = this.state.uploader.getUuid(fileId);
|
2015-06-23 13:55:05 +02:00
|
|
|
|
2015-07-14 10:52:44 +02:00
|
|
|
window.fetch(this.props.keyRoutine.url, {
|
2015-07-14 10:36:37 +02:00
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2015-07-16 18:17:45 +02:00
|
|
|
'X-CSRFToken': getCookie(AppConstants.csrftoken)
|
2015-07-14 10:36:37 +02:00
|
|
|
},
|
|
|
|
credentials: 'include',
|
|
|
|
body: JSON.stringify({
|
|
|
|
'filename': filename,
|
2015-07-20 18:59:32 +02:00
|
|
|
'category': this.props.keyRoutine.fileClass,
|
|
|
|
'uuid': uuid,
|
2015-07-14 10:36:37 +02:00
|
|
|
'piece_id': this.props.keyRoutine.pieceId
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) =>{
|
|
|
|
defer.success(res.key);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
defer.failure(err);
|
2015-06-23 13:55:05 +02:00
|
|
|
});
|
2015-06-29 17:59:35 +02:00
|
|
|
|
2015-07-14 10:36:37 +02:00
|
|
|
return defer;
|
2015-06-23 13:55:05 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
createBlob(file) {
|
|
|
|
let defer = new fineUploader.Promise();
|
2015-07-15 16:50:32 +02:00
|
|
|
|
2015-07-14 10:52:44 +02:00
|
|
|
window.fetch(this.props.createBlobRoutine.url, {
|
2015-06-23 13:55:05 +02:00
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2015-07-16 18:17:45 +02:00
|
|
|
'X-CSRFToken': getCookie(AppConstants.csrftoken)
|
2015-06-23 13:55:05 +02:00
|
|
|
},
|
|
|
|
credentials: 'include',
|
|
|
|
body: JSON.stringify({
|
|
|
|
'filename': file.name,
|
2015-06-29 17:21:14 +02:00
|
|
|
'key': file.key,
|
2015-07-09 19:46:53 +02:00
|
|
|
'piece_id': this.props.createBlobRoutine.pieceId
|
2015-06-23 13:55:05 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) =>{
|
2015-07-01 10:00:53 +02:00
|
|
|
if(res.otherdata) {
|
|
|
|
file.s3Url = res.otherdata.url_safe;
|
2015-07-01 12:13:05 +02:00
|
|
|
file.s3UrlSafe = res.otherdata.url_safe;
|
|
|
|
} else if(res.digitalwork) {
|
|
|
|
file.s3Url = res.digitalwork.url_safe;
|
|
|
|
file.s3UrlSafe = res.digitalwork.url_safe;
|
2015-07-01 10:00:53 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('Could not find a url to download.');
|
|
|
|
}
|
2015-06-23 13:55:05 +02:00
|
|
|
defer.success(res.key);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2015-07-14 10:36:37 +02:00
|
|
|
defer.failure(err);
|
2015-07-17 15:52:53 +02:00
|
|
|
console.logGlobal(err);
|
2015-06-23 13:55:05 +02:00
|
|
|
});
|
|
|
|
return defer;
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
2015-07-14 10:36:37 +02:00
|
|
|
/* FineUploader specific callback function handlers */
|
|
|
|
|
|
|
|
onComplete(id) {
|
|
|
|
let files = this.state.filesToUpload;
|
|
|
|
files[id].status = 'upload successful';
|
|
|
|
files[id].key = this.state.uploader.getKey(id);
|
|
|
|
|
|
|
|
let newState = React.addons.update(this.state, {
|
|
|
|
filesToUpload: { $set: files }
|
|
|
|
});
|
|
|
|
this.setState(newState);
|
|
|
|
this.createBlob(files[id]);
|
|
|
|
|
|
|
|
// since the form validation props isReadyForFormSubmission, setIsUploadReady and submitKey
|
|
|
|
// are optional, we'll only trigger them when they're actually defined
|
|
|
|
if(this.props.submitKey) {
|
|
|
|
this.props.submitKey(files[id].key);
|
|
|
|
} else {
|
|
|
|
console.warn('You didn\'t define submitKey in as a prop in react-s3-fine-uploader');
|
|
|
|
}
|
|
|
|
|
|
|
|
// for explanation, check comment of if statement above
|
|
|
|
if(this.props.isReadyForFormSubmission && this.props.setIsUploadReady) {
|
|
|
|
// also, lets check if after the completion of this upload,
|
|
|
|
// the form is ready for submission or not
|
|
|
|
if(this.props.isReadyForFormSubmission(this.state.filesToUpload)) {
|
|
|
|
// if so, set uploadstatus to true
|
|
|
|
this.props.setIsUploadReady(true);
|
|
|
|
} else {
|
|
|
|
this.props.setIsUploadReady(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.warn('You didn\'t define the functions isReadyForFormSubmission and/or setIsUploadReady in as a prop in react-s3-fine-uploader');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-14 10:33:15 +02:00
|
|
|
onError() {
|
2015-07-16 19:12:18 +02:00
|
|
|
Raven.captureException('react-fineuploader-error');
|
2015-07-14 10:33:15 +02:00
|
|
|
let notification = new GlobalNotificationModel(this.props.defaultErrorMessage, 'danger', 5000);
|
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
2015-07-15 16:50:32 +02:00
|
|
|
onValidate(data) {
|
|
|
|
if(data.size > this.props.validation.sizeLimit) {
|
|
|
|
this.state.uploader.cancelAll();
|
|
|
|
let notification = new GlobalNotificationModel('Your file is bigger than 10MB', 'danger', 5000);
|
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-29 10:01:54 +02:00
|
|
|
onCancel(id) {
|
|
|
|
this.removeFileWithIdFromFilesToUpload(id);
|
|
|
|
|
2015-06-29 16:00:26 +02:00
|
|
|
let notification = new GlobalNotificationModel('File upload canceled', 'success', 5000);
|
2015-06-29 10:01:54 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
2015-06-29 11:44:16 +02:00
|
|
|
|
2015-07-02 17:57:33 +02:00
|
|
|
// since the form validation props isReadyForFormSubmission, setIsUploadReady and submitKey
|
|
|
|
// are optional, we'll only trigger them when they're actually defined
|
|
|
|
if(this.props.isReadyForFormSubmission && this.props.setIsUploadReady) {
|
|
|
|
if(this.props.isReadyForFormSubmission(this.state.filesToUpload)) {
|
|
|
|
// if so, set uploadstatus to true
|
|
|
|
this.props.setIsUploadReady(true);
|
|
|
|
} else {
|
|
|
|
this.props.setIsUploadReady(false);
|
|
|
|
}
|
2015-06-29 11:44:16 +02:00
|
|
|
} else {
|
2015-07-02 17:57:33 +02:00
|
|
|
console.warn('You didn\'t define the functions isReadyForFormSubmission and/or setIsUploadReady in as a prop in react-s3-fine-uploader');
|
2015-06-29 11:44:16 +02:00
|
|
|
}
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onProgress(id, name, uploadedBytes, totalBytes) {
|
2015-06-30 15:41:39 +02:00
|
|
|
let newState = React.addons.update(this.state, {
|
2015-06-23 10:16:53 +02:00
|
|
|
filesToUpload: { [id]: {
|
2015-06-23 13:55:05 +02:00
|
|
|
progress: { $set: (uploadedBytes / totalBytes) * 100} }
|
2015-06-23 10:16:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.setState(newState);
|
|
|
|
},
|
|
|
|
|
2015-06-30 15:41:39 +02:00
|
|
|
onSessionRequestComplete(response, success) {
|
|
|
|
if(success) {
|
|
|
|
// fetch blobs for images
|
|
|
|
response = response.map((file) => {
|
2015-07-01 10:00:53 +02:00
|
|
|
file.url = file.s3UrlSafe;
|
2015-06-30 15:41:39 +02:00
|
|
|
file.status = 'online';
|
|
|
|
file.progress = 100;
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
|
|
|
|
// add file to filesToUpload
|
|
|
|
let updatedFilesToUpload = this.state.filesToUpload.concat(response);
|
|
|
|
|
|
|
|
// refresh all files ids,
|
|
|
|
updatedFilesToUpload = updatedFilesToUpload.map((file, i) => {
|
|
|
|
file.id = i;
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
|
|
|
|
let newState = React.addons.update(this.state, {filesToUpload: {$set: updatedFilesToUpload}});
|
|
|
|
this.setState(newState);
|
|
|
|
} else {
|
2015-06-30 17:57:20 +02:00
|
|
|
// server has to respond with 204
|
2015-06-30 17:12:51 +02:00
|
|
|
//let notification = new GlobalNotificationModel('Could not load attached files (Further data)', 'danger', 10000);
|
|
|
|
//GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
//
|
|
|
|
//throw new Error('The session request failed', response);
|
2015-06-30 15:41:39 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-30 16:15:50 +02:00
|
|
|
onDeleteComplete(id, xhr, isError) {
|
|
|
|
if(isError) {
|
2015-07-08 10:15:58 +02:00
|
|
|
let notification = new GlobalNotificationModel(getLangText('Couldn\'t delete file'), 'danger', 10000);
|
2015-06-30 16:15:50 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
} else {
|
|
|
|
this.removeFileWithIdFromFilesToUpload(id);
|
|
|
|
|
2015-07-08 10:15:58 +02:00
|
|
|
let notification = new GlobalNotificationModel(getLangText('File deleted'), 'success', 5000);
|
2015-06-30 16:15:50 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
}
|
|
|
|
|
2015-07-02 17:57:33 +02:00
|
|
|
// since the form validation props isReadyForFormSubmission, setIsUploadReady and submitKey
|
|
|
|
// are optional, we'll only trigger them when they're actually defined
|
|
|
|
if(this.props.isReadyForFormSubmission && this.props.setIsUploadReady) {
|
|
|
|
// also, lets check if after the completion of this upload,
|
|
|
|
// the form is ready for submission or not
|
|
|
|
if(this.props.isReadyForFormSubmission(this.state.filesToUpload)) {
|
|
|
|
// if so, set uploadstatus to true
|
|
|
|
this.props.setIsUploadReady(true);
|
|
|
|
} else {
|
|
|
|
this.props.setIsUploadReady(false);
|
|
|
|
}
|
2015-06-30 16:15:50 +02:00
|
|
|
} else {
|
2015-07-02 17:57:33 +02:00
|
|
|
console.warn('You didn\'t define the functions isReadyForFormSubmission and/or setIsUploadReady in as a prop in react-s3-fine-uploader');
|
2015-06-30 16:15:50 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
handleDeleteFile(fileId) {
|
2015-06-30 15:41:39 +02:00
|
|
|
// In some instances (when the file was already uploaded and is just displayed to the user)
|
|
|
|
// fineuploader does not register an id on the file (we do, don't be confused by this!).
|
|
|
|
// Since you can only delete a file by its id, we have to implement this method ourselves
|
2015-06-30 16:15:50 +02:00
|
|
|
//
|
2015-06-30 15:41:39 +02:00
|
|
|
// So, if an id is not present, we delete the file manually
|
|
|
|
// To check which files are already uploaded from previous sessions we check their status.
|
|
|
|
// If they are, it is "online"
|
|
|
|
|
|
|
|
if(this.state.filesToUpload[fileId].status !== 'online') {
|
|
|
|
// delete file from server
|
|
|
|
this.state.uploader.deleteFile(fileId);
|
|
|
|
// this is being continues in onDeleteFile, as
|
|
|
|
// fineuploaders deleteFile does not return a correct callback or
|
|
|
|
// promise
|
|
|
|
} else {
|
|
|
|
let fileToDelete = this.state.filesToUpload[fileId];
|
2015-06-30 17:57:20 +02:00
|
|
|
fileToDelete.status = 'deleted';
|
2015-07-01 10:00:53 +02:00
|
|
|
|
2015-06-30 15:41:39 +02:00
|
|
|
S3Fetcher
|
|
|
|
.deleteFile(fileToDelete.s3Key, fileToDelete.s3Bucket)
|
2015-06-30 16:15:50 +02:00
|
|
|
.then(() => this.onDeleteComplete(fileToDelete.id, null, false))
|
|
|
|
.catch(() => this.onDeleteComplete(fileToDelete.id, null, true));
|
2015-06-30 15:41:39 +02:00
|
|
|
}
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
2015-06-29 10:01:54 +02:00
|
|
|
handleCancelFile(fileId) {
|
|
|
|
this.state.uploader.cancel(fileId);
|
|
|
|
},
|
|
|
|
|
2015-06-29 17:37:14 +02:00
|
|
|
handlePauseFile(fileId) {
|
|
|
|
if(this.state.uploader.pauseUpload(fileId)) {
|
|
|
|
this.setStatusOfFile(fileId, 'paused');
|
|
|
|
} else {
|
|
|
|
throw new Error('File upload could not be paused.');
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
handleResumeFile(fileId) {
|
|
|
|
if(this.state.uploader.continueUpload(fileId)) {
|
|
|
|
this.setStatusOfFile(fileId, 'uploading');
|
|
|
|
} else {
|
|
|
|
throw new Error('File upload could not be resumed.');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
handleUploadFile(files) {
|
2015-06-25 11:12:40 +02:00
|
|
|
// If multiple set and user already uploaded its work,
|
|
|
|
// cancel upload
|
2015-06-29 11:01:45 +02:00
|
|
|
if(!this.props.multiple && this.state.filesToUpload.filter((file) => file.status !== 'deleted' && file.status !== 'canceled').length > 0) {
|
2015-06-25 11:12:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if multiple is set to false and user drops multiple files into the dropzone,
|
|
|
|
// take the first one and notify user that only one file can be submitted
|
|
|
|
if(!this.props.multiple && files.length > 1) {
|
|
|
|
let tempFilesList = [];
|
|
|
|
tempFilesList.push(files[0]);
|
|
|
|
|
|
|
|
// replace filelist with first-element file list
|
|
|
|
files = tempFilesList;
|
2015-07-03 19:08:56 +02:00
|
|
|
// TOOD translate?
|
2015-07-08 10:15:58 +02:00
|
|
|
let notification = new GlobalNotificationModel(getLangText('Only one file allowed (took first one)'), 'danger', 10000);
|
2015-06-25 11:12:40 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
}
|
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
this.state.uploader.addFiles(files);
|
|
|
|
let oldFiles = this.state.filesToUpload;
|
|
|
|
let oldAndNewFiles = this.state.uploader.getUploads();
|
|
|
|
|
|
|
|
// Add fineuploader specific information to new files
|
|
|
|
for(let i = 0; i < oldAndNewFiles.length; i++) {
|
|
|
|
for(let j = 0; j < files.length; j++) {
|
|
|
|
if(oldAndNewFiles[i].originalName === files[j].name) {
|
|
|
|
oldAndNewFiles[i].progress = 0;
|
|
|
|
oldAndNewFiles[i].type = files[j].type;
|
|
|
|
oldAndNewFiles[i].url = URL.createObjectURL(files[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and re-add fineuploader specific information for old files as well
|
|
|
|
for(let i = 0; i < oldAndNewFiles.length; i++) {
|
|
|
|
for(let j = 0; j < oldFiles.length; j++) {
|
|
|
|
if(oldAndNewFiles[i].originalName === oldFiles[j].name) {
|
2015-06-23 13:55:05 +02:00
|
|
|
oldAndNewFiles[i].progress = oldFiles[j].progress;
|
2015-06-23 10:16:53 +02:00
|
|
|
oldAndNewFiles[i].type = oldFiles[j].type;
|
|
|
|
oldAndNewFiles[i].url = oldFiles[j].url;
|
2015-06-29 16:00:26 +02:00
|
|
|
oldAndNewFiles[i].key = oldFiles[j].key;
|
2015-06-23 10:16:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 11:01:45 +02:00
|
|
|
// set the new file array
|
2015-06-23 10:16:53 +02:00
|
|
|
let newState = React.addons.update(this.state, {
|
|
|
|
filesToUpload: { $set: oldAndNewFiles }
|
|
|
|
});
|
|
|
|
this.setState(newState);
|
|
|
|
},
|
|
|
|
|
2015-06-29 10:01:54 +02:00
|
|
|
removeFileWithIdFromFilesToUpload(fileId) {
|
|
|
|
// also, sync files from state with the ones from fineuploader
|
|
|
|
let filesToUpload = JSON.parse(JSON.stringify(this.state.filesToUpload));
|
|
|
|
|
|
|
|
// splice because I can
|
|
|
|
filesToUpload.splice(fileId, 1);
|
|
|
|
|
|
|
|
// set state
|
|
|
|
let newState = React.addons.update(this.state, {
|
|
|
|
filesToUpload: { $set: filesToUpload }
|
|
|
|
});
|
|
|
|
this.setState(newState);
|
|
|
|
},
|
|
|
|
|
2015-06-29 17:37:14 +02:00
|
|
|
setStatusOfFile(fileId, status) {
|
|
|
|
// also, sync files from state with the ones from fineuploader
|
|
|
|
let filesToUpload = JSON.parse(JSON.stringify(this.state.filesToUpload));
|
|
|
|
|
|
|
|
// splice because I can
|
|
|
|
filesToUpload[fileId].status = status;
|
|
|
|
|
|
|
|
// set state
|
|
|
|
let newState = React.addons.update(this.state, {
|
|
|
|
filesToUpload: { $set: filesToUpload }
|
|
|
|
});
|
|
|
|
this.setState(newState);
|
|
|
|
},
|
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
render() {
|
|
|
|
return (
|
2015-06-29 16:00:26 +02:00
|
|
|
<div>
|
|
|
|
<FileDragAndDrop
|
|
|
|
className="file-drag-and-drop"
|
|
|
|
onDrop={this.handleUploadFile}
|
|
|
|
filesToUpload={this.state.filesToUpload}
|
|
|
|
handleDeleteFile={this.handleDeleteFile}
|
|
|
|
handleCancelFile={this.handleCancelFile}
|
2015-06-29 17:37:14 +02:00
|
|
|
handlePauseFile={this.handlePauseFile}
|
|
|
|
handleResumeFile={this.handleResumeFile}
|
2015-06-29 16:00:26 +02:00
|
|
|
multiple={this.props.multiple}
|
2015-06-30 13:53:02 +02:00
|
|
|
areAssetsDownloadable={this.props.areAssetsDownloadable}
|
2015-07-01 12:13:05 +02:00
|
|
|
areAssetsEditable={this.props.areAssetsEditable}
|
|
|
|
dropzoneInactive={!this.props.areAssetsEditable || !this.props.multiple && this.state.filesToUpload.filter((file) => file.status !== 'deleted' && file.status !== 'canceled' && file.size !== -1).length > 0} />
|
2015-06-29 16:00:26 +02:00
|
|
|
</div>
|
2015-06-23 10:16:53 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export default ReactS3FineUploader;
|