开发者

django authentication for static files

I have a set of static files (that get uploaded through the application) like images, video, etc. that need to be served to authenticated users (i.e. their cookie is registered as authenticated in the session).

These files are separate and in no way related to other media static files like css, javaacript, etc.

Given that my static files that need to be authenticated will be fairly large, I was wondering what would be the most efficient way of serving them (btw, i'm using wsgi).

Currently I have this:

def return_large_file(request, p_filename):
    """                                                                         
    Send a file through Django without loading the whole file into              
    memory at once. The FileWrapper will turn the file object into an           
    iterator for chunks of 8KB.                                                 
    """
    if not os.path.exists(p_filename):
        raise Exception('File %s does not exist!')

    try:
        _co开发者_JS百科ntent_type = mimetypes.guess_type(p_filename)
    except:
        _content_type = 'application/octet-stream'

    wrapper = FileWrapper(file(p_filename))
    response = HttpResponse(wrapper, content_type=_content_type)
    response['Content-Length'] = os.path.getsize(p_filename)
    return response


I'm currently using a function similar to what you use above.

I was thinking once performance/efficiency became an issue, I would use Apache mod-auth-external to do my custom authorization for a given file.

Please note, I offer this answer not based on my experience, but where my own research led me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜