Adding additional parameters for XHRUpload asynchronously
See original GitHub issueOn 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:
- Created 5 years ago
- Comments:5 (3 by maintainers)
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 thegetUploadParameters
can be supported by any service.That would easily allow adding the
Content-MD5
request header, becausegetUploadParameters
would support Promises, so it’s not limited to only HTTP requests but any operation that’s async:What do you think?
We do something like this when our access token expires.