mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-29 10:53:44 +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):
|
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)
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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,12 +11,15 @@
|
||||||
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]
|
||||||
|
try:
|
||||||
pth = create_preview(file.file.path)
|
pth = create_preview(file.file.path)
|
||||||
if pth:
|
if pth:
|
||||||
with open(pth, "rb") as f:
|
with open(pth, "rb") as f:
|
||||||
|
@ -24,6 +28,8 @@ def process_file(pk: int):
|
||||||
File(f),
|
File(f),
|
||||||
save=False,
|
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"])
|
||||||
|
|
|
@ -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 %}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user