mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-28 23:53:42 +03:00
added file processing
This commit is contained in:
parent
d6d933733b
commit
f506802b28
|
@ -0,0 +1,28 @@
|
|||
# Generated by Django 4.2 on 2023-04-07 10:40
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("files", "0010_fileintrash"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="file",
|
||||
name="file_type",
|
||||
field=models.CharField(blank=True, max_length=50, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="file",
|
||||
name="description",
|
||||
field=models.TextField(blank=True, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="file",
|
||||
name="name",
|
||||
field=models.CharField(blank=True, max_length=100, null=True),
|
||||
),
|
||||
]
|
|
@ -17,9 +17,6 @@
|
|||
class File(TimeStampedModel, ShortLink):
|
||||
"""model to store user's files"""
|
||||
|
||||
name = CharField(max_length=100)
|
||||
description = TextField(blank=True)
|
||||
|
||||
private = BooleanField(default=True)
|
||||
|
||||
user = ForeignKey("users.User", related_name="files", on_delete=CASCADE)
|
||||
|
@ -30,6 +27,11 @@ class File(TimeStampedModel, ShortLink):
|
|||
preview = FileField(blank=True, upload_to="file/previews/")
|
||||
file = FileField(blank=False, upload_to=user_file_upload_mixin)
|
||||
|
||||
# meta
|
||||
name = CharField(max_length=100, null=True, blank=True)
|
||||
description = TextField(blank=True, null=True)
|
||||
file_type = CharField(max_length=50, null=True, blank=True)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse("files:view", kwargs={"slug": self.slug})
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
from math import ceil
|
||||
|
||||
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"
|
||||
|
@ -13,6 +15,8 @@
|
|||
"Consola.ttf",
|
||||
]
|
||||
|
||||
manager = PreviewManager(cache_path, create_folder=True)
|
||||
|
||||
|
||||
def textfile_to_image(textfile_path) -> Image:
|
||||
"""Convert text file to a grayscale image.
|
||||
|
@ -75,6 +79,18 @@ def _font_points_to_pixels(pt):
|
|||
|
||||
def create_preview(file_path: str) -> str:
|
||||
# TODO: add text image generation/code image
|
||||
manager = PreviewManager(cache_path, create_folder=True)
|
||||
path_to_preview_image = manager.get_pdf_preview(file_path)
|
||||
try:
|
||||
path_to_preview_image = manager.get_jpeg_preview(file_path)
|
||||
except UnsupportedMimeType:
|
||||
return ""
|
||||
return path_to_preview_image
|
||||
|
||||
|
||||
def get_file_mimetype(file_path: str) -> str:
|
||||
return magic.from_file(file_path)
|
||||
|
||||
|
||||
def get_description(file_path: str) -> str:
|
||||
if manager.has_text_preview(file_path):
|
||||
return manager.get_text_preview(file_path)
|
||||
return ""
|
||||
|
|
|
@ -4,22 +4,29 @@
|
|||
from django.core.files import File
|
||||
|
||||
from akarpov.files.models import File as FileModel
|
||||
from akarpov.files.services.preview import create_preview
|
||||
from akarpov.files.services.preview import (
|
||||
create_preview,
|
||||
get_description,
|
||||
get_file_mimetype,
|
||||
)
|
||||
|
||||
|
||||
@shared_task()
|
||||
def process_file(pk: int):
|
||||
file = FileModel.objects.get(pk=pk)
|
||||
if not file.name:
|
||||
file.name = file.file.name
|
||||
pth = create_preview(file.file)
|
||||
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,
|
||||
)
|
||||
file.save(update_fields=["preview"])
|
||||
if os.path.isfile(pth):
|
||||
file.type = get_file_mimetype(file.file.path)
|
||||
file.description = get_description(file.file.path)
|
||||
file.save(update_fields=["preview", "name", "file_type", "description"])
|
||||
if pth and os.path.isfile(pth):
|
||||
os.remove(pth)
|
||||
return pk
|
||||
|
|
|
@ -46,7 +46,7 @@ class FileFolderView(DetailView):
|
|||
folder_view = FileFolderView.as_view()
|
||||
|
||||
|
||||
class ChunkedUploadDemo(TemplateView):
|
||||
class ChunkedUploadDemo(LoginRequiredMixin, TemplateView):
|
||||
template_name = "files/upload.html"
|
||||
|
||||
|
||||
|
@ -76,7 +76,9 @@ def check_permissions(self, request):
|
|||
|
||||
def on_completion(self, uploaded_file, request):
|
||||
if uploaded_file.size <= request.user.left_file_upload:
|
||||
File.objects.create(user=request.user, file=uploaded_file)
|
||||
File.objects.create(
|
||||
user=request.user, file=uploaded_file, name=uploaded_file.name
|
||||
)
|
||||
request.user.left_file_upload -= uploaded_file.size
|
||||
request.user.save()
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue
Block a user