question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding additional parameters for XHRUpload asynchronously

See original GitHub issue

On each XHRUpload I would like to include a Content-MD5 request header which contains the checksum of the file. I’m calculating the checksum using the spark-md5 and chunked-file-reader libraries:

function fileMD5 (file) {
  return new Promise((resolve, reject) => {
    var spark  = new SparkMD5.ArrayBuffer(),
        reader = new ChunkedFileReader();

    reader.subscribe('chunk', (e) => {
      spark.append(e.chunk);
    });

    reader.subscribe('end', (e) => {
      var rawHash    = spark.end(true);
      var base64Hash = btoa(rawHash);

      resolve(base64Hash);
    });

    reader.readChunks(file);
  });
}

Calculating the MD5 hash is asynchronous, as it uses FileReader which is asynchronous, so I wrapped it into a Promise. However, now I’m a bit stuck, because I don’t know how to tell Uppy to wait until the MD5 hash has been generated before proceeding with the upload.

Btw, for AwsS3 uploads I was able to make it work, because there I needed to calculate the MD5 hash before fetching upload parameters (which returns Content-MD5 header in headers in the response), and since getUploadParameters expects a Promise I was able to chain generating an MD5 hash before fetching upload parameters:

uppy.use(Uppy.AwsS3, {
  getUploadParameters (file) {
    return fileMD5(file.data)
      .then((hash) => { return fetch('/presign?filename='+ file.name + '&checksum=' + hash) })
      .then((response) => { return response.json() })
  }
})

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jankocommented, Apr 29, 2018

I’ve an idea: what if we moved getUploadParameters functionality into the XHRUpload plugin? It would then be optional, and the result could be merged with already specified fields and headers.

The XHRUpload plugin could then be used with Google Cloud Storage and other services (the documentation from https://github.com/transloadit/uppy/pull/777 could then be moved to XHRUpload), and the AwsS3 plugin would just add some S3-specific logic on top of XHRUpload. It’s not intuitive that the AwsS3 plugin can be used services other than AWS S3, but XHRUpload already has the generic name.

I think that would make sense, because :method, :url, :fields, and :headers define the whole request, so the getUploadParameters can be supported by any service.

That would easily allow adding the Content-MD5 request header, because getUploadParameters would support Promises, so it’s not limited to only HTTP requests but any operation that’s async:

uppy.use(Uppy.XHRUpload, {
  getUploadParameters (file) {
    return fileMD5(file.data)
      .then((md5) => { return { headers: { 'Content-MD5': md5 } } })
  }
})

What do you think?

1reaction
AshUKcommented, Jun 15, 2018

We do something like this when our access token expires.

this.uppy.setState({
  xhrUpload: {
    headers: {
      'authorization': `Bearer ${nextProps.user.access_token}`
    }
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

XMLHttpRequest to upload a file with parameters
If you want to pass additional parameters to the POST you will have to add them to the FormData. var fd = new...
Read more >
XMLHttpRequest.send() - Web APIs | MDN
The XMLHttpRequest method send() sends the request to the server. If the request is asynchronous (which is the default), this method returns ...
Read more >
XMLHttpRequest - The Modern JavaScript Tutorial
We can upload/download files, track progress and much more. ... The optional body parameter contains the request body.
Read more >
4. AJAX and Server Communication - Dojo - O'Reilly
Performs an XHR PUT request and allows you to provide the raw data that should be included as the body of the PUT....
Read more >
How to add additional POST parameters to ajax file upload
From the jQuery documentation: you specify the asynchronous option to be false to get a synchronous Ajax request. Then your callback can set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found