Serving files with Django and lighttpd

I am trying to create an easy way to serve downloadable content using Django. The idea is that registered users can upload (rather large) files via lighttpd.

There are some posts about this here, and I came also through a blog post with a simple solution.

I created a view as in the above link (and added "allow-x-send-file" => "enable" to the lighttpd configuration) and it "works." When I check the headers with Firebug, I get the correct content type, file length and 200 OK, but the file does not load.

Then I found a solution here on SO where additional headers are sent . Now the file is served, but the downloaded file is empty. The headings are still correct.

Here is my source (with auth_decorators removed and without processing a nonexistent file):

import os
import mimetypes
import django.http

from django.conf import settings

def get_absolute_filename(filename='', safe=True):
    if not filename:
        return os.path.join(settings.FILE_DOWNLOAD_PATH, 'index')
    if safe and '..' in filename.split(os.path.sep):
        return get_absolute_filename(filename='')
    return os.path.join(settings.FILE_DOWNLOAD_PATH, filename)

def retrieve_file(request, filename=''):
    abs_filename = get_absolute_filename(filename)
    response = django.http.HttpResponse(mimetype='application/force-download')
    response['X-Sendfile'] = abs_filename
    response['Content-Disposition'] = 'attachment; filename=%s' % abs_filename
    response['Content-Type'] = mimetypes.guess_type(abs_filename)
    response['Content-Length'] = os.path.getsize(abs_filename)
    return response
+3
source share
2 answers

Look at your source - you are not sending a file, but only the headers.

+1
source

Use the header instead of the 1.5 version of lighttpd X-LIGHTTPD-send-file.

0
source

Source: https://habr.com/ru/post/1731609/


All Articles