2022-12-14 21:51:37 +01:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import secrets
|
|
|
|
import shutil
|
2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Callable, Iterator
|
2023-02-27 20:55:33 +01:00
|
|
|
from datetime import datetime
|
2024-09-19 22:48:03 +02:00
|
|
|
from typing import IO, Any, Literal
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-09-10 20:33:25 +02:00
|
|
|
import pyvips
|
2022-12-14 21:51:37 +01:00
|
|
|
from django.conf import settings
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-06-25 21:03:49 +02:00
|
|
|
from zerver.lib.mime_types import guess_type
|
2024-11-08 23:21:02 +01:00
|
|
|
from zerver.lib.thumbnail import resize_logo, resize_realm_icon
|
2023-02-27 20:55:33 +01:00
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime
|
2024-09-10 20:33:25 +02:00
|
|
|
from zerver.lib.upload.base import StreamingSourceWithSize, ZulipUploadBackend
|
2022-12-14 21:51:37 +01:00
|
|
|
from zerver.lib.utils import assert_is_not_none
|
|
|
|
from zerver.models import Realm, RealmEmoji, UserProfile
|
|
|
|
|
|
|
|
|
2022-12-12 21:33:03 +01:00
|
|
|
def assert_is_local_storage_path(type: Literal["avatars", "files"], full_path: str) -> None:
|
|
|
|
"""
|
|
|
|
Verify that we are only reading and writing files under the
|
|
|
|
expected paths. This is expected to be already enforced at other
|
|
|
|
layers, via cleaning of user input, but we assert it here for
|
|
|
|
defense in depth.
|
|
|
|
"""
|
|
|
|
assert settings.LOCAL_UPLOADS_DIR is not None
|
|
|
|
type_path = os.path.join(settings.LOCAL_UPLOADS_DIR, type)
|
|
|
|
assert os.path.commonpath([type_path, full_path]) == type_path
|
|
|
|
|
|
|
|
|
|
|
|
def write_local_file(type: Literal["avatars", "files"], path: str, file_data: bytes) -> None:
|
2022-12-14 21:51:37 +01:00
|
|
|
file_path = os.path.join(assert_is_not_none(settings.LOCAL_UPLOADS_DIR), type, path)
|
2022-12-12 21:33:03 +01:00
|
|
|
assert_is_local_storage_path(type, file_path)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
|
|
|
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
|
|
|
with open(file_path, "wb") as f:
|
|
|
|
f.write(file_data)
|
|
|
|
|
|
|
|
|
2024-09-19 22:50:08 +02:00
|
|
|
def read_local_file(type: Literal["avatars", "files"], path: str) -> Iterator[bytes]:
|
2022-12-14 21:51:37 +01:00
|
|
|
file_path = os.path.join(assert_is_not_none(settings.LOCAL_UPLOADS_DIR), type, path)
|
2022-12-12 21:33:03 +01:00
|
|
|
assert_is_local_storage_path(type, file_path)
|
|
|
|
|
2022-12-14 21:51:37 +01:00
|
|
|
with open(file_path, "rb") as f:
|
2024-09-19 22:50:08 +02:00
|
|
|
yield from iter(lambda: f.read(4 * 1024 * 1024), b"")
|
2022-12-14 21:51:37 +01:00
|
|
|
|
|
|
|
|
2022-12-12 21:33:03 +01:00
|
|
|
def delete_local_file(type: Literal["avatars", "files"], path: str) -> bool:
|
2022-12-14 21:51:37 +01:00
|
|
|
file_path = os.path.join(assert_is_not_none(settings.LOCAL_UPLOADS_DIR), type, path)
|
2022-12-12 21:33:03 +01:00
|
|
|
assert_is_local_storage_path(type, file_path)
|
|
|
|
|
2022-12-14 21:51:37 +01:00
|
|
|
if os.path.isfile(file_path):
|
|
|
|
os.remove(file_path)
|
2024-07-13 04:25:47 +02:00
|
|
|
|
|
|
|
# Remove as many directories up the tree as are now empty
|
|
|
|
directory = os.path.dirname(file_path)
|
|
|
|
while directory != settings.LOCAL_UPLOADS_DIR:
|
|
|
|
try:
|
|
|
|
os.rmdir(directory)
|
|
|
|
directory = os.path.dirname(directory)
|
|
|
|
except OSError:
|
|
|
|
break
|
2022-12-14 21:51:37 +01:00
|
|
|
return True
|
|
|
|
file_name = path.split("/")[-1]
|
|
|
|
logging.warning("%s does not exist. Its entry in the database will be removed.", file_name)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class LocalUploadBackend(ZulipUploadBackend):
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2022-12-14 21:51:37 +01:00
|
|
|
def get_public_upload_root_url(self) -> str:
|
|
|
|
return "/user_avatars/"
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-06-20 18:11:59 +02:00
|
|
|
def generate_message_upload_path(self, realm_id: str, sanitized_file_name: str) -> str:
|
2022-12-14 21:51:37 +01:00
|
|
|
# Split into 256 subdirectories to prevent directories from getting too big
|
|
|
|
return "/".join(
|
|
|
|
[
|
|
|
|
realm_id,
|
|
|
|
format(random.randint(0, 255), "x"),
|
|
|
|
secrets.token_urlsafe(18),
|
2024-06-20 18:11:59 +02:00
|
|
|
sanitized_file_name,
|
2022-12-14 21:51:37 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-02-28 03:46:41 +01:00
|
|
|
def upload_message_attachment(
|
2022-12-14 21:51:37 +01:00
|
|
|
self,
|
2024-06-20 18:11:59 +02:00
|
|
|
path_id: str,
|
2024-08-29 23:39:07 +02:00
|
|
|
filename: str,
|
2024-06-20 18:19:25 +02:00
|
|
|
content_type: str,
|
2022-12-14 21:51:37 +01:00
|
|
|
file_data: bytes,
|
2024-06-20 23:58:27 +02:00
|
|
|
user_profile: UserProfile | None,
|
2024-06-20 18:11:59 +02:00
|
|
|
) -> None:
|
|
|
|
write_local_file("files", path_id, file_data)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-09-19 22:48:03 +02:00
|
|
|
def save_attachment_contents(self, path_id: str, filehandle: IO[bytes]) -> None:
|
2024-09-19 22:50:08 +02:00
|
|
|
for chunk in read_local_file("files", path_id):
|
|
|
|
filehandle.write(chunk)
|
2023-03-14 17:16:41 +01:00
|
|
|
|
2024-09-10 20:33:25 +02:00
|
|
|
@override
|
|
|
|
def attachment_vips_source(self, path_id: str) -> StreamingSourceWithSize:
|
|
|
|
file_path = os.path.join(assert_is_not_none(settings.LOCAL_UPLOADS_DIR), "files", path_id)
|
|
|
|
assert_is_local_storage_path("files", file_path)
|
|
|
|
source = pyvips.Source.new_from_file(file_path)
|
|
|
|
return StreamingSourceWithSize(size=os.path.getsize(file_path), source=source)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-02-28 03:49:04 +01:00
|
|
|
def delete_message_attachment(self, path_id: str) -> bool:
|
2022-12-14 21:51:37 +01:00
|
|
|
return delete_local_file("files", path_id)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-06-20 23:58:27 +02:00
|
|
|
def all_message_attachments(
|
2024-09-24 20:44:03 +02:00
|
|
|
self,
|
|
|
|
include_thumbnails: bool = False,
|
|
|
|
prefix: str = "",
|
2024-06-20 23:58:27 +02:00
|
|
|
) -> Iterator[tuple[str, datetime]]:
|
2023-02-27 20:55:33 +01:00
|
|
|
assert settings.LOCAL_UPLOADS_DIR is not None
|
2024-06-20 23:58:27 +02:00
|
|
|
top = settings.LOCAL_UPLOADS_DIR + "/files"
|
2024-09-24 20:44:03 +02:00
|
|
|
start = top
|
|
|
|
if prefix != "":
|
|
|
|
start += f"/{prefix}"
|
|
|
|
for dirname, subdirnames, files in os.walk(start):
|
2024-06-20 23:58:27 +02:00
|
|
|
if not include_thumbnails and dirname == top and "thumbnail" in subdirnames:
|
|
|
|
subdirnames.remove("thumbnail")
|
2023-02-27 20:55:33 +01:00
|
|
|
for f in files:
|
|
|
|
fullpath = os.path.join(dirname, f)
|
|
|
|
yield (
|
2024-09-24 20:44:03 +02:00
|
|
|
os.path.relpath(fullpath, top),
|
2023-02-27 20:55:33 +01:00
|
|
|
timestamp_to_datetime(os.path.getmtime(fullpath)),
|
|
|
|
)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-02-28 04:33:25 +01:00
|
|
|
def get_avatar_url(self, hash_key: str, medium: bool = False) -> str:
|
2024-06-25 21:03:49 +02:00
|
|
|
return "/user_avatars/" + self.get_avatar_path(hash_key, medium)
|
2023-02-28 04:33:25 +01:00
|
|
|
|
2024-06-25 21:03:49 +02:00
|
|
|
@override
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_avatar_contents(self, file_path: str) -> tuple[bytes, str]:
|
2024-09-19 22:50:08 +02:00
|
|
|
image_data = b"".join(read_local_file("avatars", file_path + ".original"))
|
2024-06-25 21:03:49 +02:00
|
|
|
content_type = guess_type(file_path)[0]
|
|
|
|
return image_data, content_type or "application/octet-stream"
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-06-25 21:03:49 +02:00
|
|
|
def upload_single_avatar_image(
|
2022-12-14 21:51:37 +01:00
|
|
|
self,
|
2024-06-25 21:03:49 +02:00
|
|
|
file_path: str,
|
|
|
|
*,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
image_data: bytes,
|
2024-07-12 02:30:23 +02:00
|
|
|
content_type: str | None,
|
2024-06-13 14:57:18 +02:00
|
|
|
future: bool = True,
|
2022-12-14 21:51:37 +01:00
|
|
|
) -> None:
|
2024-07-02 14:08:44 +02:00
|
|
|
write_local_file("avatars", file_path, image_data)
|
2023-02-28 04:33:25 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-06-25 21:03:49 +02:00
|
|
|
def delete_avatar_image(self, path_id: str) -> None:
|
2022-12-14 21:51:37 +01:00
|
|
|
delete_local_file("avatars", path_id + ".original")
|
2024-06-25 21:03:49 +02:00
|
|
|
delete_local_file("avatars", self.get_avatar_path(path_id, True))
|
|
|
|
delete_local_file("avatars", self.get_avatar_path(path_id, False))
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-02-28 04:33:25 +01:00
|
|
|
def get_realm_icon_url(self, realm_id: int, version: int) -> str:
|
|
|
|
return f"/user_avatars/{realm_id}/realm/icon.png?version={version}"
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-07-10 22:54:47 +02:00
|
|
|
def upload_realm_icon_image(
|
|
|
|
self, icon_file: IO[bytes], user_profile: UserProfile, content_type: str
|
|
|
|
) -> None:
|
2022-12-14 21:51:37 +01:00
|
|
|
upload_path = self.realm_avatar_and_logo_path(user_profile.realm)
|
|
|
|
image_data = icon_file.read()
|
2022-12-12 21:33:03 +01:00
|
|
|
write_local_file("avatars", os.path.join(upload_path, "icon.original"), image_data)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-11-08 23:21:02 +01:00
|
|
|
resized_data = resize_realm_icon(image_data)
|
2022-12-12 21:33:03 +01:00
|
|
|
write_local_file("avatars", os.path.join(upload_path, "icon.png"), resized_data)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-02-28 04:33:25 +01:00
|
|
|
def get_realm_logo_url(self, realm_id: int, version: int, night: bool) -> str:
|
|
|
|
if night:
|
|
|
|
file_name = "night_logo.png"
|
|
|
|
else:
|
|
|
|
file_name = "logo.png"
|
|
|
|
return f"/user_avatars/{realm_id}/realm/{file_name}?version={version}"
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2022-12-14 21:51:37 +01:00
|
|
|
def upload_realm_logo_image(
|
2024-07-10 22:54:47 +02:00
|
|
|
self, logo_file: IO[bytes], user_profile: UserProfile, night: bool, content_type: str
|
2022-12-14 21:51:37 +01:00
|
|
|
) -> None:
|
|
|
|
upload_path = self.realm_avatar_and_logo_path(user_profile.realm)
|
|
|
|
if night:
|
|
|
|
original_file = "night_logo.original"
|
|
|
|
resized_file = "night_logo.png"
|
|
|
|
else:
|
|
|
|
original_file = "logo.original"
|
|
|
|
resized_file = "logo.png"
|
|
|
|
image_data = logo_file.read()
|
2022-12-12 21:33:03 +01:00
|
|
|
write_local_file("avatars", os.path.join(upload_path, original_file), image_data)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
|
|
|
resized_data = resize_logo(image_data)
|
2022-12-12 21:33:03 +01:00
|
|
|
write_local_file("avatars", os.path.join(upload_path, resized_file), resized_data)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-02-28 04:33:25 +01:00
|
|
|
def get_emoji_url(self, emoji_file_name: str, realm_id: int, still: bool = False) -> str:
|
|
|
|
if still:
|
|
|
|
return os.path.join(
|
|
|
|
"/user_avatars",
|
|
|
|
RealmEmoji.STILL_PATH_ID_TEMPLATE.format(
|
|
|
|
realm_id=realm_id,
|
|
|
|
emoji_filename_without_extension=os.path.splitext(emoji_file_name)[0],
|
|
|
|
),
|
|
|
|
)
|
2022-12-14 21:51:37 +01:00
|
|
|
else:
|
2023-02-28 04:33:25 +01:00
|
|
|
return os.path.join(
|
|
|
|
"/user_avatars",
|
|
|
|
RealmEmoji.PATH_ID_TEMPLATE.format(
|
|
|
|
realm_id=realm_id, emoji_file_name=emoji_file_name
|
|
|
|
),
|
|
|
|
)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-06-24 22:25:16 +02:00
|
|
|
def upload_single_emoji_image(
|
2024-07-12 02:30:23 +02:00
|
|
|
self, path: str, content_type: str | None, user_profile: UserProfile, image_data: bytes
|
2024-06-24 22:25:16 +02:00
|
|
|
) -> None:
|
|
|
|
write_local_file("avatars", path, image_data)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-02-28 04:33:25 +01:00
|
|
|
def get_export_tarball_url(self, realm: Realm, export_path: str) -> str:
|
|
|
|
# export_path has a leading `/`
|
2024-05-06 15:27:22 +02:00
|
|
|
return realm.url + export_path
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2022-12-14 21:51:37 +01:00
|
|
|
def upload_export_tarball(
|
|
|
|
self,
|
|
|
|
realm: Realm,
|
|
|
|
tarball_path: str,
|
2024-07-12 02:30:23 +02:00
|
|
|
percent_callback: Callable[[Any], None] | None = None,
|
2022-12-14 21:51:37 +01:00
|
|
|
) -> str:
|
|
|
|
path = os.path.join(
|
|
|
|
"exports",
|
|
|
|
str(realm.id),
|
|
|
|
secrets.token_urlsafe(18),
|
|
|
|
os.path.basename(tarball_path),
|
|
|
|
)
|
2022-12-12 22:02:25 +01:00
|
|
|
abs_path = os.path.join(assert_is_not_none(settings.LOCAL_AVATARS_DIR), path)
|
2022-12-14 21:51:37 +01:00
|
|
|
os.makedirs(os.path.dirname(abs_path), exist_ok=True)
|
|
|
|
shutil.copy(tarball_path, abs_path)
|
2022-04-07 01:52:23 +02:00
|
|
|
return self.get_export_tarball_url(realm, "/user_avatars/" + path)
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-07-12 02:30:23 +02:00
|
|
|
def delete_export_tarball(self, export_path: str) -> str | None:
|
2022-12-14 21:51:37 +01:00
|
|
|
# Get the last element of a list in the form ['user_avatars', '<file_path>']
|
|
|
|
assert export_path.startswith("/")
|
2024-09-03 19:42:14 +02:00
|
|
|
file_path = export_path.removeprefix("/").split("/", 1)[-1]
|
2022-12-14 21:51:37 +01:00
|
|
|
if delete_local_file("avatars", file_path):
|
|
|
|
return export_path
|
|
|
|
return None
|