upload: Fix browser caching of uploads with local uploads backend.

Apparently, our change in b8a1050fc4 to
stop caching responses on API endpoints accidentally ended up
affecting uploaded files as well.

Fix this by explicitly setting a Cache-Control header in our Sendfile
responses, as well as changing our outer API caching code to only set
the never cache headers if the view function didn't explicitly specify
them itself.

This is not directly related to #13088, as that is a similar issue
with the S3 backend.

Thanks to Gert Burger for the report.
This commit is contained in:
Tim Abbott
2019-10-01 15:10:30 -07:00
parent 16c9d63056
commit c869a3bf82
5 changed files with 26 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
from django.http import HttpRequest, HttpResponse, HttpResponseForbidden, \
HttpResponseNotFound
from django.shortcuts import redirect
from django.utils.cache import patch_cache_control
from django.utils.translation import ugettext as _
from zerver.lib.response import json_success, json_error
@@ -40,8 +41,10 @@ def serve_local(request: HttpRequest, path_id: str) -> HttpResponse:
mimetype, encoding = guess_type(local_path)
attachment = mimetype not in INLINE_MIME_TYPES
return sendfile(request, local_path, attachment=attachment,
mimetype=mimetype, encoding=encoding)
response = sendfile(request, local_path, attachment=attachment,
mimetype=mimetype, encoding=encoding)
patch_cache_control(response, private=True, immutable=True)
return response
def serve_file_backend(request: HttpRequest, user_profile: UserProfile,
realm_id_str: str, filename: str) -> HttpResponse: