2022-12-12 21:34:59 +01:00
|
|
|
import os
|
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-07-12 02:30:25 +02:00
|
|
|
from typing import IO, Any, BinaryIO
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-06-20 18:12:58 +02:00
|
|
|
from zerver.models import Realm, UserProfile
|
2022-12-14 21:51:37 +01:00
|
|
|
|
|
|
|
INLINE_MIME_TYPES = [
|
|
|
|
"application/pdf",
|
2024-06-18 23:41:51 +02:00
|
|
|
"audio/aac",
|
|
|
|
"audio/flac",
|
|
|
|
"audio/mp4",
|
|
|
|
"audio/mpeg",
|
|
|
|
"audio/wav",
|
|
|
|
"audio/webm",
|
|
|
|
"image/apng",
|
|
|
|
"image/avif",
|
2022-12-14 21:51:37 +01:00
|
|
|
"image/gif",
|
|
|
|
"image/jpeg",
|
|
|
|
"image/png",
|
|
|
|
"image/webp",
|
2023-10-03 06:08:09 +02:00
|
|
|
"video/mp4",
|
|
|
|
"video/webm",
|
2022-12-14 21:51:37 +01:00
|
|
|
# To avoid cross-site scripting attacks, DO NOT add types such
|
|
|
|
# as application/xhtml+xml, application/x-shockwave-flash,
|
|
|
|
# image/svg+xml, text/html, or text/xml.
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ZulipUploadBackend:
|
2023-02-28 04:33:25 +01:00
|
|
|
# Message attachment uploads
|
2022-12-14 21:51:37 +01:00
|
|
|
def get_public_upload_root_url(self) -> str:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
|
|
|
def generate_message_upload_path(self, realm_id: str, uploaded_file_name: str) -> str:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
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-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:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-03-14 17:16:41 +01:00
|
|
|
def save_attachment_contents(self, path_id: str, filehandle: BinaryIO) -> None:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2023-02-28 04:33:25 +01:00
|
|
|
def delete_message_attachment(self, path_id: str) -> bool:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def delete_message_attachments(self, path_ids: list[str]) -> None:
|
2023-02-28 04:44:29 +01:00
|
|
|
for path_id in path_ids:
|
|
|
|
self.delete_message_attachment(path_id)
|
|
|
|
|
2024-06-20 23:58:27 +02:00
|
|
|
def all_message_attachments(
|
|
|
|
self, include_thumbnails: bool = False
|
|
|
|
) -> Iterator[tuple[str, datetime]]:
|
2023-02-27 20:55:33 +01:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2023-02-28 04:33:25 +01:00
|
|
|
# Avatar image uploads
|
|
|
|
def get_avatar_url(self, hash_key: str, medium: bool = False) -> str:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_avatar_contents(self, file_path: str) -> tuple[bytes, str]:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-06-25 21:03:49 +02:00
|
|
|
def get_avatar_path(self, hash_key: str, medium: bool = False) -> str:
|
|
|
|
if medium:
|
|
|
|
return f"{hash_key}-medium.png"
|
|
|
|
else:
|
|
|
|
return f"{hash_key}.png"
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-06-25 21:03:49 +02:00
|
|
|
def upload_single_avatar_image(
|
|
|
|
self,
|
|
|
|
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,
|
2024-06-25 21:03:49 +02:00
|
|
|
) -> None:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-06-25 21:03:49 +02:00
|
|
|
def delete_avatar_image(self, path_id: str) -> None:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-02-28 04:33:25 +01:00
|
|
|
# Realm icon and logo uploads
|
|
|
|
def realm_avatar_and_logo_path(self, realm: Realm) -> str:
|
|
|
|
return os.path.join(str(realm.id), "realm")
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-02-28 04:33:25 +01:00
|
|
|
def get_realm_icon_url(self, realm_id: int, version: int) -> str:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
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:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-02-28 04:33:25 +01:00
|
|
|
def get_realm_logo_url(self, realm_id: int, version: int, night: bool) -> str:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
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:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-02-28 04:33:25 +01:00
|
|
|
# Realm emoji uploads
|
|
|
|
def get_emoji_url(self, emoji_file_name: str, realm_id: int, still: bool = False) -> str:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-06-24 22:25:16 +02:00
|
|
|
def upload_single_emoji_image(
|
|
|
|
self,
|
|
|
|
path: str,
|
2024-07-12 02:30:23 +02:00
|
|
|
content_type: str | None,
|
2024-06-24 22:25:16 +02:00
|
|
|
user_profile: UserProfile,
|
|
|
|
image_data: bytes,
|
|
|
|
) -> None:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2023-02-28 04:33:25 +01:00
|
|
|
# Export tarballs
|
|
|
|
def get_export_tarball_url(self, realm: Realm, export_path: str) -> str:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
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:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|
2022-12-14 21:51:37 +01:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def delete_export_tarball(self, export_path: str) -> str | None:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|