mirror of
https://github.com/ascribe/onion.git
synced 2025-01-03 10:25:08 +01:00
converte computeHashOfFile to a promise based solution
This commit is contained in:
parent
71fcdff874
commit
33a179cfe2
@ -34,7 +34,8 @@
|
|||||||
"globals": {
|
"globals": {
|
||||||
"Intercom": true,
|
"Intercom": true,
|
||||||
"fetch": true,
|
"fetch": true,
|
||||||
"require": true
|
"require": true,
|
||||||
|
"File": true
|
||||||
},
|
},
|
||||||
"plugins": [
|
"plugins": [
|
||||||
"react"
|
"react"
|
||||||
|
@ -171,7 +171,8 @@ let FileUploader = React.createClass({
|
|||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
areAssetsDownloadable={true}
|
areAssetsDownloadable={true}
|
||||||
areAssetsEditable={this.props.editable}/>
|
areAssetsEditable={this.props.editable}/
|
||||||
|
localHashing={true}>
|
||||||
</Property>
|
</Property>
|
||||||
<hr />
|
<hr />
|
||||||
</Form>
|
</Form>
|
||||||
|
@ -8,7 +8,7 @@ import SparkMD5 from 'spark-md5';
|
|||||||
* @param {string} text regular javascript string
|
* @param {string} text regular javascript string
|
||||||
* @return {string} regular javascript string
|
* @return {string} regular javascript string
|
||||||
*/
|
*/
|
||||||
export function makeTextFile(text) {
|
function makeTextFile(text) {
|
||||||
let data = new Blob([text], {type: 'text/plain'});
|
let data = new Blob([text], {type: 'text/plain'});
|
||||||
return window.URL.createObjectURL(data);
|
return window.URL.createObjectURL(data);
|
||||||
}
|
}
|
||||||
@ -20,15 +20,16 @@ export function makeTextFile(text) {
|
|||||||
* @return {string} regular javascript string
|
* @return {string} regular javascript string
|
||||||
*/
|
*/
|
||||||
export function computeHashOfFile(file) {
|
export function computeHashOfFile(file) {
|
||||||
let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
|
return new Promise((resolve, reject) => {
|
||||||
chunkSize = 2097152, // Read in chunks of 2MB
|
let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
|
||||||
chunks = Math.ceil(file.size / chunkSize),
|
let chunkSize = 2097152; // Read in chunks of 2MB
|
||||||
currentChunk = 0,
|
let chunks = Math.ceil(file.size / chunkSize);
|
||||||
spark = new SparkMD5.ArrayBuffer(),
|
let currentChunk = 0;
|
||||||
fileReader = new FileReader();
|
let spark = new SparkMD5.ArrayBuffer();
|
||||||
|
let fileReader = new FileReader();
|
||||||
|
|
||||||
let startTime = new Date();
|
let startTime = new Date();
|
||||||
fileReader.onload = function (e) {
|
fileReader.onload = function(e) {
|
||||||
//console.log('read chunk nr', currentChunk + 1, 'of', chunks);
|
//console.log('read chunk nr', currentChunk + 1, 'of', chunks);
|
||||||
spark.append(e.target.result); // Append array buffer
|
spark.append(e.target.result); // Append array buffer
|
||||||
currentChunk++;
|
currentChunk++;
|
||||||
@ -37,18 +38,21 @@ export function computeHashOfFile(file) {
|
|||||||
loadNext();
|
loadNext();
|
||||||
} else {
|
} else {
|
||||||
let fileHash = spark.end();
|
let fileHash = spark.end();
|
||||||
|
|
||||||
console.info('computed hash %s (took %d s)',
|
console.info('computed hash %s (took %d s)',
|
||||||
fileHash,
|
fileHash,
|
||||||
Math.round(((new Date() - startTime) / 1000) % 60)); // Compute hash
|
Math.round(((new Date() - startTime) / 1000) % 60)); // Compute hash
|
||||||
let hashFile = this.makeTextFile(fileHash);
|
|
||||||
|
let hashFile = makeTextFile(fileHash);
|
||||||
console.info('hash: ', hashFile);
|
console.info('hash: ', hashFile);
|
||||||
return hashFile;
|
resolve(hashFile);
|
||||||
}
|
}
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
fileReader.onerror = function () {
|
fileReader.onerror = function () {
|
||||||
console.warn('oops, something went wrong.');
|
reject(new Error('We weren\' able to hash your file locally. Try to upload it manually or consider contact us.'));
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadNext() {
|
function loadNext() {
|
||||||
var start = currentChunk * chunkSize,
|
var start = currentChunk * chunkSize,
|
||||||
end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
|
end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
|
||||||
@ -57,5 +61,5 @@ export function computeHashOfFile(file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadNext();
|
loadNext();
|
||||||
|
});
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user