Skip to content

Commit

Permalink
Use django-sendfile for serving local files.
Browse files Browse the repository at this point in the history
  • Loading branch information
rahuldeve committed Jul 18, 2016
1 parent 5d18ef6 commit 81dd185
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion puppet/zulip/files/nginx/sites-available/zulip-enterprise
Expand Up @@ -12,7 +12,8 @@ server {
ssl_certificate /etc/ssl/certs/zulip.combined-chain.crt;
ssl_certificate_key /etc/ssl/private/zulip.key;

location /user_uploads {
location /user_uploads_internal {
internal;
add_header X-Content-Type-Options nosniff;
include /etc/nginx/zulip-include/uploads.types;
alias /home/zulip/uploads/files;
Expand Down
11 changes: 4 additions & 7 deletions zerver/views/upload.py
Expand Up @@ -14,6 +14,8 @@
from zerver.models import UserProfile, validate_attachment_request
from django.conf import settings

from sendfile import sendfile

def serve_s3(request, user_profile, realm_id_str, filename, redir):
# type: (HttpRequest, UserProfile, str, str, bool) -> HttpResponse
url_path = "%s/%s" % (realm_id_str, filename)
Expand All @@ -36,18 +38,13 @@ def serve_s3(request, user_profile, realm_id_str, filename, redir):
else:
return HttpResponseForbidden()

# TODO: Rewrite this once we have django-sendfile
def serve_local(request, path_id):
# type: (HttpRequest, str) -> HttpResponse
import os
import mimetypes
local_path = get_local_file_path(path_id)
if local_path is None:
return HttpResponseNotFound('<p>File not found</p>')
filename = os.path.basename(local_path)
response = FileResponse(open(local_path, 'rb'), content_type = mimetypes.guess_type(filename))
return response
response['Content-Disposition'] = 'attachment; filename=%s' % (filename)
else:
return sendfile(request, local_path)

@has_request_variables
def serve_file_backend(request, user_profile, realm_id_str, filename,
Expand Down
1 change: 1 addition & 0 deletions zproject/dev_settings.py
Expand Up @@ -21,4 +21,5 @@
# Disable Camo in development
CAMO_URI = ''
OPEN_REALM_CREATION = True
SENDFILE_BACKEND = 'sendfile.backends.development'

4 changes: 4 additions & 0 deletions zproject/settings.py
Expand Up @@ -81,6 +81,10 @@ def get_secret(key):
# Import local_settings after determining the deployment/machine type
if PRODUCTION:
from .local_settings import *
# Configuration for django-sendfile
SENDFILE_BACKEND = 'sendfile.backends.nginx'
SENDFILE_ROOT = os.path.join(LOCAL_UPLOADS_DIR, 'files')
SENDFILE_URL = '/user_uploads_internal'
else:
from .dev_settings import *

Expand Down

0 comments on commit 81dd185

Please sign in to comment.