2018-07-30 22:16:26 +02:00
|
|
|
# See https://zulip.readthedocs.io/en/latest/subsystems/thumbnailing.html
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseForbidden
|
2018-03-08 09:37:09 +01:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.utils.translation import ugettext as _
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2018-03-08 09:37:09 +01:00
|
|
|
from zerver.lib.thumbnail import generate_thumbnail_url
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import UserProfile, validate_attachment_request
|
|
|
|
|
2018-03-08 09:37:09 +01:00
|
|
|
|
|
|
|
def validate_thumbnail_request(user_profile: UserProfile, path: str) -> Optional[bool]:
|
|
|
|
# path here does not have a leading / as it is parsed from request hitting the
|
|
|
|
# thumbnail endpoint (defined in urls.py) that way.
|
2021-02-12 08:20:45 +01:00
|
|
|
if path.startswith("user_uploads/"):
|
|
|
|
path_id = path[len("user_uploads/") :]
|
2018-03-08 09:37:09 +01:00
|
|
|
return validate_attachment_request(user_profile, path_id)
|
|
|
|
|
|
|
|
# This is an external link and we don't enforce restricted view policy here.
|
|
|
|
return True
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-08 09:37:09 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def backend_serve_thumbnail(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
url: str = REQ(),
|
|
|
|
size_requested: str = REQ("size"),
|
|
|
|
) -> HttpResponse:
|
2018-03-08 09:37:09 +01:00
|
|
|
if not validate_thumbnail_request(user_profile, url):
|
|
|
|
return HttpResponseForbidden(_("<p>You are not authorized to view this file.</p>"))
|
|
|
|
|
|
|
|
size = None
|
2021-02-12 08:20:45 +01:00
|
|
|
if size_requested == "thumbnail":
|
|
|
|
size = "0x300"
|
|
|
|
elif size_requested == "full":
|
|
|
|
size = "0x0"
|
2018-03-08 09:37:09 +01:00
|
|
|
|
|
|
|
if size is None:
|
|
|
|
return HttpResponseForbidden(_("<p>Invalid size.</p>"))
|
|
|
|
|
|
|
|
thumbnail_url = generate_thumbnail_url(url, size)
|
|
|
|
return redirect(thumbnail_url)
|