From 0d80c63ffed6c22dce7067672dc4689347e91a4d Mon Sep 17 00:00:00 2001 From: TheBurchLog <5104941+TheBurchLog@users.noreply.github.com> Date: Tue, 17 Sep 2024 12:14:59 -0400 Subject: [PATCH] Base64 Upload Supports MD5 Tracking --- package.json | 5 ++++- src/directives/file-upload.js | 27 +++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 9b82502..a381683 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@beer-garden/addons", - "version": "3.3.0", + "version": "3.4.0", "description": "Awesome Addons for Angular Schema Form.", "main": "dist/addons.js", "scripts": { @@ -15,6 +15,9 @@ "author": "The Beergarden Team", "contributors": [], "license": "MIT", + "dependencies": { + "crypto-js": "4.2.0" + }, "peerDependencies": { "angular": "^1.6.5", "angular-schema-form": "1.0.0-alpha.4", diff --git a/src/directives/file-upload.js b/src/directives/file-upload.js index 874bba7..3c84bfa 100644 --- a/src/directives/file-upload.js +++ b/src/directives/file-upload.js @@ -1,4 +1,5 @@ import angular from "angular"; +import CryptoJS from 'crypto-js'; class FileUploader { constructor(url_prefix) { @@ -119,10 +120,30 @@ class FileUploader { reader.readAsDataURL(chunk); } - uploadFile(file, ngModel, scope) { + calculateMD5(file) { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + + reader.onload = (event) => { + const wordArray = CryptoJS.lib.WordArray.create(event.target.result); + const hash = CryptoJS.MD5(wordArray); + resolve(hash.toString(CryptoJS.enc.Hex)); + }; + + reader.onerror = (error) => { + reject(error); + }; + + reader.readAsArrayBuffer(file); + }); + } + + async uploadFile(file, ngModel, scope) { this.file = file; this.numChunks = Math.ceil(file.size / this.chunkSize); + this.md5_sum = await this.calculateMD5(file); + $.get( this.apiPath + "id/?file_name=" + @@ -130,7 +151,9 @@ class FileUploader { "&file_size=" + this.file.size + "&chunk_size=" + - this.chunkSize + this.chunkSize + + "&md5_sum=" + + this.md5_sum ) .done((data) => { this.fileId = data["details"]["file_id"];