fixed file preview handle

This commit is contained in:
Alexander Karpov 2023-04-07 23:10:03 +03:00
parent 43187854ac
commit 0b3bb1b6fd
4 changed files with 36 additions and 17 deletions

View File

@ -38,6 +38,9 @@ def get_absolute_url(self):
def __str__(self): def __str__(self):
return f"file: {self.name}" return f"file: {self.name}"
class Meta:
ordering = ["modified"]
class FileInTrash(TimeStampedModel): class FileInTrash(TimeStampedModel):
user = ForeignKey("users.User", related_name="trash_files", on_delete=CASCADE) user = ForeignKey("users.User", related_name="trash_files", on_delete=CASCADE)

View File

@ -2,7 +2,6 @@
import magic import magic
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
from preview_generator.exception import UnsupportedMimeType
from preview_generator.manager import PreviewManager from preview_generator.manager import PreviewManager
cache_path = "/tmp/preview_cache" cache_path = "/tmp/preview_cache"
@ -79,11 +78,9 @@ def _font_points_to_pixels(pt):
def create_preview(file_path: str) -> str: def create_preview(file_path: str) -> str:
# TODO: add text image generation/code image # TODO: add text image generation/code image
try: if manager.has_jpeg_preview(file_path):
path_to_preview_image = manager.get_jpeg_preview(file_path) return manager.get_jpeg_preview(file_path, height=500)
except UnsupportedMimeType: return ""
return ""
return path_to_preview_image
def get_file_mimetype(file_path: str) -> str: def get_file_mimetype(file_path: str) -> str:

View File

@ -1,5 +1,6 @@
import os import os
import structlog
from celery import shared_task from celery import shared_task
from django.core.files import File from django.core.files import File
@ -10,20 +11,25 @@
get_file_mimetype, get_file_mimetype,
) )
logger = structlog.get_logger(__name__)
@shared_task() @shared_task()
def process_file(pk: int): def process_file(pk: int):
file = FileModel.objects.get(pk=pk) file = FileModel.objects.get(pk=pk)
if not file.name: if not file.name:
file.name = file.file.name.split("/")[-1] file.name = file.file.name.split("/")[-1]
pth = create_preview(file.file.path) try:
if pth: pth = create_preview(file.file.path)
with open(pth, "rb") as f: if pth:
file.preview.save( with open(pth, "rb") as f:
pth.split("/")[-1], file.preview.save(
File(f), pth.split("/")[-1],
save=False, File(f),
) save=False,
)
except Exception as e:
logger.error(e)
file.type = get_file_mimetype(file.file.path) file.type = get_file_mimetype(file.file.path)
file.description = get_description(file.file.path) file.description = get_description(file.file.path)
file.save(update_fields=["preview", "name", "file_type", "description"]) file.save(update_fields=["preview", "name", "file_type", "description"])

View File

@ -4,7 +4,20 @@
{% for folder in folders %} {% for folder in folders %}
{{ folder.name }} {{ folder.name }}
{% endfor %} {% endfor %}
{% for file in file_list %} <div class="row">
{{ file.name }} - {{ file.file.url }} {% for file in file_list %}
{% endfor %} <div class="col-sm-2">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ file.name }}</h5>
<p class="card-text">{{ file.description }}</p>
<p class="card-text"><small class="text-body-secondary">Last updated 3 mins ago</small></p>
</div>
{% if file.preview %}
<img src="{{ file.preview.url }}" class="card-img-bottom" alt="">
{% endif %}
</div>
</div>
{% endfor %}
</div>
{% endblock %} {% endblock %}