filepond icon indicating copy to clipboard operation
filepond copied to clipboard

Added capabilities to have async header functions

Open jzucchetto opened this issue 2 years ago • 1 comments

A big part of uploading is to validate that the data doesn't get corrupted. One way to do it is to do a checksum and send it to the server as a part of the headers. Most checksum functions are asynchronous and this change would allow these types of functions to be added to the Filepond headers.

Example:

const server = {
    url: '/api/http-upload/chunk',
    patch: {
      headers: async (chunk) => {
        const { data, file, offset } = chunk;

        const md5 = async () => await hashwasm.md5(new Uint8Array(await data.arrayBuffer()));

        return {
          'Content-Type': 'application/offset+octet-stream',
          'Upload-Offset': offset,
          'Upload-Length': file.size,
          'Upload-Name': file.name,
          'x-checksum-md5': await md5()
        };
      }
    }
  };

jzucchetto avatar Apr 11 '22 17:04 jzucchetto

Excellent idea.

Can you move these outside of the Promise, I guess we can set them before applying the headers.

        // set type of response
        if (options.responseType) {
            xhr.responseType = options.responseType;
        }

        // set credentials
        if (options.withCredentials) {
            xhr.withCredentials = true;
        }

rikschennink avatar Apr 20 '22 08:04 rikschennink