Monday 16 July 2018

Laravel, FineUploader and FileReader API: compare hash of file before uploading

I'd like to compare the hashed version of the file against the data stored in the database, so I can abort duplicate uploads.

Being quite "new" to the subjects of hashing and the FileReader API, I'm finding the process a bit confusing. I am using the frontend library SparkMD5.js

Test n1 compare two strings:

// frontend
$.post(url, {sparkMD5('random text')})

// backend
$input->hash == md5('random text')  // outputs true

Test n2 - inside fineuploader onSubmit event handler

// frontend
var file = this.getFile(id);
var reader = new FileReader();
//
reader.onload = function(e) {
  var data = reader.result;
  var hexHash = SparkMD5.hash(data);
  console.log(hexHash);
}

var res = reader.readAsDataURL(file); // or reader.readAsBinaryString(file)

......

//backend
$file = Input::file('qqfile');
$hash = md5(file ) // or md5_file(file )
$hash == $input->hexHash // outputs false

My questions:

1) Why is the result of md5($file) == md5_file($file) \\ false ?

2) What's the correct way of reading a file readAsDataURL or readAsBinaryString ?

3) Is there a way to read and hash the result of fineuploader's getFile(id) which returns File or Blob, without using filereader api?

Answers to the above questions may be considered broad and out of scope of my immediate problem, but i'd like to understand the subject as best as possible.

Thanks.



from Laravel, FineUploader and FileReader API: compare hash of file before uploading

No comments:

Post a Comment