diff --git a/akarpov/files/models.py b/akarpov/files/models.py index a2c8499..376659e 100644 --- a/akarpov/files/models.py +++ b/akarpov/files/models.py @@ -38,6 +38,9 @@ def get_absolute_url(self): def __str__(self): return f"file: {self.name}" + class Meta: + ordering = ["modified"] + class FileInTrash(TimeStampedModel): user = ForeignKey("users.User", related_name="trash_files", on_delete=CASCADE) diff --git a/akarpov/files/services/preview.py b/akarpov/files/services/preview.py index b6fbd91..5c3551b 100644 --- a/akarpov/files/services/preview.py +++ b/akarpov/files/services/preview.py @@ -2,7 +2,6 @@ import magic from PIL import Image, ImageDraw, ImageFont -from preview_generator.exception import UnsupportedMimeType from preview_generator.manager import PreviewManager cache_path = "/tmp/preview_cache" @@ -79,11 +78,9 @@ def _font_points_to_pixels(pt): def create_preview(file_path: str) -> str: # TODO: add text image generation/code image - try: - path_to_preview_image = manager.get_jpeg_preview(file_path) - except UnsupportedMimeType: - return "" - return path_to_preview_image + if manager.has_jpeg_preview(file_path): + return manager.get_jpeg_preview(file_path, height=500) + return "" def get_file_mimetype(file_path: str) -> str: diff --git a/akarpov/files/tasks.py b/akarpov/files/tasks.py index d660601..b1e5332 100644 --- a/akarpov/files/tasks.py +++ b/akarpov/files/tasks.py @@ -1,5 +1,6 @@ import os +import structlog from celery import shared_task from django.core.files import File @@ -10,20 +11,25 @@ get_file_mimetype, ) +logger = structlog.get_logger(__name__) + @shared_task() def process_file(pk: int): file = FileModel.objects.get(pk=pk) if not file.name: file.name = file.file.name.split("/")[-1] - pth = create_preview(file.file.path) - if pth: - with open(pth, "rb") as f: - file.preview.save( - pth.split("/")[-1], - File(f), - save=False, - ) + try: + pth = create_preview(file.file.path) + if pth: + with open(pth, "rb") as f: + file.preview.save( + pth.split("/")[-1], + File(f), + save=False, + ) + except Exception as e: + logger.error(e) file.type = get_file_mimetype(file.file.path) file.description = get_description(file.file.path) file.save(update_fields=["preview", "name", "file_type", "description"]) diff --git a/akarpov/templates/files/list.html b/akarpov/templates/files/list.html index 107e934..ca36465 100644 --- a/akarpov/templates/files/list.html +++ b/akarpov/templates/files/list.html @@ -4,7 +4,20 @@ {% for folder in folders %} {{ folder.name }} {% endfor %} -{% for file in file_list %} - {{ file.name }} - {{ file.file.url }} -{% endfor %} +
+ {% for file in file_list %} +
+
+
+
{{ file.name }}
+

{{ file.description }}

+

Last updated 3 mins ago

+
+ {% if file.preview %} + + {% endif %} +
+
+ {% endfor %} +
{% endblock %}