snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
This commit is contained in:
107
zerver/management/commands/thumbnail.py
Normal file
107
zerver/management/commands/thumbnail.py
Normal file
@@ -0,0 +1,107 @@
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
from django.core.management.base import CommandParser
|
||||
from django.db.models import Exists, OuterRef
|
||||
from django.utils.timezone import now as timezone_now
|
||||
from typing_extensions import override
|
||||
|
||||
from zerver.actions.message_edit import re_thumbnail
|
||||
from zerver.lib.management import ZulipBaseCommand
|
||||
from zerver.lib.queue import queue_event_on_commit
|
||||
from zerver.lib.thumbnail import StoredThumbnailFormat, get_image_thumbnail_path
|
||||
from zerver.lib.upload import all_message_attachments
|
||||
from zerver.models import ArchivedMessage, Attachment, ImageAttachment, Message
|
||||
|
||||
|
||||
class Command(ZulipBaseCommand):
|
||||
help = """Manages thumbnailing in messages."""
|
||||
|
||||
@override
|
||||
def add_arguments(self, parser: CommandParser) -> None:
|
||||
self.add_realm_args(parser, required=True)
|
||||
mode = parser.add_mutually_exclusive_group(required=True)
|
||||
mode.add_argument(
|
||||
"--stuck-spinners",
|
||||
action="store_true",
|
||||
help="Attempt to re-render messages with stuck spinners",
|
||||
)
|
||||
mode.add_argument(
|
||||
"--old-images",
|
||||
action="store_true",
|
||||
help="Generate thumbnails of old images",
|
||||
)
|
||||
mode.add_argument(
|
||||
"--missing-files",
|
||||
action="store_true",
|
||||
help="Regenerate thumbnails when their files are missing",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--cutoff",
|
||||
help="Only process messages sent less than this many days ago",
|
||||
type=int,
|
||||
default=100,
|
||||
)
|
||||
|
||||
@override
|
||||
def handle(self, *args: Any, **options: Any) -> None:
|
||||
realm = self.get_realm(options)
|
||||
assert realm is not None
|
||||
|
||||
if options.get("missing_files"):
|
||||
realm_imageattachments = (
|
||||
ImageAttachment.objects.alias(
|
||||
in_realm=Exists(
|
||||
Attachment.objects.filter(path_id=OuterRef("path_id"), realm_id=realm.id)
|
||||
)
|
||||
)
|
||||
.filter(in_realm=True)
|
||||
.exclude(thumbnail_metadata=[])
|
||||
)
|
||||
for image_attachment in realm_imageattachments:
|
||||
found = []
|
||||
changed = False
|
||||
thumb_dir = f"thumbnail/{image_attachment.path_id}"
|
||||
found_thumbs = [
|
||||
e[0] for e in all_message_attachments(include_thumbnails=True, prefix=thumb_dir)
|
||||
]
|
||||
for existing_thumbnail in image_attachment.thumbnail_metadata:
|
||||
thumb = StoredThumbnailFormat(**existing_thumbnail)
|
||||
if get_image_thumbnail_path(image_attachment, thumb) in found_thumbs:
|
||||
found.append(existing_thumbnail)
|
||||
else:
|
||||
changed = True
|
||||
if changed:
|
||||
image_attachment.thumbnail_metadata = found
|
||||
image_attachment.save(update_fields=["thumbnail_metadata"])
|
||||
queue_event_on_commit(
|
||||
"thumbnail",
|
||||
{"id": image_attachment.id, "path_id": image_attachment.path_id},
|
||||
)
|
||||
return
|
||||
|
||||
for message_class in (Message, ArchivedMessage):
|
||||
messages = message_class.objects.filter(
|
||||
realm_id=realm.id,
|
||||
has_image=True,
|
||||
date_sent__gt=timezone_now() - timedelta(days=options["cutoff"]),
|
||||
)
|
||||
if options.get("stuck_spinners"):
|
||||
messages = messages.filter(
|
||||
rendered_content__contains='class="image-loading-placeholder"',
|
||||
date_sent__lt=timezone_now() - timedelta(seconds=60),
|
||||
)
|
||||
elif options.get("old_images"):
|
||||
messages = messages.filter(
|
||||
rendered_content__contains='<img src="/user_uploads/',
|
||||
).exclude(
|
||||
rendered_content__contains='<img src="/user_uploads/thumbnail/',
|
||||
)
|
||||
|
||||
message_ids = list(messages.values_list("id", flat=True))
|
||||
print(f"Processing {len(message_ids)} {message_class.__name__} objects")
|
||||
for i, message_id in enumerate(message_ids):
|
||||
re_thumbnail(message_class, message_id, enqueue=options["old_images"])
|
||||
|
||||
if (i + 1) % 100 == 0:
|
||||
print(f"Processed {i + 1}/{len(message_ids)} {message_class.__name__} objects")
|
||||
Reference in New Issue
Block a user