-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
executable file
·162 lines (148 loc) · 3.99 KB
/
upload.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var dropArea = document.getElementById("drop-area");
var filebtn = document.getElementById("file");
var uploadbtn = document.getElementById("submit");
var loadingICN = document.getElementById("loading");
var lastOffset = 0;
var fileMD5 = null;
var fileSHA1 = null;
var fileCRC = null;
var fileSize = null;
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
function highlight(e) {
preventDefaults(e);
dropArea.classList.add("highlight");
}
function unhighlight(e) {
preventDefaults(e);
dropArea.classList.remove("highlight");
}
dropArea.addEventListener("dragenter", highlight, false);
dropArea.addEventListener("dragover", highlight, false);
dropArea.addEventListener("dragleave", unhighlight, false);
dropArea.addEventListener("drop", unhighlight, false);
dropArea.addEventListener("drop", dropped, false);
filebtn.onchange = function(e) {
hash(e.target.files);
};
uploadbtn.onclick = function(e) {
var urlParams = new URLSearchParams(window.location.search);
if (fileMD5 != null && fileSHA1 != null && fileCRC != null && fileSize != null) {
window.location.replace(
window.location.href.split("?")[0] +
"?id=" +
urlParams.get("id") +
"&md5=" +
fileMD5 +
"&sha1=" +
fileSHA1 +
"&crc=" +
fileCRC +
"&size=" +
fileSize
);
}
};
function hash(files) {
var file = files[0];
fileSize = file.size;
loadingICN.style.opacity = 1;
hashChainMD5(file);
}
function hashChainMD5(file) {
var MD5 = CryptoJS.algo.MD5.create();
var counter = 0;
loading(
file,
function(data) {
var wordBuffer = CryptoJS.lib.WordArray.create(data);
MD5.update(wordBuffer);
counter += data.byteLength;
console.log(((counter / file.size) * 100).toFixed(0) + "%");
},
function(data) {
console.log("100%");
var encrypted = MD5.finalize().toString();
lastOffset = 0;
fileMD5 = encrypted;
console.log("encrypted: " + encrypted);
hashChainSHA1(file);
}
);
}
function hashChainSHA1(file) {
var SHA1 = CryptoJS.algo.SHA1.create();
var counter = 0;
loading(
file,
function(data) {
var wordBuffer = CryptoJS.lib.WordArray.create(data);
SHA1.update(wordBuffer);
counter += data.byteLength;
console.log(((counter / file.size) * 100).toFixed(0) + "%");
},
function(data) {
console.log("100%");
var encrypted = SHA1.finalize().toString();
lastOffset = 0;
fileSHA1 = encrypted;
console.log("encrypted: " + encrypted);
hashChainCRC(file);
}
);
}
function hashChainCRC(file) {
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function(evt) {
fileCRC = crc32(evt.target.result);
uploadbtn.removeAttribute("disabled");
loadingICN.style.opacity = 0;
};
}
function callbackRead(reader, file, evt, callbackProgress, callbackFinal) {
if (lastOffset === reader.offset) {
// in order chunk
lastOffset = reader.offset + reader.size;
callbackProgress(evt.target.result);
if (reader.offset + reader.size >= file.size) {
callbackFinal();
}
} else {
// not in order chunk
timeout = setTimeout(function() {
callbackRead(reader, file, evt, callbackProgress, callbackFinal);
}, 10);
}
}
function loading(file, callbackProgress, callbackFinal) {
var chunkSize = 1024 * 1024; // bytes
var offset = 0;
var size = chunkSize;
var partial;
var index = 0;
if (file.size === 0) {
callbackFinal();
}
while (offset < file.size) {
partial = file.slice(offset, offset + size);
var reader = new FileReader();
reader.size = chunkSize;
reader.offset = offset;
reader.index = index;
reader.onload = function(evt) {
callbackRead(this, file, evt, callbackProgress, callbackFinal);
};
reader.readAsArrayBuffer(partial);
offset += chunkSize;
index += 1;
}
}
function dropped(e) {
preventDefaults(e);
var dt = e.dataTransfer;
var files = dt.files;
hash(files);
}