From ff272f868eb7c2e64faf6c71284916f0c2050a5b Mon Sep 17 00:00:00 2001 From: Alexandr Karpov Date: Tue, 11 Apr 2023 14:48:39 +0300 Subject: [PATCH] added trash files, preview dependencies for docker --- .../migrations/0014_alter_fileintrash_file.py | 21 +++++++++++++++ .../files/migrations/0015_fileintrash_name.py | 18 +++++++++++++ akarpov/files/models.py | 5 ++-- akarpov/files/services/files.py | 6 +++++ akarpov/files/signals.py | 27 ++++++++++++++++--- akarpov/templates/files/list.html | 3 --- compose/local/django/Dockerfile | 4 ++- .../local/django/install_preview_dependencies | 10 +++++++ 8 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 akarpov/files/migrations/0014_alter_fileintrash_file.py create mode 100644 akarpov/files/migrations/0015_fileintrash_name.py create mode 100644 compose/local/django/install_preview_dependencies diff --git a/akarpov/files/migrations/0014_alter_fileintrash_file.py b/akarpov/files/migrations/0014_alter_fileintrash_file.py new file mode 100644 index 0000000..dac2abc --- /dev/null +++ b/akarpov/files/migrations/0014_alter_fileintrash_file.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2 on 2023-04-11 10:55 + +import akarpov.files.services.files +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("files", "0013_alter_file_options"), + ] + + operations = [ + migrations.AlterField( + model_name="fileintrash", + name="file", + field=models.FileField( + upload_to=akarpov.files.services.files.trash_file_upload + ), + ), + ] diff --git a/akarpov/files/migrations/0015_fileintrash_name.py b/akarpov/files/migrations/0015_fileintrash_name.py new file mode 100644 index 0000000..65cc41b --- /dev/null +++ b/akarpov/files/migrations/0015_fileintrash_name.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2 on 2023-04-11 11:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("files", "0014_alter_fileintrash_file"), + ] + + operations = [ + migrations.AddField( + model_name="fileintrash", + name="name", + field=models.CharField(blank=True, max_length=200), + ), + ] diff --git a/akarpov/files/models.py b/akarpov/files/models.py index bbe50fc..4618f7e 100644 --- a/akarpov/files/models.py +++ b/akarpov/files/models.py @@ -13,7 +13,7 @@ from django.urls import reverse from model_utils.models import TimeStampedModel -from akarpov.files.services.files import user_unique_file_upload +from akarpov.files.services.files import trash_file_upload, user_unique_file_upload from akarpov.tools.shortener.models import ShortLink @@ -62,8 +62,9 @@ class Meta: class FileInTrash(TimeStampedModel): + name = CharField(max_length=200, blank=True) user = ForeignKey("users.User", related_name="trash_files", on_delete=CASCADE) - file = FileField(blank=False, upload_to="file/trash/") + file = FileField(blank=False, upload_to=trash_file_upload) class Folder(TimeStampedModel, ShortLink): diff --git a/akarpov/files/services/files.py b/akarpov/files/services/files.py index cfdd9f9..b814774 100644 --- a/akarpov/files/services/files.py +++ b/akarpov/files/services/files.py @@ -14,3 +14,9 @@ def user_unique_file_upload(instance, filename): slug = generate_charset(5) return os.path.join(f"files/{username}/{slug}", filename) + + +def trash_file_upload(instance, filename): + """stores user trash files""" + username = instance.user.username + return os.path.join(f"file/trash/{username}", filename) diff --git a/akarpov/files/signals.py b/akarpov/files/signals.py index bd17aca..e2dcdb5 100644 --- a/akarpov/files/signals.py +++ b/akarpov/files/signals.py @@ -1,12 +1,16 @@ -from django.db.models.signals import post_save +import os + +from django.core.files.base import File +from django.db.models.signals import post_delete, post_save from django.dispatch import receiver -from akarpov.files.models import File +from akarpov.files.models import File as FileModel +from akarpov.files.models import FileInTrash from akarpov.files.tasks import process_file -@receiver(post_save, sender=File) -def post_on_create(sender, instance: File, created, **kwargs): +@receiver(post_save, sender=FileModel) +def post_on_create(sender, instance: FileModel, created, **kwargs): if created: process_file.apply_async( kwargs={ @@ -14,3 +18,18 @@ def post_on_create(sender, instance: File, created, **kwargs): }, countdown=2, ) + + +@receiver(post_delete, sender=FileModel) +def move_file_to_trash(sender, instance, **kwargs): + if instance.file: + name = instance.file.name.split("/")[-1] + trash = FileInTrash(user=instance.user, name=name) + trash.file = File(instance.file, name=name) + trash.save() + path = instance.file.path + file_dir = "/".join(path.split("/")[:-1]) + "/" + if os.path.isfile(path): + os.remove(path) + if os.path.isdir(file_dir) and len(os.listdir(file_dir)) == 0: + os.rmdir(file_dir) diff --git a/akarpov/templates/files/list.html b/akarpov/templates/files/list.html index b4493fb..ec96413 100644 --- a/akarpov/templates/files/list.html +++ b/akarpov/templates/files/list.html @@ -152,11 +152,8 @@ dataType: "json", maxChunkSize: 1000000, // Chunks of 1000 kB formData: form_data, - prependFiles: true, dropZone: body, pasteZone: body, - sequentialUploads: true, - autoUpload: false, add: function(e, data) { // Called before starting upload if (cur !== true){ cur = true diff --git a/compose/local/django/Dockerfile b/compose/local/django/Dockerfile index b361736..81e7c46 100644 --- a/compose/local/django/Dockerfile +++ b/compose/local/django/Dockerfile @@ -27,7 +27,6 @@ RUN apt-get update && \ apt-get install -y build-essential libpq-dev gettext libmagic-dev libjpeg-dev zlib1g-dev && \ # Dependencies for file preview generation apt-get install -y webp libimage-exiftool-perl libmagickwand-dev ffmpeg && \ - # apt-get install -y poppler-utils libfile-mimeinfo-perl ghostscript libsecret-1-0 zlib1g-dev libjpeg-dev imagemagick libmagic1 libreoffice inkscape xvfb && \ apt-get purge -y --auto-remove -o APT:AutoRemove:RecommendsImportant=false && \ rm -rf /var/lib/apt/lists/* @@ -51,6 +50,9 @@ COPY ./compose/local/django/start /start RUN sed -i 's/\r$//g' /start RUN chmod +x /start +COPY ./compose/local/django/install_preview_dependencies /install_preview_dependencies +RUN sed -i 's/\r$//g' /install_preview_dependencies +RUN chmod +x /install_preview_dependencies COPY ./compose/local/django/celery/worker/start /start-celeryworker RUN sed -i 's/\r$//g' /start-celeryworker diff --git a/compose/local/django/install_preview_dependencies b/compose/local/django/install_preview_dependencies new file mode 100644 index 0000000..1f60066 --- /dev/null +++ b/compose/local/django/install_preview_dependencies @@ -0,0 +1,10 @@ +#!/bin/bash + +apt-get update +apt-get install -y poppler-utils libfile-mimeinfo-perl ghostscript libsecret-1-0 zlib1g-dev libjpeg-dev imagemagick libmagic1 libreoffice inkscape xvfb +wget https://github.com/jgraph/drawio-desktop/releases/download/v13.0.3/draw.io-amd64-13.0.3.deb +dpkg -i draw.io-amd64-13.0.3.deb +rm draw.io-amd64-13.0.3.deb +apt-get purge -y --auto-remove -o APT:AutoRemove:RecommendsImportant=false && \ +rm -rf /var/lib/apt/lists/* +preview --check-dependencies``