mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-22 19:06:41 +03:00
35 lines
939 B
Python
35 lines
939 B
Python
import os
|
|
|
|
import structlog
|
|
from celery import shared_task
|
|
from django.core.files import File
|
|
|
|
from akarpov.files.models import File as FileModel
|
|
from akarpov.files.services.preview import create_preview, get_file_mimetype
|
|
|
|
logger = structlog.get_logger(__name__)
|
|
|
|
|
|
@shared_task()
|
|
def process_file(pk: int):
|
|
pth = None
|
|
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:
|
|
file.preview.save(
|
|
pth.split("/")[-1],
|
|
File(f),
|
|
save=False,
|
|
)
|
|
except Exception as e:
|
|
logger.error(e)
|
|
file.file_type = get_file_mimetype(file.file.path)
|
|
file.save(update_fields=["preview", "name", "file_type"])
|
|
if pth and os.path.isfile(pth):
|
|
os.remove(pth)
|
|
return pk
|