2015-07-24 13:44:28 +02:00
|
|
|
import Q from 'q';
|
2015-07-23 13:03:30 +02:00
|
|
|
import SparkMD5 from 'spark-md5';
|
2015-11-03 10:57:41 +01:00
|
|
|
import Moment from 'moment';
|
2015-07-23 13:03:30 +02:00
|
|
|
|
2016-06-13 14:35:02 +02:00
|
|
|
import { getLangText } from './lang';
|
2015-07-23 17:05:52 +02:00
|
|
|
|
2016-06-13 14:55:27 +02:00
|
|
|
// Re-export related utilities from js-utility-belt for easier access
|
|
|
|
export { extractFileExtensionFromString, extractFileExtensionFromUrl } from 'js-utility-belt/es6/file';
|
2015-07-23 13:03:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a file Object, computes the MD5 hash and returns the URL of the textfile with the hash
|
|
|
|
*
|
|
|
|
* @param {File} file javascript File object
|
|
|
|
* @return {string} regular javascript string
|
|
|
|
*/
|
|
|
|
export function computeHashOfFile(file) {
|
2015-07-24 14:39:04 +02:00
|
|
|
return Q.Promise((resolve, reject, notify) => {
|
2016-06-13 14:55:43 +02:00
|
|
|
const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
|
|
|
|
const chunkSize = 2097152; // Read in chunks of 2MB
|
|
|
|
const chunks = Math.ceil(file.size / chunkSize);
|
|
|
|
const spark = new SparkMD5.ArrayBuffer();
|
|
|
|
const fileReader = new FileReader();
|
|
|
|
const startTime = new Moment();
|
2015-07-23 15:38:52 +02:00
|
|
|
let currentChunk = 0;
|
2015-07-23 17:17:48 +02:00
|
|
|
|
|
|
|
// comment: We should convert this to es6 at some point, however if so please consider that
|
|
|
|
// an arrow function will get rid of the function's scope...
|
2016-06-13 14:55:43 +02:00
|
|
|
fileReader.onload = (e) => {
|
|
|
|
// console.log('read chunk nr', currentChunk + 1, 'of', chunks);
|
2015-07-23 15:38:52 +02:00
|
|
|
spark.append(e.target.result); // Append array buffer
|
|
|
|
currentChunk++;
|
|
|
|
|
|
|
|
if (currentChunk < chunks) {
|
|
|
|
loadNext();
|
|
|
|
} else {
|
2016-06-13 14:55:43 +02:00
|
|
|
const fileHash = spark.end();
|
2015-07-23 13:03:30 +02:00
|
|
|
|
2015-07-23 15:38:52 +02:00
|
|
|
console.info('computed hash %s (took %d s)',
|
|
|
|
fileHash,
|
2015-11-03 10:57:41 +01:00
|
|
|
Math.round(((new Moment() - startTime) / 1000) % 60)); // Compute hash
|
2015-07-23 13:03:30 +02:00
|
|
|
|
2016-06-13 14:55:43 +02:00
|
|
|
resolve(makeTextFile(fileHash, file));
|
2015-07-23 15:38:52 +02:00
|
|
|
}
|
2016-06-13 14:55:43 +02:00
|
|
|
};
|
2015-07-23 13:03:30 +02:00
|
|
|
|
2016-06-13 14:55:43 +02:00
|
|
|
fileReader.onerror = () => {
|
|
|
|
reject(new Error(getLangText("We weren't able to hash your file locally. Try to " +
|
|
|
|
'upload it manually or consider contact us.')));
|
2015-07-23 15:38:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function loadNext() {
|
2016-06-13 14:55:43 +02:00
|
|
|
const start = currentChunk * chunkSize;
|
|
|
|
const end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
|
2015-07-23 15:38:52 +02:00
|
|
|
|
2015-07-24 14:39:04 +02:00
|
|
|
// send progress
|
2015-07-24 16:29:57 +02:00
|
|
|
// Due to the fact that progressHandler and notify are going to be removed in v2
|
|
|
|
// of Q, the functionality of throwing errors in the progressHandler will not be implemented
|
|
|
|
// anymore. To still be able to throw an error however, we can just expose the promise's reject
|
|
|
|
// method to the .progress function to stop the execution immediately.
|
2015-07-24 16:17:27 +02:00
|
|
|
notify({
|
|
|
|
progress: start / file.size,
|
2015-07-24 16:29:57 +02:00
|
|
|
reject
|
2015-07-24 16:17:27 +02:00
|
|
|
});
|
2016-01-26 11:33:38 +01:00
|
|
|
|
2015-07-23 15:38:52 +02:00
|
|
|
fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
|
|
|
|
}
|
2015-07-23 13:03:30 +02:00
|
|
|
|
2015-07-23 15:38:52 +02:00
|
|
|
loadNext();
|
|
|
|
});
|
2015-11-19 10:43:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-13 14:55:27 +02:00
|
|
|
* Takes a string, creates a text file and returns the URL
|
2015-11-19 10:43:40 +01:00
|
|
|
*
|
2016-06-13 14:55:27 +02:00
|
|
|
* @param {string} text regular javascript string
|
|
|
|
* @return {string} regular javascript string
|
2015-11-19 10:43:40 +01:00
|
|
|
*/
|
2016-06-13 14:55:27 +02:00
|
|
|
function makeTextFile(text, file) {
|
|
|
|
const textFileBlob = new Blob([text], { type: 'text/plain' });
|
|
|
|
const textFile = new File([textFileBlob], `hash-of-${file.name}.txt`, {
|
|
|
|
lastModifiedDate: file.lastModifiedDate,
|
|
|
|
lastModified: file.lastModified,
|
|
|
|
type: 'text/plain'
|
|
|
|
});
|
2016-03-10 15:09:22 +01:00
|
|
|
|
2016-06-13 14:55:27 +02:00
|
|
|
return textFile;
|
2016-03-10 15:09:22 +01:00
|
|
|
}
|