mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-12-01 14:23:44 +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):
|
class File(TimeStampedModel, ShortLink):
|
||||||
"""model to store user's files"""
|
"""model to store user's files"""
|
||||||
|
|
||||||
name = CharField(max_length=100)
|
|
||||||
description = TextField(blank=True)
|
|
||||||
|
|
||||||
private = BooleanField(default=True)
|
private = BooleanField(default=True)
|
||||||
|
|
||||||
user = ForeignKey("users.User", related_name="files", on_delete=CASCADE)
|
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/")
|
preview = FileField(blank=True, upload_to="file/previews/")
|
||||||
file = FileField(blank=False, upload_to=user_file_upload_mixin)
|
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):
|
def get_absolute_url(self):
|
||||||
return reverse("files:view", kwargs={"slug": self.slug})
|
return reverse("files:view", kwargs={"slug": self.slug})
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
|
||||||
|
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"
|
||||||
|
@ -13,6 +15,8 @@
|
||||||
"Consola.ttf",
|
"Consola.ttf",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
manager = PreviewManager(cache_path, create_folder=True)
|
||||||
|
|
||||||
|
|
||||||
def textfile_to_image(textfile_path) -> Image:
|
def textfile_to_image(textfile_path) -> Image:
|
||||||
"""Convert text file to a grayscale 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:
|
def create_preview(file_path: str) -> str:
|
||||||
# TODO: add text image generation/code image
|
# TODO: add text image generation/code image
|
||||||
manager = PreviewManager(cache_path, create_folder=True)
|
try:
|
||||||
path_to_preview_image = manager.get_pdf_preview(file_path)
|
path_to_preview_image = manager.get_jpeg_preview(file_path)
|
||||||
|
except UnsupportedMimeType:
|
||||||
|
return ""
|
||||||
return path_to_preview_image
|
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 django.core.files import File
|
||||||
|
|
||||||
from akarpov.files.models import File as FileModel
|
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()
|
@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
|
file.name = file.file.name.split("/")[-1]
|
||||||
pth = create_preview(file.file)
|
pth = create_preview(file.file.path)
|
||||||
with open(pth, "rb") as f:
|
if pth:
|
||||||
file.preview.save(
|
with open(pth, "rb") as f:
|
||||||
pth.split("/")[-1],
|
file.preview.save(
|
||||||
File(f),
|
pth.split("/")[-1],
|
||||||
save=False,
|
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)
|
os.remove(pth)
|
||||||
return pk
|
return pk
|
||||||
|
|
|
@ -46,7 +46,7 @@ class FileFolderView(DetailView):
|
||||||
folder_view = FileFolderView.as_view()
|
folder_view = FileFolderView.as_view()
|
||||||
|
|
||||||
|
|
||||||
class ChunkedUploadDemo(TemplateView):
|
class ChunkedUploadDemo(LoginRequiredMixin, TemplateView):
|
||||||
template_name = "files/upload.html"
|
template_name = "files/upload.html"
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,7 +76,9 @@ def check_permissions(self, request):
|
||||||
|
|
||||||
def on_completion(self, uploaded_file, request):
|
def on_completion(self, uploaded_file, request):
|
||||||
if uploaded_file.size <= request.user.left_file_upload:
|
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.left_file_upload -= uploaded_file.size
|
||||||
request.user.save()
|
request.user.save()
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user