2011-07-13 139 views
3

我有一组静态文件(通过应用程序上传)像图像,视频等需要提供给认证用户(即他们的cookie在会话中被注册为认证) 。django静态文件认证

这些文件是分开的,不以任何方式与其他媒体如CSS,javaacript静态文件等

考虑到我的需要进行身份验证的静态文件将是相当大的,我不知道会是什么服务他们的最有效的方式(顺便说一句,我使用的是wsgi)。

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: 
     _content_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 

回答

1

我目前使用类似于你在上面使用的功能:

目前我有这个。

我一直在想性能/效率成了一个问题,我会用Apache mod-auth-external来为给定文件做我的自定义授权。

请注意,我提供这个答案不是基于我的经验,而是我自己的研究领导我。