From 80861813e9f0645e0070b8c9bd3d2f9ba5cc5590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Thu, 23 Jul 2015 15:57:25 +0200 Subject: [PATCH] documente and implement routine in react fine uploader --- .../react_s3_fine_uploader.js | 39 ++++++++++++++++++- js/utils/file_utils.js | 2 +- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/js/components/ascribe_uploader/react_s3_fine_uploader.js b/js/components/ascribe_uploader/react_s3_fine_uploader.js index dd66b2f3..438741c8 100644 --- a/js/components/ascribe_uploader/react_s3_fine_uploader.js +++ b/js/components/ascribe_uploader/react_s3_fine_uploader.js @@ -16,6 +16,8 @@ import GlobalNotificationActions from '../../actions/global_notification_actions import AppConstants from '../../constants/application_constants'; +import { computeHashOfFile } from '../../utils/file_utils'; + var ReactS3FineUploader = React.createClass({ propTypes: { @@ -489,9 +491,44 @@ var ReactS3FineUploader = React.createClass({ GlobalNotificationActions.appendGlobalNotification(notification); } + // 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. + // This if statement essentially takes care of that solution. + if(this.props.localHashing) { + - // routine for adding all the files submitted to fineuploader + let convertedFilePromises = []; + // "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++) { + + // 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 + Promise.all(convertedFilePromises) + .then((convertedFiles) => { + // actually replacing all files with their txt-hash representative + files = convertedFiles; + }) + .catch((err) => { + // if we're running into an error during the hash creation, we'll tell the user + console.logGlobal(err); + let notification = new GlobalNotificationModel(err.message, 'danger', 5000); + GlobalNotificationActions.appendGlobalNotification(notification); + }); + } + + // routine for adding all the files submitted to fineuploader for actual uploading them + // to the server this.state.uploader.addFiles(files); + let oldFiles = this.state.filesToUpload; let oldAndNewFiles = this.state.uploader.getUploads(); // Add fineuploader specific information to new files diff --git a/js/utils/file_utils.js b/js/utils/file_utils.js index 516543b5..42999603 100644 --- a/js/utils/file_utils.js +++ b/js/utils/file_utils.js @@ -50,7 +50,7 @@ export function computeHashOfFile(file) { }.bind(this); fileReader.onerror = function () { - reject(new Error('We weren\' able to hash your file locally. Try to upload it manually or consider contact us.')); + reject(new Error('We weren\'t able to hash your file locally. Try to upload it manually or consider contact us.')); }; function loadNext() {