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):
return f"file: {self.name}"
class Meta:
ordering = ["modified"]
class FileInTrash(TimeStampedModel):
user = ForeignKey("users.User", related_name="trash_files", on_delete=CASCADE)

View File

@ -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:
if manager.has_jpeg_preview(file_path):
return manager.get_jpeg_preview(file_path, height=500)
return ""
return path_to_preview_image
def get_file_mimetype(file_path: str) -> str:

View File

@ -1,5 +1,6 @@
import os
import structlog
from celery import shared_task
from django.core.files import File
@ -10,12 +11,15 @@
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]
try:
pth = create_preview(file.file.path)
if pth:
with open(pth, "rb") as f:
@ -24,6 +28,8 @@ def process_file(pk: int):
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"])

View File

@ -4,7 +4,20 @@
{% for folder in folders %}
{{ folder.name }}
{% endfor %}
<div class="row">
{% for file in file_list %}
{{ file.name }} - {{ file.file.url }}
<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 %}