Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

This is a modified FileResponse that returns `Content-Range` headers with the HTTP response, so browsers (read Safari 9+) that request the file, can stream the response properly.

License

Notifications You must be signed in to change notification settings

i3thuan5/django-ranged-response

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Ranged Response

Build Status Coverage Status

Why content range?

If you're in the situation that you have an authenticated Django view that returns files for download, you may have noticed that Safari 9.x doesn't play audio files properly when returning audio-files. The reason is that Safari sends a HTTP_RANGE request header and expects a proper Content-Range response header in return. There is a Django ticket for this, however no indication that this feature will be implemented soon.

Three approches about content range

Choose proper one by your situation.

1. Support all dynamic views and static files

The WhiteNoise provides a middleware to support global settings.

2. Support static files only

Nginx can support static and media files.

See more details on Setting up Django and your web server with uWSGI and nginx. If some views need redirect to static or media files, see the stackoverflow answer.

3. Support specific dynamic views only

This project supports RangedFileResponse with content range. Use RangedFileResponse for every view you wanted. The views are mostly designed for media API, like speech synthesis, video generators.

Installation

pip install django-ranged-response

Usage

You can use it for custom views like:

import io
from ranged_response import RangedFileResponse

def some_proxy_view(request):
    filename = 'myfile.wav'
    response = RangedFileResponse(request, open(filename, 'rb'), content_type='audio/wav')
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename
    return response

def some_dynamic_view(request):
    binarydata = b'RIFF...'
    response = RangedFileResponse(request, io.BytesIO(binarydata), content_type='audio/wav')
    response['Content-Disposition'] = 'attachment; filename="myfile.wav"'
    return response

About this project

The original suggested fix applies the code to Django's static view. This is a packaged version of that fix, but uses a modified FileResponse, instead of applying it to Django's static view.

About

This is a modified FileResponse that returns `Content-Range` headers with the HTTP response, so browsers (read Safari 9+) that request the file, can stream the response properly.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%