mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-22 07:26:33 +03:00
fixed file preview handle
This commit is contained in:
parent
43187854ac
commit
0b3bb1b6fd
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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"])
|
||||
|
|
|
@ -4,7 +4,20 @@
|
|||
{% for folder in folders %}
|
||||
{{ folder.name }}
|
||||
{% endfor %}
|
||||
{% for file in file_list %}
|
||||
{{ file.name }} - {{ file.file.url }}
|
||||
{% endfor %}
|
||||
<div class="row">
|
||||
{% for file in file_list %}
|
||||
<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 %}
|
||||
|
|
Loading…
Reference in New Issue
Block a user