Skip to content

Commit 22c9902

Browse files
authored
Merge pull request #86 from penwern/dev/checksum-generation-fix
passing multipart configuration down from worker manager to worker
2 parents e0d97e0 + feedb70 commit 22c9902

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/js/core/WorkerManager.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,17 @@ class CurateWorkerManager {
6868
if (this.taskQueue.length > 0) {
6969
const task = this.taskQueue.shift();
7070
this.currentTasks.set(workerId, task);
71-
worker.postMessage({ file: task.file, msg: "begin hash" });
71+
72+
// Get PydioApi values and pass them to worker
73+
const multipartThreshold = PydioApi.getMultipartThreshold();
74+
const multipartPartSize = PydioApi.getMultipartPartSize();
75+
76+
worker.postMessage({
77+
file: task.file,
78+
msg: "begin hash",
79+
multipartThreshold,
80+
multipartPartSize
81+
});
7282
} else if (this.currentTasks.size === 0) {
7383
// No more tasks in queue and no running tasks - cleanup workers
7484
this.cleanupWorkers();

src/js/workers/hashWorker.worker.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import SparkMD5 from "spark-md5";
1+
importScripts("https://cdnjs.cloudflare.com/ajax/libs/spark-md5/3.0.2/spark-md5.min.js")
22

33
// Function to calculate the checksum for multipart files
44
const calculateMultipartChecksum = (file, partSize) =>
@@ -53,19 +53,65 @@ const calculateMultipartChecksum = (file, partSize) =>
5353
loadNext();
5454
});
5555

56+
57+
const incrementalMD5 = file => new Promise((resolve, reject) => {
58+
var loaded = 0;
59+
var startTime = performance.now();
60+
var tSize = file.size;
61+
const fileReader = new FileReader();
62+
const spark = new SparkMD5.ArrayBuffer();
63+
const chunkSize = 2097152; // Read in chunks of 2MB
64+
const chunks = Math.ceil(file.size / chunkSize);
65+
let currentChunk = 0;
66+
67+
fileReader.onload = event => {
68+
spark.append(event.target.result); // Append array buffer
69+
++currentChunk;
70+
if (currentChunk < chunks) {
71+
loadNext();
72+
} else {
73+
resolve(spark.end()); // Compute hash
74+
}
75+
};
76+
77+
fileReader.addEventListener("progress", event => {
78+
loaded += event.loaded;
79+
let pE = Math.round((loaded / tSize) * 100);
80+
let rS = pE + "%";
81+
// console.log(rS)
82+
});
83+
84+
fileReader.addEventListener("loadend", event => {
85+
if (event.total > 0) {
86+
var endTime = performance.now();
87+
// console.log(`Took ${endTime - startTime} milliseconds`)
88+
}
89+
});
90+
91+
fileReader.onerror = () => reject(fileReader.error);
92+
93+
const loadNext = () => {
94+
const start = currentChunk * chunkSize;
95+
const end = start + chunkSize >= file.size ? file.size : start + chunkSize;
96+
fileReader.readAsArrayBuffer(File.prototype.slice.call(file, start, end));
97+
};
98+
99+
loadNext();
100+
});
101+
56102
// Main worker handler
57103
self.onmessage = async function (event) {
58104
if (event.data.file && event.data.msg == "begin hash") {
59-
console.log("ello chum!");
60105
const file = event.data.file;
61-
const multipartThreshold = PydioApi.getMultipartPartSize(); // Get the current multipart chunk size
106+
const multipartThreshold = event.data.multipartThreshold;
107+
const multipartPartSize = event.data.multipartPartSize;
62108

63109
if (file.size > multipartThreshold) {
64110
// Only run multipart checksum logic for files above the threshold
65111
try {
66112
const finalChecksum = await calculateMultipartChecksum(
67113
file,
68-
multipartThreshold
114+
multipartPartSize
69115
);
70116
postMessage({ status: "complete", hash: finalChecksum });
71117
} catch (error) {

0 commit comments

Comments
 (0)