Added upload resume/skip to device sharing.
Ylian Saint-Hilaire committed
Oct 16, 2021 at 16:24 UTC
47fbeef9243db22c86e7951384be7f7fa15a6ae3
1 file changed
+33
-1
views/sharing.handlebars
+33
-1
@@ -2086,6 +2086,12 @@
2086
p13uploadNextFile();
2087
}
2088
2089
+ // Perform SHA-384 hashing
2090
+ const byteToHex = [];
2091
+ for (var n = 0; n <= 0xff; ++n) { var hexOctet = n.toString(16).padStart(2, '0'); byteToHex.push(hexOctet); }
2092
+ function arrayBufferToHex(arrayBuffer) { return Array.prototype.map.call(new Uint8Array(arrayBuffer), n => byteToHex[n]).join(''); }
2093
+ function performHash(data, f) { window.crypto.subtle.digest('SHA-384', data).then(function (v) { f(arrayBufferToHex(v)); }, function () { f(null); }); }
2094
+
2095
// Push the next file
2096
function p13uploadNextFile() {
2097
uploadFile.xfilePtr++;
@@ -2100,7 +2106,17 @@
2106
uploadFile.xreader = new FileReader();
2107
uploadFile.xreader.onload = function () {
2108
uploadFile.xdata = uploadFile.xreader.result;
2103
- files.sendText(JSON.stringify({ action: 'upload', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, size: uploadFile.xdata.byteLength }));
2109
+
2110
+ // If the remote file already exists and is smaller then our file, see if we can resume the trasfer
2111
+ var f = null;
2112
+ for (var i in p13filetree.dir) { if (p13filetree.dir[i].n == file.name) { f = p13filetree.dir[i]; } }
2113
+ if ((f != null) && (f.s <= uploadFile.xreader.result.byteLength)) {
2114
+ performHash(uploadFile.xreader.result.slice(0, f.s), function (hash) {
2115
+ files.sendText(JSON.stringify({ action: 'uploadhash', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, tag: { h: hash.toUpperCase(), s: f.s, skip: f.s == uploadFile.xreader.result.byteLength } }));
2116
+ });
2117
+ } else {
2118
+ files.sendText(JSON.stringify({ action: 'upload', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, size: uploadFile.xdata.byteLength }));
2119
+ }
2120
};
2121
uploadFile.xreader.readAsArrayBuffer(file);
2122
} else {
@@ -2134,6 +2150,22 @@
2150
case 'uploadack': { p13uploadNextPart(false); break; }
2151
case 'uploaddone': { if (uploadFile.xfiles.length > uploadFile.xfilePtr + 1) { p13uploadNextFile(); } else { p13uploadFileTransferDone(); } break; }
2152
case 'uploaderror': { p13uploadFileCancel(); break; }
2153
+ case 'uploadhash': {
2154
+ var file = uploadFile.xfiles[uploadFile.xfilePtr];
2155
+ if (file) {
2156
+ if (cmd.tag.h === cmd.hash) {
2157
+ if (cmd.tag.skip) {
2158
+ p13uploadNextFile();
2159
+ } else {
2160
+ uploadFile.xptr = cmd.tag.s;
2161
+ files.sendText(JSON.stringify({ action: 'upload', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, size: uploadFile.xdata.byteLength, append: true }));
2162
+ }
2163
+ } else {
2164
+ files.sendText(JSON.stringify({ action: 'upload', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, size: uploadFile.xdata.byteLength, append: false }));
2165
+ }
2166
+ }
2167
+ break;
2168
+ }
2169
}
2170
}
2171