2009-11-26 2 views
1

Apache와 Pinax는 인증 된 사용자에게만 첨부 파일을 제공하기를 원합니다.Pinax 및 Apache : 인증 된 사용자에게만 첨부 파일을 전달하십시오.

post을 찾았지만 제대로 작동하지 않습니다.

내 아파치의 conf 파일 :

WSGIPythonPath /usr/local/bin/python 

<VirtualHost *:80> 
    ServerName  www.domain.com 
    ServerAlias  domain.com 


    WSGIDaemonProcess k-production python-path=/path/to/app/pinax-env/lib/python2.6/site-packages 
    WSGIProcessGroup k-production 

    Alias /site_media /path/to/app/cp/site_media  
    <Directory /path/to/app/cp/site_media> 
    Order deny,allow 
    Allow from all 
    </Directory> 

    WSGIScriptAlias /site_media/media/attachments /path/to/app/cp/deploy/pinax.wsgi 
    <Directory /path/to/app/cp/site_media/media/attachments> 
    Deny from all 
    </Directory> 

    XSendFile On 
    XSendFileAllowAbove On 

    WSGIScriptAlias//path/to/app/cp/deploy/pinax.wsgi 
    <Directory /path/to/app/cp/deploy> 
     Order deny,allow 
     Allow from all 
    </Directory> 

</VirtualHost> 

내 (여전히 거친) 뷰는, 그 호출을하셔야합니다 :

@login_required 
def sendfile(request, slug): 

    app, content_object_id, img = slug.split('/') 
    project_file = get_object_or_404(Attachment, attachment_file = 'attachments/'+slug) 
    response = HttpResponse() 
    response['X-Sendfile'] = os.path.join(settings.MEDIA_ROOT, 'attachments/'+slug) 
    content_type = 'application/octet-stream' 
    response['Content-Type'] = content_type 
    response['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(os.path.join(settings.MEDIA_ROOT, 'attachments/'+slug)) 
    return response 

아파치가 403을 상관없이 발생 사용자가 로그인되어있는 경우

배포 서버를 통해 뷰에 액세스 할 수 있지만 데이터가 전송되지 않습니다.

무엇이 잘못 되었나요?

답변

1

나는 정확히 똑같은 작업을하려고했는데 WSGIScriptAlias를 사용하지 않고 대신 wsgi 처리기를 정의한 디렉토리에 일반 별칭을 사용하는 것으로 나타났습니다. 뷰에서는 기본적으로 django.views.static.serve 주위에 래퍼를 작성했습니다.

내 아파치 conf의이 같은 찾고 결국 :

# myproject 
<VirtualHost *:8080> 
    #DocumentRoot /var/www/myproject/public 
    ServerName myproject 
    ErrorLog /var/www/myproject/logs/apache_error_log 
    CustomLog /var/www/myproject/logs/apache_access_log common 

    AliasMatch ^/(media/uploads/protected/.*) /var/www/myproject/src/myproject-trunk/server/django.wsgi/$1 
    Alias /media/ /var/www/myproject/public/media/ 
    Alias//var/www/myproject/src/myproject-trunk/server/django.wsgi/ 

    <Directory /var/www/myproject/src/myproject-trunk/server> 
     Options ExecCGI 
     AddHandler wsgi-script .wsgi 
     # WSGIApplicationGroup %{GLOBAL} 
     Order allow,deny 
     Allow from all 
    </Directory> 

    <Directory /var/www/myproject/public/media> 
     Order deny,allow 
     Allow from all 
    </Directory> 
</VirtualHost> 
1

시도가 처음 개발 서버에 집중 - 그것은 간단한 설정 때문에 덜 오류 경향이 있기 때문이다.

아마도이 시도 :


그러나 아파치가 제공하는 파일을 할 것이기 때문에, 당신이 어떤 파일을 제공하지 않습니다 dev에 서버를 사용하여

@login_required def sendfile(request, slug): 
    ## these are never used  
    # app, content_object_id, img = slug.split('/') 
    # project_file = get_object_or_404(Attachment, attachment_file = 'attachments/'+slug) 

    response = HttpResponse() 
    response['X-Sendfile'] = os.path.join(settings.MEDIA_ROOT, 'attachments/'+slug) 

    import pdb; pdb.set_trace() 
    # your development server will stop here 
    # you can now inspect the your context, e.g. 
    # >> p response['X-Sendfile'] 
    # this should print the value of response['X-Sendfile'] 
    # >> c 
    # this will continue program execution 
    # for more commands see http://www.python.org/doc/2.4/lib/debugger-commands.html 

    content_type = 'application/octet-stream' 
    response['Content-Type'] = content_type 
    # Content-Disposition filename is only for suggesting a name for the file 
    # when the user tries to download it, e.g.: 
    # response['Content-Disposition'] = 'attachment; filename='localfile.txt' 
    response['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(os.path.join(settings.MEDIA_ROOT, 'attachments/'+slug)) 
    return response 
. 아파치에 보낸 데이터가 올바른지 확인할 수 있습니다. 또는 사용

response = HttpResponse() 
response['X-Sendfile'] = os.path.join(settings.MEDIA_ROOT, 'attachments/'+slug) 

일부 링크의

f = open(os.path.join(settings.MEDIA_ROOT, 'attachments/'+slug), 'rb') 
response = HttpResponse(f.read()) 

대신 (프로덕션 서버에 대한 권장하지 않음) :

+0

내가 내 생산을 요구하고있다 서버 설정. dev 서버를 실행할 때 뷰가 호출된다는 것은 URL이 올바른지 증명하는 것입니다. – vikingosegundo

+0

아파치 설정을 실행하는 데 도움이 될 수 있습니다. http://serverfault.com/questions/59791/configure-apache-to-handle-a-sub-path-using-wsgi –

관련 문제