1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
var readFirstNLines = (fileelement, count, process_content_fns) => {
var thefile = fileelement.files[0];
var reader = new FileReader();
if(typeof thefile !== "undefined" && thefile !== null) {
reader.addEventListener("load", (event) => {
var content = event
.target
.result
.split("\n")
.slice(0, count)
.map((line) => {return line.trim("\r");});
process_content_fns.forEach((fn) => {fn(content);});
});
reader.readAsText(thefile);
}
};
var read_first_n_lines = readFirstNLines;
var readBinaryFile = (file) => {
return new Promise((resolve, reject) => {
var _reader = new FileReader();
_reader.onload = (event) => {resolve(_reader.result);};
_reader.readAsArrayBuffer(file);
});
};
var Uint8ArrayToHex = (arr) => {
var toHex = (val) => {
_hex = val.toString(16);
if(_hex.length < 2) {
return "0" + val;
}
return _hex;
};
_hexstr = ""
arr.forEach((val) => {_hexstr += toHex(val)});
return _hexstr
};
var computeFileChecksum = (file) => {
return readBinaryFile(file)
.then((content) => {
return window.crypto.subtle.digest(
"SHA-256", new Uint8Array(content));
}).then((digest) => {
return Uint8ArrayToHex(new Uint8Array(digest))
});
};
var defaultResumableHandler = (event) => {
throw new Error("Please provide a valid event handler!");
};
var addHandler = (resumable, handlername, handler) => {
if(resumable.support) {
resumable.on(handlername, (handler || defaultResumableHandler));
}
return resumable;
};
var makeResumableHandler = (handlername) => {
return (resumable, handler) => {
return addHandler(resumable, handlername, handler);
};
};
var fileSuccessHandler = makeResumableHandler("fileSuccess");
var fileProgressHandler = makeResumableHandler("fileProgress");
var fileAddedHandler = makeResumableHandler("fileAdded");
var filesAddedHandler = makeResumableHandler("filesAdded");
var filesRetryHandler = makeResumableHandler("filesRetry");
var filesErrorHandler = makeResumableHandler("filesError");
var uploadStartHandler = makeResumableHandler("uploadStart");
var completeHandler = makeResumableHandler("complete");
var progressHandler = makeResumableHandler("progress");
var errorHandler = makeResumableHandler("error");
var markResumableDragAndDropElement = (resumable, fileinput, droparea, browsebutton) => {
if(resumable.support) {
//Hide file input element and display drag&drop UI
add_class(fileinput, "hidden");
remove_class(droparea, "hidden");
// Define UI elements for browse and drag&drop
resumable.assignDrop(droparea);
resumable.assignBrowse(browsebutton);
}
return resumable;
};
var makeResumableElement = (targeturi, fileinput, droparea, uploadbutton, filetype) => {
var resumable = Resumable({
target: targeturi,
fileType: filetype,
maxFiles: 1,
forceChunkSize: true,
generateUniqueIdentifier: (file, event) => {
return computeFileChecksum(file).then((checksum) => {
var _relativePath = (file.webkitRelativePath
|| file.relativePath
|| file.fileName
|| file.name);
return checksum + "-" + _relativePath.replace(
/[^a-zA-Z0-9_-]/img, "");
});
}
});
return resumable;
};
|