Skip to content

Commit

Permalink
Merge pull request #57 from beer-garden/md5_sum_file_chunks
Browse files Browse the repository at this point in the history
Base64 Upload Supports MD5 Tracking
  • Loading branch information
TheBurchLog authored Sep 18, 2024
2 parents f89eb01 + 0d80c63 commit ecf6715
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
27 changes: 25 additions & 2 deletions src/directives/file-upload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import angular from "angular";
import CryptoJS from 'crypto-js';

class FileUploader {
constructor(url_prefix) {
Expand Down Expand Up @@ -119,18 +120,40 @@ 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=" +
encodeURIComponent(file.name) +
"&file_size=" +
this.file.size +
"&chunk_size=" +
this.chunkSize
this.chunkSize +
"&md5_sum=" +
this.md5_sum
)
.done((data) => {
this.fileId = data["details"]["file_id"];
Expand Down

0 comments on commit ecf6715

Please sign in to comment.