2015-06-23 10:16:53 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react/addons';
|
2015-07-27 15:34:45 +02:00
|
|
|
import Router from 'react-router';
|
2015-07-24 13:49:16 +02:00
|
|
|
import Q from 'q';
|
2015-06-23 10:16:53 +02:00
|
|
|
|
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-07-23 15:57:25 +02:00
|
|
|
import { computeHashOfFile } from '../../utils/file_utils';
|
2015-09-15 13:30:24 +02:00
|
|
|
import { displayValidFilesFilter } from './react_s3_fine_uploader_utils';
|
|
|
|
import { getCookie } from '../../utils/fetch_api_utils';
|
|
|
|
import { getLangText } from '../../utils/lang_utils';
|
2015-07-23 15:57:25 +02:00
|
|
|
|
2015-09-15 13:30:24 +02:00
|
|
|
let ReactS3FineUploader = React.createClass({
|
2015-06-23 10:16:53 +02:00
|
|
|
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-08-21 11:11:31 +02:00
|
|
|
uploadStarted: React.PropTypes.func,
|
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,
|
2015-07-21 11:09:21 +02:00
|
|
|
defaultErrorMessage: React.PropTypes.string,
|
2015-07-29 09:35:14 +02:00
|
|
|
onInactive: React.PropTypes.func,
|
2015-07-23 15:27:14 +02:00
|
|
|
|
|
|
|
// We encountered some cases where people had difficulties to upload their
|
|
|
|
// works to ascribe due to a slow internet connection.
|
|
|
|
// One solution we found in the process of tackling this problem was to hash
|
|
|
|
// the file in the browser using md5 and then uploading the resulting text document instead
|
|
|
|
// of the actual file.
|
|
|
|
// This boolean essentially enables that behavior
|
2015-07-27 15:34:45 +02:00
|
|
|
enableLocalHashing: React.PropTypes.bool,
|
|
|
|
|
|
|
|
// automatically injected by React-Router
|
|
|
|
query: React.PropTypes.object
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
2015-07-27 15:34:45 +02:00
|
|
|
mixins: [Router.State],
|
|
|
|
|
2015-06-29 10:00:26 +02:00
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
autoUpload: true,
|
|
|
|
debug: false,
|
|
|
|
objectProperties: {
|
|
|
|
acl: 'public-read',
|
|
|
|
bucket: 'ascribe0'
|
|
|
|
},
|
|
|
|
request: {
|
2015-09-04 15:54:49 +02:00
|
|
|
//endpoint: 'https://www.ascribe.io.global.prod.fastly.net',
|
2015-06-29 10:00:26 +02:00
|
|
|
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: {
|
2015-07-23 18:14:17 +02:00
|
|
|
enabled: true,
|
|
|
|
concurrent: {
|
|
|
|
enabled: true
|
|
|
|
}
|
2015-06-29 10:00:26 +02:00
|
|
|
},
|
|
|
|
resume: {
|
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
retry: {
|
|
|
|
enableAuto: false
|
|
|
|
},
|
|
|
|
session: {
|
|
|
|
endpoint: null
|
|
|
|
},
|
|
|
|
messages: {
|
2015-07-23 17:17:48 +02:00
|
|
|
unsupportedBrowser: '<h3>' + getLangText('Upload is not functional in IE7 as IE7 has no support for CORS!') + '</h3>'
|
2015-06-29 10:00:26 +02:00
|
|
|
},
|
|
|
|
formatFileName: function(name){// fix maybe
|
|
|
|
if (name !== undefined && name.length > 26) {
|
|
|
|
name = name.slice(0, 15) + '...' + name.slice(-15);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
},
|
2015-08-18 13:15:22 +02:00
|
|
|
multiple: false,
|
2015-07-23 17:17:48 +02:00
|
|
|
defaultErrorMessage: getLangText('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-23 17:05:52 +02:00
|
|
|
csrfToken: getCookie(AppConstants.csrftoken),
|
2015-08-03 18:35:56 +02:00
|
|
|
|
2015-07-24 16:17:27 +02:00
|
|
|
// -1: aborted
|
|
|
|
// -2: uninitialized
|
2015-08-03 18:35:56 +02:00
|
|
|
hashingProgress: -2,
|
|
|
|
|
|
|
|
// this is for logging
|
|
|
|
chunks: {}
|
2015-06-23 13:55:05 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-07-08 10:15:58 +02:00
|
|
|
componentWillUpdate() {
|
2015-07-27 15:34:45 +02:00
|
|
|
// since the csrf header is defined in this component's props,
|
|
|
|
// everytime the csrf cookie is changed we'll need to reinitalize
|
|
|
|
// fineuploader and update the actual csrf token
|
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,
|
2015-08-03 18:35:56 +02:00
|
|
|
onUploadChunk: this.onUploadChunk,
|
|
|
|
onUploadChunkSuccess: this.onUploadChunkSuccess
|
2015-06-23 10:16:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2015-06-23 13:55:05 +02:00
|
|
|
|
2015-09-10 11:22:42 +02:00
|
|
|
// Resets the whole react fineuploader component to its initial state
|
|
|
|
reset() {
|
|
|
|
// Cancel all currently ongoing uploads
|
|
|
|
this.state.uploader.cancelAll();
|
|
|
|
|
|
|
|
// and reset component in general
|
|
|
|
this.state.uploader.reset();
|
|
|
|
|
|
|
|
// proclaim that upload is not ready
|
|
|
|
this.props.setIsUploadReady(false);
|
|
|
|
|
|
|
|
// reset internal data structures of component
|
|
|
|
this.setState(this.getInitialState());
|
|
|
|
},
|
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
requestKey(fileId) {
|
|
|
|
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-27 09:28:50 +02:00
|
|
|
return Q.Promise((resolve, reject) => {
|
|
|
|
window.fetch(this.props.keyRoutine.url, {
|
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRFToken': getCookie(AppConstants.csrftoken)
|
|
|
|
},
|
|
|
|
credentials: 'include',
|
|
|
|
body: JSON.stringify({
|
|
|
|
'filename': filename,
|
|
|
|
'category': this.props.keyRoutine.fileClass,
|
|
|
|
'uuid': uuid,
|
|
|
|
'piece_id': this.props.keyRoutine.pieceId
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
return res.json();
|
2015-07-14 10:36:37 +02:00
|
|
|
})
|
2015-07-27 09:28:50 +02:00
|
|
|
.then((res) =>{
|
|
|
|
resolve(res.key);
|
2015-07-14 10:36:37 +02:00
|
|
|
})
|
2015-07-27 09:28:50 +02:00
|
|
|
.catch((err) => {
|
2015-08-03 18:35:56 +02:00
|
|
|
console.logGlobal(err, false, {
|
|
|
|
files: this.state.filesToUpload,
|
|
|
|
chunks: this.state.chunks
|
|
|
|
});
|
2015-07-27 09:28:50 +02:00
|
|
|
reject(err);
|
|
|
|
});
|
2015-06-23 13:55:05 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
createBlob(file) {
|
2015-07-27 09:28:50 +02:00
|
|
|
return Q.Promise((resolve, reject) => {
|
|
|
|
window.fetch(this.props.createBlobRoutine.url, {
|
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRFToken': getCookie(AppConstants.csrftoken)
|
|
|
|
},
|
|
|
|
credentials: 'include',
|
|
|
|
body: JSON.stringify({
|
|
|
|
'filename': file.name,
|
|
|
|
'key': file.key,
|
|
|
|
'piece_id': this.props.createBlobRoutine.pieceId
|
|
|
|
})
|
2015-06-23 13:55:05 +02:00
|
|
|
})
|
2015-07-27 09:28:50 +02:00
|
|
|
.then((res) => {
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) =>{
|
|
|
|
if(res.otherdata) {
|
|
|
|
file.s3Url = res.otherdata.url_safe;
|
|
|
|
file.s3UrlSafe = res.otherdata.url_safe;
|
|
|
|
} else if(res.digitalwork) {
|
|
|
|
file.s3Url = res.digitalwork.url_safe;
|
|
|
|
file.s3UrlSafe = res.digitalwork.url_safe;
|
2015-08-31 17:29:43 +02:00
|
|
|
} else if(res.contractblob) {
|
|
|
|
file.s3Url = res.contractblob.url_safe;
|
|
|
|
file.s3UrlSafe = res.contractblob.url_safe;
|
2015-07-27 09:28:50 +02:00
|
|
|
} else {
|
|
|
|
throw new Error(getLangText('Could not find a url to download.'));
|
|
|
|
}
|
2015-07-27 14:21:26 +02:00
|
|
|
resolve(res);
|
2015-07-27 09:28:50 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2015-08-03 18:35:56 +02:00
|
|
|
console.logGlobal(err, false, {
|
|
|
|
files: this.state.filesToUpload,
|
|
|
|
chunks: this.state.chunks
|
|
|
|
});
|
2015-07-27 09:28:50 +02:00
|
|
|
reject(err);
|
|
|
|
});
|
2015-06-23 13:55:05 +02:00
|
|
|
});
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
2015-07-14 10:36:37 +02:00
|
|
|
/* FineUploader specific callback function handlers */
|
|
|
|
|
2015-08-03 18:35:56 +02:00
|
|
|
onUploadChunk(id, name, chunkData) {
|
|
|
|
|
|
|
|
let chunks = this.state.chunks;
|
|
|
|
|
|
|
|
chunks[id + '-' + chunkData.startByte + '-' + chunkData.endByte] = {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
chunkData,
|
|
|
|
completed: false
|
|
|
|
};
|
|
|
|
|
2015-09-07 12:02:01 +02:00
|
|
|
let startedChunks = React.addons.update(this.state.startedChunks, { $set: chunks });
|
2015-08-03 18:35:56 +02:00
|
|
|
|
2015-09-07 12:02:01 +02:00
|
|
|
this.setState({ startedChunks });
|
2015-08-03 18:35:56 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onUploadChunkSuccess(id, chunkData, responseJson, xhr) {
|
|
|
|
|
|
|
|
let chunks = this.state.chunks;
|
|
|
|
let chunkKey = id + '-' + chunkData.startByte + '-' + chunkData.endByte;
|
|
|
|
|
|
|
|
if(chunks[chunkKey]) {
|
|
|
|
chunks[chunkKey].completed = true;
|
|
|
|
chunks[chunkKey].responseJson = responseJson;
|
|
|
|
chunks[chunkKey].xhr = xhr;
|
|
|
|
|
2015-09-07 12:02:01 +02:00
|
|
|
let startedChunks = React.addons.update(this.state.startedChunks, { $set: chunks });
|
2015-08-03 18:35:56 +02:00
|
|
|
|
2015-09-07 12:02:01 +02:00
|
|
|
this.setState({ startedChunks });
|
2015-08-03 18:35:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-08-03 10:54:34 +02:00
|
|
|
onComplete(id, name, res, xhr) {
|
|
|
|
// there has been an issue with the server's connection
|
2015-09-04 15:54:49 +02:00
|
|
|
if((xhr && xhr.status === 0) || res.error) {
|
|
|
|
console.logGlobal(new Error(res.error || 'Complete was called but there wasn\t a success'), false, {
|
2015-08-03 18:50:14 +02:00
|
|
|
files: this.state.filesToUpload,
|
|
|
|
chunks: this.state.chunks
|
|
|
|
});
|
2015-09-04 17:31:58 +02:00
|
|
|
} else {
|
|
|
|
let files = this.state.filesToUpload;
|
2015-08-03 18:50:14 +02:00
|
|
|
|
2015-09-04 17:31:58 +02:00
|
|
|
// Set the state of the completed file to 'upload successful' in order to
|
|
|
|
// remove it from the GUI
|
|
|
|
files[id].status = 'upload successful';
|
|
|
|
files[id].key = this.state.uploader.getKey(id);
|
2015-07-14 10:36:37 +02:00
|
|
|
|
2015-09-14 11:14:02 +02:00
|
|
|
let filesToUpload = React.addons.update(this.state.filesToUpload, { $set: files });
|
|
|
|
this.setState({ filesToUpload });
|
2015-07-14 10:36:37 +02:00
|
|
|
|
2015-09-04 17:31:58 +02:00
|
|
|
// Only after the blob has been created server-side, we can make the form submittable.
|
|
|
|
this.createBlob(files[id])
|
|
|
|
.then(() => {
|
|
|
|
// 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);
|
2015-07-27 09:33:31 +02:00
|
|
|
} else {
|
2015-09-04 17:31:58 +02:00
|
|
|
console.warn('You didn\'t define submitKey in as a prop in react-s3-fine-uploader');
|
2015-07-27 09:33:31 +02:00
|
|
|
}
|
2015-09-04 17:31:58 +02:00
|
|
|
|
|
|
|
// 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');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.logGlobal(err, false, {
|
|
|
|
files: this.state.filesToUpload,
|
|
|
|
chunks: this.state.chunks
|
|
|
|
});
|
|
|
|
let notification = new GlobalNotificationModel(err.message, 'danger', 5000);
|
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
2015-08-03 18:35:56 +02:00
|
|
|
});
|
2015-09-04 17:31:58 +02:00
|
|
|
}
|
2015-07-14 10:36:37 +02:00
|
|
|
},
|
|
|
|
|
2015-07-28 16:24:18 +02:00
|
|
|
onError(id, name, errorReason) {
|
2015-08-03 18:35:56 +02:00
|
|
|
console.logGlobal(errorReason, false, {
|
|
|
|
files: this.state.filesToUpload,
|
|
|
|
chunks: this.state.chunks
|
|
|
|
});
|
2015-08-03 11:09:31 +02:00
|
|
|
this.state.uploader.cancelAll();
|
|
|
|
|
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-08-24 13:57:02 +02:00
|
|
|
isFileValid(file) {
|
|
|
|
if(file.size > this.props.validation.sizeLimit) {
|
2015-07-23 17:17:48 +02:00
|
|
|
|
|
|
|
let fileSizeInMegaBytes = this.props.validation.sizeLimit / 1000000;
|
2015-08-24 13:57:02 +02:00
|
|
|
|
|
|
|
let notification = new GlobalNotificationModel(getLangText('A file you submitted is bigger than ' + fileSizeInMegaBytes + 'MB.'), 'danger', 5000);
|
2015-07-15 16:50:32 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
2015-08-24 13:57:02 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
2015-07-15 16:50:32 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-29 10:01:54 +02:00
|
|
|
onCancel(id) {
|
2015-08-11 11:39:58 +02:00
|
|
|
// when a upload is canceled, we need to update this components file array
|
|
|
|
this.setStatusOfFile(id, 'canceled');
|
2015-06-29 10:01:54 +02:00
|
|
|
|
2015-07-23 17:17:48 +02:00
|
|
|
let notification = new GlobalNotificationModel(getLangText('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-09-07 12:02:01 +02:00
|
|
|
|
|
|
|
return true;
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onProgress(id, name, uploadedBytes, totalBytes) {
|
2015-09-07 12:02:01 +02:00
|
|
|
let filesToUpload = React.addons.update(this.state.filesToUpload, {
|
|
|
|
[id]: {
|
|
|
|
progress: { $set: (uploadedBytes / totalBytes) * 100}
|
2015-06-23 10:16:53 +02:00
|
|
|
}
|
|
|
|
});
|
2015-09-07 12:02:01 +02:00
|
|
|
this.setState({ filesToUpload });
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2015-09-07 12:02:01 +02:00
|
|
|
let filesToUpload = React.addons.update(this.state.filesToUpload, {$set: updatedFilesToUpload});
|
|
|
|
|
|
|
|
this.setState({filesToUpload });
|
2015-06-30 15:41:39 +02:00
|
|
|
} 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-09-09 17:21:55 +02:00
|
|
|
this.setStatusOfFile(id, 'online');
|
|
|
|
|
|
|
|
let notification = new GlobalNotificationModel(getLangText('There was an error deleting your file.'), 'danger', 10000);
|
2015-06-30 16:15:50 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
} else {
|
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-09-09 17:21:55 +02:00
|
|
|
// We set the files state to 'deleted' immediately, so that the user is not confused with
|
|
|
|
// the unresponsiveness of the UI
|
|
|
|
//
|
|
|
|
// If there is an error during the deletion, we will just change the status back to 'online'
|
|
|
|
// and display an error message
|
|
|
|
this.setStatusOfFile(fileId, 'deleted');
|
|
|
|
|
2015-08-24 12:20:54 +02:00
|
|
|
// In some instances (when the file was already uploaded and is just displayed to the user
|
2015-08-31 11:05:33 +02:00
|
|
|
// - for example in the contract or additional files dialog)
|
2015-06-30 15:41:39 +02:00
|
|
|
// 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);
|
2015-08-11 11:39:58 +02:00
|
|
|
// this is being continued in onDeleteFile, as
|
2015-06-30 15:41:39 +02:00
|
|
|
// fineuploaders deleteFile does not return a correct callback or
|
|
|
|
// promise
|
|
|
|
} else {
|
|
|
|
let fileToDelete = this.state.filesToUpload[fileId];
|
|
|
|
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 {
|
2015-07-23 17:17:48 +02:00
|
|
|
throw new Error(getLangText('File upload could not be paused.'));
|
2015-06-29 17:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
handleResumeFile(fileId) {
|
|
|
|
if(this.state.uploader.continueUpload(fileId)) {
|
|
|
|
this.setStatusOfFile(fileId, 'uploading');
|
|
|
|
} else {
|
2015-07-23 17:17:48 +02:00
|
|
|
throw new Error(getLangText('File upload could not be resumed.'));
|
2015-06-29 17:37:14 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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-09-10 09:54:02 +02:00
|
|
|
if(!this.props.multiple && this.state.filesToUpload.filter(displayValidFilesFilter).length > 0) {
|
2015-06-25 11:12:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:57:02 +02:00
|
|
|
// validate each submitted file if it fits the file size
|
|
|
|
let validFiles = [];
|
|
|
|
for(let i = 0; i < files.length; i++) {
|
|
|
|
if(this.isFileValid(files[i])) {
|
|
|
|
validFiles.push(files[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// override standard files list with only valid files
|
|
|
|
files = validFiles;
|
|
|
|
|
2015-08-21 11:11:31 +02:00
|
|
|
// Call this method to signal the outside component that an upload is in progress
|
2015-09-14 11:36:24 +02:00
|
|
|
if(typeof this.props.uploadStarted === 'function' && files.length > 0) {
|
2015-08-21 11:11:31 +02:00
|
|
|
this.props.uploadStarted();
|
|
|
|
}
|
|
|
|
|
2015-06-25 11:12:40 +02:00
|
|
|
// 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-07-23 15:57:25 +02:00
|
|
|
// As mentioned already in the propTypes declaration, in some instances we need to calculate the
|
|
|
|
// md5 hash of a file locally and just upload a txt file containing that hash.
|
2015-07-27 15:34:45 +02:00
|
|
|
//
|
|
|
|
// In the view this only happens when the user is allowed to do local hashing as well
|
|
|
|
// as when the correct query parameter is present in the url ('hash' and not 'upload')
|
|
|
|
let queryParams = this.getQuery();
|
|
|
|
if(this.props.enableLocalHashing && queryParams && queryParams.method === 'hash') {
|
2015-07-23 17:05:52 +02:00
|
|
|
|
2015-07-23 15:57:25 +02:00
|
|
|
let convertedFilePromises = [];
|
2015-07-24 14:39:04 +02:00
|
|
|
let overallFileSize = 0;
|
2015-07-23 15:57:25 +02:00
|
|
|
// "files" is not a classical Javascript array but a Javascript FileList, therefore
|
|
|
|
// we can not use map to convert values
|
|
|
|
for(let i = 0; i < files.length; i++) {
|
|
|
|
|
2015-07-24 14:39:04 +02:00
|
|
|
// for calculating the overall progress of all submitted files
|
|
|
|
// we'll need to calculate the overall sum of all files' sizes
|
|
|
|
overallFileSize += files[i].size;
|
|
|
|
|
|
|
|
// also, we need to set the files' initial progress value
|
|
|
|
files[i].progress = 0;
|
|
|
|
|
2015-07-23 15:57:25 +02:00
|
|
|
// since the actual computation of a file's hash is an async task ,
|
|
|
|
// we're using promises to handle that
|
|
|
|
let hashedFilePromise = computeHashOfFile(files[i]);
|
|
|
|
convertedFilePromises.push(hashedFilePromise);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// To react after the computation of all files, we define the resolvement
|
|
|
|
// with the all function for iterables and essentially replace all original files
|
|
|
|
// with their txt representative
|
2015-07-24 13:44:28 +02:00
|
|
|
Q.all(convertedFilePromises)
|
2015-07-24 16:29:57 +02:00
|
|
|
.progress(({index, value: {progress, reject}}) => {
|
2015-07-24 16:17:27 +02:00
|
|
|
|
|
|
|
// hashing progress has been aborted from outside
|
|
|
|
// To get out of the executing, we need to call reject from the
|
|
|
|
// inside of the promise's execution.
|
|
|
|
// This is why we're passing (along with value) a function that essentially
|
|
|
|
// just does that (calling reject(err))
|
|
|
|
//
|
|
|
|
// In the promises catch method, we're then checking if the interruption
|
|
|
|
// was due to that error or another generic one.
|
|
|
|
if(this.state.hashingProgress === -1) {
|
2015-07-24 16:29:57 +02:00
|
|
|
reject(new Error(getLangText('Hashing canceled')));
|
2015-07-24 16:17:27 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 14:39:04 +02:00
|
|
|
// update file's progress
|
2015-07-24 16:17:27 +02:00
|
|
|
files[index].progress = progress;
|
2015-07-24 14:39:04 +02:00
|
|
|
|
2015-07-24 15:13:20 +02:00
|
|
|
// calculate weighted average for overall progress of all
|
|
|
|
// currently hashing files
|
2015-07-24 14:39:04 +02:00
|
|
|
let overallHashingProgress = 0;
|
|
|
|
for(let i = 0; i < files.length; i++) {
|
|
|
|
|
2015-07-24 16:17:27 +02:00
|
|
|
let filesSliceOfOverall = files[i].size / overallFileSize;
|
2015-07-24 14:39:04 +02:00
|
|
|
overallHashingProgress += filesSliceOfOverall * files[i].progress;
|
2015-07-24 16:17:27 +02:00
|
|
|
|
2015-07-24 14:39:04 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 15:13:20 +02:00
|
|
|
// Multiply by 100, since react-progressbar expects decimal numbers
|
|
|
|
this.setState({ hashingProgress: overallHashingProgress * 100});
|
2015-07-24 14:39:04 +02:00
|
|
|
|
|
|
|
})
|
2015-07-23 15:57:25 +02:00
|
|
|
.then((convertedFiles) => {
|
2015-07-23 17:05:52 +02:00
|
|
|
|
2015-07-24 15:13:20 +02:00
|
|
|
// clear hashing progress, since its done
|
2015-07-24 16:17:27 +02:00
|
|
|
this.setState({ hashingProgress: -2});
|
2015-07-24 15:13:20 +02:00
|
|
|
|
2015-07-23 15:57:25 +02:00
|
|
|
// actually replacing all files with their txt-hash representative
|
|
|
|
files = convertedFiles;
|
2015-07-23 16:47:32 +02:00
|
|
|
|
|
|
|
// routine for adding all the files submitted to fineuploader for actual uploading them
|
|
|
|
// to the server
|
|
|
|
this.state.uploader.addFiles(files);
|
|
|
|
this.synchronizeFileLists(files);
|
2015-07-23 17:05:52 +02:00
|
|
|
|
2015-07-23 15:57:25 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2015-07-24 16:17:27 +02:00
|
|
|
// If the error is that hashing has been canceled, we want to display a success
|
|
|
|
// message instead of a danger message
|
|
|
|
let typeOfMessage = 'danger';
|
|
|
|
|
|
|
|
if(err.message === getLangText('Hashing canceled')) {
|
|
|
|
typeOfMessage = 'success';
|
|
|
|
this.setState({ hashingProgress: -2 });
|
|
|
|
} else {
|
|
|
|
// if there was a more generic error, we also log it
|
|
|
|
console.logGlobal(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let notification = new GlobalNotificationModel(err.message, typeOfMessage, 5000);
|
2015-07-23 15:57:25 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
});
|
2015-07-23 15:27:14 +02:00
|
|
|
|
2015-07-23 16:47:32 +02:00
|
|
|
// if we're not hashing the files locally, we're just going to hand them over to fineuploader
|
|
|
|
// to upload them to the server
|
|
|
|
} else {
|
2015-08-24 14:16:13 +02:00
|
|
|
if(files.length > 0) {
|
|
|
|
this.state.uploader.addFiles(files);
|
|
|
|
this.synchronizeFileLists(files);
|
|
|
|
}
|
2015-07-23 16:47:32 +02:00
|
|
|
}
|
|
|
|
},
|
2015-07-23 15:57:25 +02:00
|
|
|
|
2015-07-24 16:17:27 +02:00
|
|
|
handleCancelHashing() {
|
|
|
|
// Every progress tick of the hashing function in handleUploadFile there is a
|
|
|
|
// check if this.state.hashingProgress is -1. If so, there is an error thrown that cancels
|
|
|
|
// the hashing of all files immediately.
|
|
|
|
this.setState({ hashingProgress: -1 });
|
|
|
|
},
|
|
|
|
|
2015-07-23 16:47:32 +02:00
|
|
|
// ReactFineUploader is essentially just a react layer around s3 fineuploader.
|
|
|
|
// However, since we need to display the status of a file (progress, uploading) as well as
|
|
|
|
// be able to execute actions on a currently uploading file we need to exactly sync the file list
|
|
|
|
// fineuploader is keeping internally.
|
|
|
|
//
|
|
|
|
// Unfortunately though fineuploader is not keeping all of a File object's properties after
|
|
|
|
// submitting them via .addFiles (it deletes the type, key as well as the ObjectUrl (which we need for
|
|
|
|
// displaying a thumbnail)), we need to readd them manually after each file that gets submitted
|
|
|
|
// to the dropzone.
|
|
|
|
// This method is essentially taking care of all these steps.
|
|
|
|
synchronizeFileLists(files) {
|
2015-06-23 10:16:53 +02:00
|
|
|
let oldFiles = this.state.filesToUpload;
|
|
|
|
let oldAndNewFiles = this.state.uploader.getUploads();
|
2015-09-09 17:21:55 +02:00
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
// 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++) {
|
2015-09-09 17:21:55 +02:00
|
|
|
|
|
|
|
// EXCEPTION:
|
|
|
|
//
|
|
|
|
// Files do not necessarily come from the user's hard drive but can also be fetched
|
|
|
|
// from Amazon S3. This is handled in onSessionRequestComplete.
|
|
|
|
//
|
|
|
|
// If the user deletes one of those files, then fineuploader will still keep it in his
|
|
|
|
// files array but with key, progress undefined and size === -1 but
|
|
|
|
// status === 'upload successful'.
|
|
|
|
// This poses a problem as we depend on the amount of files that have
|
|
|
|
// status === 'upload successful', therefore once the file is synced,
|
|
|
|
// we need to tag its status as 'deleted' (which basically happens here)
|
|
|
|
if(oldAndNewFiles[i].size === -1 && (!oldAndNewFiles[i].progress || oldAndNewFiles[i].progress === 0)) {
|
|
|
|
oldAndNewFiles[i].status = 'deleted';
|
|
|
|
}
|
|
|
|
|
2015-06-23 10:16:53 +02:00
|
|
|
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-09-07 12:02:01 +02:00
|
|
|
let filesToUpload = React.addons.update(this.state.filesToUpload, { $set: oldAndNewFiles });
|
|
|
|
|
|
|
|
this.setState({ filesToUpload });
|
2015-06-23 10:16:53 +02:00
|
|
|
},
|
|
|
|
|
2015-06-29 17:37:14 +02:00
|
|
|
setStatusOfFile(fileId, status) {
|
2015-09-04 15:13:38 +02:00
|
|
|
let changeSet = {};
|
2015-06-29 17:37:14 +02:00
|
|
|
|
2015-08-11 11:39:58 +02:00
|
|
|
if(status === 'deleted' || status === 'canceled') {
|
2015-09-04 15:13:38 +02:00
|
|
|
changeSet.progress = { $set: 0 };
|
2015-08-11 11:39:58 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 15:13:38 +02:00
|
|
|
changeSet.status = { $set: status };
|
2015-08-11 11:39:58 +02:00
|
|
|
|
2015-09-07 12:02:01 +02:00
|
|
|
let filesToUpload = React.addons.update(this.state.filesToUpload, { [fileId]: changeSet });
|
2015-09-04 15:13:38 +02:00
|
|
|
|
2015-09-07 12:02:01 +02:00
|
|
|
this.setState({ filesToUpload });
|
2015-06-29 17:37:14 +02:00
|
|
|
},
|
2015-07-23 11:45:25 +02:00
|
|
|
|
2015-07-27 16:00:54 +02:00
|
|
|
isDropzoneInactive() {
|
|
|
|
let filesToDisplay = this.state.filesToUpload.filter((file) => file.status !== 'deleted' && file.status !== 'canceled' && file.size !== -1);
|
|
|
|
let queryParams = this.getQuery();
|
|
|
|
|
|
|
|
if((this.props.enableLocalHashing && !queryParams.method) || !this.props.areAssetsEditable || !this.props.multiple && filesToDisplay.length > 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2015-06-29 17:37:14 +02:00
|
|
|
|
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-07-24 16:17:27 +02:00
|
|
|
handleCancelHashing={this.handleCancelHashing}
|
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}
|
2015-07-29 09:35:14 +02:00
|
|
|
onInactive={this.props.onInactive}
|
2015-07-27 16:00:54 +02:00
|
|
|
dropzoneInactive={this.isDropzoneInactive()}
|
2015-07-27 09:23:01 +02:00
|
|
|
hashingProgress={this.state.hashingProgress}
|
2015-07-27 15:34:45 +02:00
|
|
|
enableLocalHashing={this.props.enableLocalHashing} />
|
2015-06-29 16:00:26 +02:00
|
|
|
</div>
|
2015-06-23 10:16:53 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export default ReactS3FineUploader;
|