upload: Generate thumbnails when images are uploaded.

A new table is created to track which path_id attachments are images,
and for those their metadata, and which thumbnails have been created.
Using path_id as the effective primary key lets us ignore if the
attachment is archived or not, saving some foreign key messes.

A new worker is added to observe events when rows are added to this
table, and to generate and store thumbnails for those images in
differing sizes and formats.
This commit is contained in:
Alex Vandiver
2024-06-20 21:58:27 +00:00
committed by Tim Abbott
parent 7aa5bb233d
commit 2e38f426f4
21 changed files with 788 additions and 50 deletions

View File

@@ -91,7 +91,7 @@ class LocalUploadBackend(ZulipUploadBackend):
path_id: str,
content_type: str,
file_data: bytes,
user_profile: UserProfile,
user_profile: UserProfile | None,
) -> None:
write_local_file("files", path_id, file_data)
@@ -104,9 +104,14 @@ class LocalUploadBackend(ZulipUploadBackend):
return delete_local_file("files", path_id)
@override
def all_message_attachments(self) -> Iterator[tuple[str, datetime]]:
def all_message_attachments(
self, include_thumbnails: bool = False
) -> Iterator[tuple[str, datetime]]:
assert settings.LOCAL_UPLOADS_DIR is not None
for dirname, _, files in os.walk(settings.LOCAL_UPLOADS_DIR + "/files"):
top = settings.LOCAL_UPLOADS_DIR + "/files"
for dirname, subdirnames, files in os.walk(top):
if not include_thumbnails and dirname == top and "thumbnail" in subdirnames:
subdirnames.remove("thumbnail")
for f in files:
fullpath = os.path.join(dirname, f)
yield (