2022-11-30 19:11:35 +01:00
|
|
|
import os
|
2020-06-11 00:54:34 +02:00
|
|
|
from mimetypes import guess_type
|
2021-11-02 15:42:58 +01:00
|
|
|
from typing import Union
|
2022-11-30 19:11:35 +01:00
|
|
|
from urllib.parse import quote, urlparse
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
2021-11-02 15:42:58 +01:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2022-06-21 22:23:34 +02:00
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
2022-12-06 22:26:39 +01:00
|
|
|
from django.http import (
|
|
|
|
FileResponse,
|
|
|
|
HttpRequest,
|
|
|
|
HttpResponse,
|
|
|
|
HttpResponseBase,
|
|
|
|
HttpResponseForbidden,
|
|
|
|
HttpResponseNotFound,
|
|
|
|
)
|
2016-06-07 01:09:05 +02:00
|
|
|
from django.shortcuts import redirect
|
2019-10-02 00:10:30 +02:00
|
|
|
from django.utils.cache import patch_cache_control
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2016-06-07 01:09:05 +02:00
|
|
|
|
2021-11-02 15:42:58 +01:00
|
|
|
from zerver.context_processors import get_valid_realm_from_request
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.response import json_success
|
2022-12-14 21:51:37 +01:00
|
|
|
from zerver.lib.upload import check_upload_within_quota, upload_message_image_from_request
|
|
|
|
from zerver.lib.upload.base import INLINE_MIME_TYPES
|
|
|
|
from zerver.lib.upload.local import (
|
2020-06-11 00:54:34 +02:00
|
|
|
generate_unauthed_file_access_url,
|
|
|
|
get_local_file_path,
|
|
|
|
get_local_file_path_id_from_token,
|
|
|
|
)
|
2022-12-14 21:51:37 +01:00
|
|
|
from zerver.lib.upload.s3 import get_signed_upload_url
|
2016-06-17 19:48:17 +02:00
|
|
|
from zerver.models import UserProfile, validate_attachment_request
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2016-06-07 01:09:05 +02:00
|
|
|
|
2022-11-30 19:11:35 +01:00
|
|
|
def patch_disposition_header(response: HttpResponse, url: str, is_attachment: bool) -> None:
|
|
|
|
"""
|
|
|
|
This replicates django.utils.http.content_disposition_header's
|
|
|
|
algorithm, which is introduced in Django 4.2.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# TODO: Replace this with django.utils.http.content_disposition_header when we upgrade in Django 4.2
|
|
|
|
disposition = "attachment" if is_attachment else "inline"
|
|
|
|
|
|
|
|
# Trim to only the filename part of the URL
|
|
|
|
filename = os.path.basename(urlparse(url).path)
|
|
|
|
|
|
|
|
# Content-Disposition is defined in RFC 6266:
|
|
|
|
# https://datatracker.ietf.org/doc/html/rfc6266
|
|
|
|
#
|
|
|
|
# For the 'filename' attribute of it, see RFC 8187:
|
|
|
|
# https://datatracker.ietf.org/doc/html/rfc8187
|
|
|
|
try:
|
|
|
|
# If the filename is pure-ASCII (determined by trying to
|
|
|
|
# encode it as such), then we escape slashes and quotes, and
|
|
|
|
# provide a filename="..."
|
|
|
|
filename.encode("ascii")
|
|
|
|
file_expr = 'filename="{}"'.format(filename.replace("\\", "\\\\").replace('"', r"\""))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
# If it contains non-ASCII characters, we URI-escape it and
|
|
|
|
# provide a filename*=encoding'language'value
|
|
|
|
file_expr = "filename*=utf-8''{}".format(quote(filename))
|
|
|
|
|
|
|
|
response.headers["Content-Disposition"] = f"{disposition}; {file_expr}"
|
|
|
|
|
|
|
|
|
2022-12-06 22:26:39 +01:00
|
|
|
def internal_nginx_redirect(internal_path: str) -> HttpResponse:
|
|
|
|
# The following headers from this initial response are
|
|
|
|
# _preserved_, if present, and sent unmodified to the client;
|
|
|
|
# all other headers are overridden by the redirected URL:
|
|
|
|
# - Content-Type
|
|
|
|
# - Content-Disposition
|
|
|
|
# - Accept-Ranges
|
|
|
|
# - Set-Cookie
|
|
|
|
# - Cache-Control
|
|
|
|
# - Expires
|
|
|
|
# As such, we unset the Content-type header to allow nginx to set
|
|
|
|
# it from the static file; the caller can set Content-Disposition
|
|
|
|
# and Cache-Control on this response as they desire, and the
|
|
|
|
# client will see those values.
|
|
|
|
response = HttpResponse()
|
|
|
|
response["X-Accel-Redirect"] = internal_path
|
|
|
|
del response["Content-Type"]
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2022-03-22 04:38:18 +01:00
|
|
|
def serve_s3(
|
|
|
|
request: HttpRequest, url_path: str, url_only: bool, download: bool = False
|
|
|
|
) -> HttpResponse:
|
|
|
|
url = get_signed_upload_url(url_path, download=download)
|
2020-04-08 00:27:24 +02:00
|
|
|
if url_only:
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data=dict(url=url))
|
2016-06-08 11:22:06 +02:00
|
|
|
|
2020-04-08 00:27:24 +02:00
|
|
|
return redirect(url)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-03-22 04:38:18 +01:00
|
|
|
def serve_local(
|
|
|
|
request: HttpRequest, path_id: str, url_only: bool, download: bool = False
|
2022-12-06 22:26:39 +01:00
|
|
|
) -> HttpResponseBase:
|
2016-06-09 12:19:56 +02:00
|
|
|
local_path = get_local_file_path(path_id)
|
|
|
|
if local_path is None:
|
2021-02-12 08:20:45 +01:00
|
|
|
return HttpResponseNotFound("<p>File not found</p>")
|
2018-03-13 07:08:27 +01:00
|
|
|
|
2020-04-08 00:27:24 +02:00
|
|
|
if url_only:
|
|
|
|
url = generate_unauthed_file_access_url(path_id)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data=dict(url=url))
|
2020-04-08 00:27:24 +02:00
|
|
|
|
2019-09-10 00:21:31 +02:00
|
|
|
mimetype, encoding = guess_type(local_path)
|
2022-03-22 04:38:18 +01:00
|
|
|
attachment = download or mimetype not in INLINE_MIME_TYPES
|
2018-03-13 07:08:27 +01:00
|
|
|
|
2022-12-06 22:26:39 +01:00
|
|
|
if settings.DEVELOPMENT:
|
|
|
|
# In development, we do not have the nginx server to offload
|
|
|
|
# the response to; serve it directly ourselves.
|
|
|
|
# FileResponse handles setting Content-Disposition, etc.
|
|
|
|
response: HttpResponseBase = FileResponse(open(local_path, "rb"), as_attachment=attachment)
|
|
|
|
patch_cache_control(response, private=True, immutable=True)
|
|
|
|
return response
|
|
|
|
|
2022-12-09 01:12:06 +01:00
|
|
|
response = internal_nginx_redirect(quote(f"/internal/uploads/{path_id}"))
|
2022-11-30 19:11:35 +01:00
|
|
|
patch_disposition_header(response, local_path, attachment)
|
2022-12-06 22:26:39 +01:00
|
|
|
patch_cache_control(response, private=True, immutable=True)
|
2019-10-02 00:10:30 +02:00
|
|
|
return response
|
2016-06-09 12:19:56 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-03-22 04:38:18 +01:00
|
|
|
def serve_file_download_backend(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, realm_id_str: str, filename: str
|
2022-12-06 22:26:39 +01:00
|
|
|
) -> HttpResponseBase:
|
2022-03-22 04:38:18 +01:00
|
|
|
return serve_file(request, user_profile, realm_id_str, filename, url_only=False, download=True)
|
|
|
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def serve_file_backend(
|
2021-11-02 15:42:58 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
maybe_user_profile: Union[UserProfile, AnonymousUser],
|
|
|
|
realm_id_str: str,
|
|
|
|
filename: str,
|
2022-12-06 22:26:39 +01:00
|
|
|
) -> HttpResponseBase:
|
2021-11-02 15:42:58 +01:00
|
|
|
return serve_file(request, maybe_user_profile, realm_id_str, filename, url_only=False)
|
2020-04-08 00:27:24 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def serve_file_url_backend(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, realm_id_str: str, filename: str
|
2022-12-06 22:26:39 +01:00
|
|
|
) -> HttpResponseBase:
|
2020-04-08 00:27:24 +02:00
|
|
|
"""
|
|
|
|
We should return a signed, short-lived URL
|
|
|
|
that the client can use for native mobile download, rather than serving a redirect.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return serve_file(request, user_profile, realm_id_str, filename, url_only=True)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def serve_file(
|
|
|
|
request: HttpRequest,
|
2021-11-02 15:42:58 +01:00
|
|
|
maybe_user_profile: Union[UserProfile, AnonymousUser],
|
2021-02-12 08:19:30 +01:00
|
|
|
realm_id_str: str,
|
|
|
|
filename: str,
|
|
|
|
url_only: bool = False,
|
2022-03-22 04:38:18 +01:00
|
|
|
download: bool = False,
|
2022-12-06 22:26:39 +01:00
|
|
|
) -> HttpResponseBase:
|
2020-06-10 06:41:04 +02:00
|
|
|
path_id = f"{realm_id_str}/{filename}"
|
2021-11-02 15:42:58 +01:00
|
|
|
realm = get_valid_realm_from_request(request)
|
|
|
|
is_authorized = validate_attachment_request(maybe_user_profile, path_id, realm)
|
2016-06-17 19:48:17 +02:00
|
|
|
|
|
|
|
if is_authorized is None:
|
|
|
|
return HttpResponseNotFound(_("<p>File not found.</p>"))
|
|
|
|
if not is_authorized:
|
|
|
|
return HttpResponseForbidden(_("<p>You are not authorized to view this file.</p>"))
|
2016-06-08 11:22:06 +02:00
|
|
|
if settings.LOCAL_UPLOADS_DIR is not None:
|
2022-03-22 04:38:18 +01:00
|
|
|
return serve_local(request, path_id, url_only, download=download)
|
2020-04-08 00:27:24 +02:00
|
|
|
|
2022-03-22 04:38:18 +01:00
|
|
|
return serve_s3(request, path_id, url_only, download=download)
|
2020-04-08 00:27:24 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-12-06 22:26:39 +01:00
|
|
|
def serve_local_file_unauthed(request: HttpRequest, token: str, filename: str) -> HttpResponseBase:
|
2020-04-08 00:27:24 +02:00
|
|
|
path_id = get_local_file_path_id_from_token(token)
|
|
|
|
if path_id is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Invalid token"))
|
2021-02-12 08:20:45 +01:00
|
|
|
if path_id.split("/")[-1] != filename:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Invalid filename"))
|
2016-06-08 11:22:06 +02:00
|
|
|
|
2020-04-08 00:27:24 +02:00
|
|
|
return serve_local(request, path_id, url_only=False)
|
2016-06-08 11:22:06 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def upload_file_backend(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2016-06-27 19:28:09 +02:00
|
|
|
if len(request.FILES) == 0:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("You must specify a file to upload"))
|
2016-06-27 19:28:09 +02:00
|
|
|
if len(request.FILES) != 1:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("You may only upload one file at a time"))
|
2016-06-27 19:28:09 +02:00
|
|
|
|
|
|
|
user_file = list(request.FILES.values())[0]
|
2022-06-21 22:23:34 +02:00
|
|
|
assert isinstance(user_file, UploadedFile)
|
2018-02-02 05:43:18 +01:00
|
|
|
file_size = user_file.size
|
2022-06-21 22:23:34 +02:00
|
|
|
assert file_size is not None
|
2017-03-02 11:17:10 +01:00
|
|
|
if settings.MAX_FILE_UPLOAD_SIZE * 1024 * 1024 < file_size:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2021-02-12 08:19:30 +01:00
|
|
|
_("Uploaded file is larger than the allowed limit of {} MiB").format(
|
|
|
|
settings.MAX_FILE_UPLOAD_SIZE,
|
|
|
|
)
|
|
|
|
)
|
2018-01-26 16:13:33 +01:00
|
|
|
check_upload_within_quota(user_profile.realm, file_size)
|
2016-06-27 19:28:09 +02:00
|
|
|
|
2022-07-27 22:54:31 +02:00
|
|
|
uri = upload_message_image_from_request(user_file, user_profile, file_size)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"uri": uri})
|