|
@@ -1,44 +1,51 @@
|
1
|
|
-import { SparkMD5 } from 'spark-md5'
|
|
1
|
+import SparkMD5 from 'spark-md5'
|
2
|
2
|
|
3
|
|
-export function get_filemd5sum(ofile) {
|
4
|
|
- var file = ofile;
|
5
|
|
- var tmp_md5;
|
6
|
|
- var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
|
7
|
|
- // file = this.files[0],
|
8
|
|
- chunkSize = 8097152, // Read in chunks of 2MB
|
9
|
|
- chunks = Math.ceil(file.size / chunkSize),
|
10
|
|
- currentChunk = 0,
|
11
|
|
- spark = new SparkMD5.ArrayBuffer(),
|
12
|
|
- fileReader = new FileReader();
|
|
3
|
+export function getFilemd5sum(ofile) {
|
|
4
|
+ return new Promise((resolve, reject) => {
|
|
5
|
+ let file = ofile;
|
|
6
|
+ let tmp_md5;
|
|
7
|
+ let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
|
|
8
|
+ // file = this.files[0],
|
|
9
|
+ chunkSize = 8097152, // Read in chunks of 2MB
|
|
10
|
+ chunks = Math.ceil(file.size / chunkSize),
|
|
11
|
+ currentChunk = 0,
|
|
12
|
+ spark = new SparkMD5.ArrayBuffer(),
|
|
13
|
+ fileReader = new FileReader();
|
13
|
14
|
|
14
|
|
- fileReader.onload = function(e) {
|
15
|
|
- spark.append(e.target.result); // Append array buffer
|
16
|
|
- currentChunk++;
|
|
15
|
+ fileReader.onload = function (e) {
|
|
16
|
+ spark.append(e.target.result); // Append array buffer
|
|
17
|
+ currentChunk++;
|
17
|
18
|
|
18
|
|
- if (currentChunk < chunks) {
|
19
|
|
- loadNext();
|
20
|
|
- } else {
|
21
|
|
- tmp_md5 = spark.end();
|
22
|
|
- return tmp_md5
|
23
|
|
- }
|
24
|
|
- };
|
|
19
|
+ if (currentChunk < chunks) {
|
|
20
|
+ loadNext();
|
|
21
|
+ } else {
|
|
22
|
+ tmp_md5 = spark.end();
|
|
23
|
+ resolve(tmp_md5)
|
|
24
|
+ }
|
|
25
|
+ };
|
25
|
26
|
|
26
|
|
- fileReader.onerror = function() {
|
27
|
|
- console.warn('oops, something went wrong.');
|
28
|
|
- };
|
|
27
|
+ fileReader.onerror = function () {
|
|
28
|
+ reject('error');
|
|
29
|
+ };
|
29
|
30
|
|
30
|
|
- function loadNext() {
|
31
|
|
- var start = currentChunk * chunkSize,
|
32
|
|
- end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
|
33
|
|
- fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
|
34
|
|
- }
|
35
|
|
- loadNext();
|
|
31
|
+ loadNext = () => {
|
|
32
|
+ var start = currentChunk * chunkSize,
|
|
33
|
+ end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
|
|
34
|
+ fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
|
|
35
|
+ }
|
|
36
|
+ loadNext();
|
|
37
|
+ })
|
36
|
38
|
}
|
37
|
39
|
|
38
|
40
|
export function getBase64(file) {
|
39
|
|
- var reader = new FileReader();
|
40
|
|
- reader.readAsDataURL(file);
|
41
|
|
- reader.onload = function (e) {
|
42
|
|
- return this.result.substring(this.result.indexOf(',')+1);
|
43
|
|
- }
|
|
41
|
+ return new Promise((resolve, reject) => {
|
|
42
|
+ var reader = new FileReader();
|
|
43
|
+ reader.readAsDataURL(file);
|
|
44
|
+ reader.onload = function (e) {
|
|
45
|
+ resolve(e.target.result);
|
|
46
|
+ }
|
|
47
|
+ reader.onerror = function() {
|
|
48
|
+ reject('error')
|
|
49
|
+ }
|
|
50
|
+ });
|
44
|
51
|
}
|