Skip to content

Commit

Permalink
Merge pull request #23 from penwern/fix-multipart-checksums
Browse files Browse the repository at this point in the history
calculate on byte digests
  • Loading branch information
Sunday-Crunk authored Nov 19, 2024
2 parents f894088 + 0278d59 commit ff6b993
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/js/core/ChecksumValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ window.addEventListener("load", () => {
const originalUploadPresigned =
UploaderModel.UploadItem.prototype.uploadPresigned;

// Helper function to convert hex string to bytes
function hexToBytes(hex) {
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
}
return bytes;
}

UploaderModel.UploadItem.prototype.uploadPresigned = function () {
const originalPromise = originalUploadPresigned.apply(this, arguments);
const multipartThreshold = PydioApi.getMultipartPartSize();
Expand All @@ -31,22 +40,26 @@ window.addEventListener("load", () => {
const partSize = multipartThreshold;
const parts = Math.ceil(this._file.size / partSize);
const partChecksums = [];
const partDigests = new Uint8Array(16 * parts); // MD5 digest is 16 bytes

for (let i = 0; i < parts; i++) {
const start = i * partSize;
const end = Math.min(start + partSize, this._file.size);
const blob = this._file.slice(start, end);
const checksumData = await workerManager.generateChecksum(blob);
partChecksums.push(checksumData.hash);

// Convert hex checksum to bytes and store in concatenated array
const digestBytes = hexToBytes(checksumData.hash);
partDigests.set(digestBytes, i * 16);
}

// Concatenate all part checksums and generate final checksum
const concatenatedChecksums = partChecksums.join("");
const concatenatedBlob = new Blob([concatenatedChecksums]);
// Generate final checksum from concatenated digest bytes
const digestBlob = new Blob([partDigests]);
const finalChecksumData = await workerManager.generateChecksum(
concatenatedBlob
digestBlob
);
finalChecksum = finalChecksumData.hash;
finalChecksum = `${finalChecksumData.hash}-${parts}`;

console.log("Generated multipart checksum:", finalChecksum);
} else {
Expand Down

0 comments on commit ff6b993

Please sign in to comment.