ImperialCollegeLondon / django-drf-filepond

A Django app providing a server implemention for the Filepond file upload library
BSD 3-Clause "New" or "Revised" License
105 stars 40 forks source link

Unsupported Media Type with Chunked Uploads #123

Open bptarpley opened 1 week ago

bptarpley commented 1 week ago

Greetings! I'm excited about using this package to handle my integration with Filepond. Unfortunately, when setting up my client according to your documentation, my Django app returns this response:

method: PATCH code: 415 message: Unsupported media type "application offset/octet stream"

You documentation mentions no further configuration necessary for supporting chunked uploads, but I can't seem to get it to work. I'm using:

django 4.2.7 djangorestframework 3.14.0 django-drf-filepond 0.5.0

In settings.py, I tried specifying "rest_framework.parsers.FileUploadParser" under 'DEFAULT_PARSER_CLASSES' for the REST_FRAMEWORK setting but no luck.

What am I missing? Many thanks in advance!

bptarpley commented 1 week ago

Okay, so after a lot of sleuthing, it looks like FilePond is setting the CONTENT_TYPE header to "application%2Foffset%2Boctet-stream" which looks like a URL encoded version of the expected "application/offset+octet-stream". Here's my Filepond (v4.31.3) config on the frontend:

  FilePond.create(target[0], {
          chunkUploads: true,
          chunkSize: 500000,
          server: {
              url: '/fp',
              process: '/process/',
              patch: '/patch/',
              revert: '/revert/',
              fetch: '/fetch/?target=',
              headers: {'X-CSRFToken': crsf_token}
          }
      })

A hacky bit of Django middleware placed near the top of the stack fixes the problem:

  class ChunkedTransferMiddleware:

      def __init__(self, get_response):
          self.get_response = get_response

      def __call__(self, request):
          if 'CONTENT_TYPE' in request.headers and request.headers['CONTENT_TYPE'] == 'application%2Foffset%2Boctet-stream':
              request.META['CONTENT_TYPE'] = 'application/offset+octet-stream'

          response = self.get_response(request)
          return response

This may be a bug on the Filepond side of the fence, but figured I'd report here in case anyone else has this issue.

jcohen02 commented 1 week ago

Hi, thanks for highlighting this issue. I'll try to find some time to investigate this in the next couple of days.

It sounds like something may possibly have changed on the Filepond client side - glad you managed to work around this with some middleware but it should certainly work without that and certainly did previously so it looks like maybe a fix is required here.