2018-07-30 22:16:26 +02:00
|
|
|
# See https://zulip.readthedocs.io/en/latest/subsystems/thumbnailing.html
|
2017-11-09 16:31:57 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import base64
|
|
|
|
import logging
|
2020-07-07 01:26:50 +02:00
|
|
|
import urllib.parse
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-11-09 16:31:57 +01:00
|
|
|
from tc_aws.loaders import s3_loader
|
|
|
|
from thumbor.context import Context
|
2020-06-11 00:54:34 +02:00
|
|
|
from thumbor.loaders import LoaderResult, file_loader, https_loader
|
|
|
|
|
2017-11-09 16:31:57 +01:00
|
|
|
from .helpers import (
|
2020-06-11 00:54:34 +02:00
|
|
|
THUMBOR_EXTERNAL_TYPE,
|
|
|
|
THUMBOR_LOCAL_FILE_TYPE,
|
|
|
|
THUMBOR_S3_TYPE,
|
2018-03-08 09:37:09 +01:00
|
|
|
separate_url_and_source_type,
|
2017-11-09 16:31:57 +01:00
|
|
|
)
|
|
|
|
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2020-04-17 19:42:20 +02:00
|
|
|
def get_not_found_result() -> LoaderResult:
|
2017-11-09 16:31:57 +01:00
|
|
|
result = LoaderResult()
|
|
|
|
result.error = LoaderResult.ERROR_NOT_FOUND
|
|
|
|
result.successful = False
|
|
|
|
return result
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-04-17 19:42:20 +02:00
|
|
|
async def load(context: Context, url: str) -> LoaderResult:
|
2018-03-08 09:37:09 +01:00
|
|
|
source_type, encoded_url = separate_url_and_source_type(url)
|
2021-02-12 08:20:45 +01:00
|
|
|
actual_url = base64.urlsafe_b64decode(urllib.parse.unquote(encoded_url)).decode("utf-8")
|
2017-11-09 16:31:57 +01:00
|
|
|
|
|
|
|
if source_type == THUMBOR_S3_TYPE:
|
2021-02-12 08:20:45 +01:00
|
|
|
if actual_url.startswith("/user_uploads/"):
|
|
|
|
actual_url = actual_url[len("/user_uploads/") :]
|
2018-09-01 14:15:02 +02:00
|
|
|
else:
|
|
|
|
raise AssertionError("Unexpected s3 file.")
|
|
|
|
|
2020-03-21 04:31:01 +01:00
|
|
|
return await s3_loader.load(context, actual_url)
|
2017-11-09 16:31:57 +01:00
|
|
|
elif source_type == THUMBOR_LOCAL_FILE_TYPE:
|
2021-02-12 08:20:45 +01:00
|
|
|
if actual_url.startswith("/user_uploads/"):
|
|
|
|
actual_url = actual_url[len("/user_uploads/") :]
|
|
|
|
local_file_path_prefix = "files/"
|
2018-08-11 02:20:43 +02:00
|
|
|
else:
|
|
|
|
raise AssertionError("Unexpected local file.")
|
|
|
|
|
2019-08-10 00:30:35 +02:00
|
|
|
patched_local_url = local_file_path_prefix + actual_url
|
2020-03-21 04:31:01 +01:00
|
|
|
return await file_loader.load(context, patched_local_url)
|
2017-11-09 16:31:57 +01:00
|
|
|
elif source_type == THUMBOR_EXTERNAL_TYPE:
|
2020-03-21 04:31:01 +01:00
|
|
|
return await https_loader.load(context, actual_url)
|
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
logging.warning("INVALID SOURCE TYPE: " + source_type)
|
2020-03-21 04:31:01 +01:00
|
|
|
return get_not_found_result()
|