From 8a995a8ca5840fbb2a9731f6e2cd0dea1ae93f1b Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Tue, 11 Apr 2023 22:10:54 +0300 Subject: [PATCH] added file previews, file page --- akarpov/files/previews/__init__.py | 3 ++ akarpov/files/previews/image/__init__.py | 1 + akarpov/files/previews/image/jpeg.py | 35 ++++++++++++++++++++++++ akarpov/files/previews/video/__init__.py | 1 + akarpov/files/previews/video/mp4.py | 20 ++++++++++++++ akarpov/files/services/preview.py | 3 +- akarpov/files/tasks.py | 2 +- akarpov/files/views.py | 16 +++++++++++ akarpov/templates/base.html | 2 +- akarpov/templates/files/list.html | 35 +++++++++++++++++++++--- akarpov/templates/files/view.html | 33 ++++++++++++++++++++++ 11 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 akarpov/files/previews/__init__.py create mode 100644 akarpov/files/previews/image/__init__.py create mode 100644 akarpov/files/previews/image/jpeg.py create mode 100644 akarpov/files/previews/video/__init__.py create mode 100644 akarpov/files/previews/video/mp4.py create mode 100644 akarpov/templates/files/view.html diff --git a/akarpov/files/previews/__init__.py b/akarpov/files/previews/__init__.py new file mode 100644 index 0000000..df2941f --- /dev/null +++ b/akarpov/files/previews/__init__.py @@ -0,0 +1,3 @@ +from . import image, video + +previews = {"video": {"mp4": video.mp4.view}, "image": {"jpeg": image.jpeg.view}} diff --git a/akarpov/files/previews/image/__init__.py b/akarpov/files/previews/image/__init__.py new file mode 100644 index 0000000..ae2d45a --- /dev/null +++ b/akarpov/files/previews/image/__init__.py @@ -0,0 +1 @@ +from . import jpeg # noqa diff --git a/akarpov/files/previews/image/jpeg.py b/akarpov/files/previews/image/jpeg.py new file mode 100644 index 0000000..bb7aa50 --- /dev/null +++ b/akarpov/files/previews/image/jpeg.py @@ -0,0 +1,35 @@ +from akarpov.files.models import File + + +def view(file: File) -> (str, str): + static = """ + + """ + content = ( + f""" +
+ Picture +
+ + """ + + """ + + """ + ) + + return static, content diff --git a/akarpov/files/previews/video/__init__.py b/akarpov/files/previews/video/__init__.py new file mode 100644 index 0000000..6c53191 --- /dev/null +++ b/akarpov/files/previews/video/__init__.py @@ -0,0 +1 @@ +from . import mp4 # noqa diff --git a/akarpov/files/previews/video/mp4.py b/akarpov/files/previews/video/mp4.py new file mode 100644 index 0000000..93864ee --- /dev/null +++ b/akarpov/files/previews/video/mp4.py @@ -0,0 +1,20 @@ +from akarpov.files.models import File + + +def view(file: File) -> (str, str): + static = """ + + """ + data = """"playbackRates": [0.5, 1, 1.5, 2], "responsive": true }""" + content = f""" + + + + + """ + + return static, content diff --git a/akarpov/files/services/preview.py b/akarpov/files/services/preview.py index 5c3551b..af61183 100644 --- a/akarpov/files/services/preview.py +++ b/akarpov/files/services/preview.py @@ -84,7 +84,8 @@ def create_preview(file_path: str) -> str: def get_file_mimetype(file_path: str) -> str: - return magic.from_file(file_path) + mime = magic.Magic(mime=True) + return mime.from_file(file_path) def get_description(file_path: str) -> str: diff --git a/akarpov/files/tasks.py b/akarpov/files/tasks.py index da22eef..561e071 100644 --- a/akarpov/files/tasks.py +++ b/akarpov/files/tasks.py @@ -31,7 +31,7 @@ def process_file(pk: int): ) except Exception as e: logger.error(e) - file.type = get_file_mimetype(file.file.path) + file.file_type = get_file_mimetype(file.file.path) descr = None try: descr = get_description(file.file.path) diff --git a/akarpov/files/views.py b/akarpov/files/views.py index d4a71f8..973ea99 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -11,10 +11,12 @@ ChunkedUploadView, ) from akarpov.files.models import File, Folder +from akarpov.files.previews import previews class TopFolderView(LoginRequiredMixin, ListView): template_name = "files/list.html" + paginate_by = 20 model = File def get_queryset(self): @@ -33,6 +35,20 @@ class FileView(DetailView): model = File slug_field = "slug" + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["has_perm"] = self.object.user == self.request.user + static = "" + content = "" + if self.object.file_type: + t1, t2 = self.object.file_type.split("/") + if t1 in previews: + if t2 in previews[t1]: + static, content = previews[t1][t2](self.object) + context["preview_static"] = static + context["preview_content"] = content + return context + files_view = FileView.as_view() diff --git a/akarpov/templates/base.html b/akarpov/templates/base.html index 0d505aa..1826e4e 100644 --- a/akarpov/templates/base.html +++ b/akarpov/templates/base.html @@ -51,7 +51,7 @@ {% if request.user.is_authenticated %}
  • - + Files
  • {% endif %} diff --git a/akarpov/templates/files/list.html b/akarpov/templates/files/list.html index ec96413..e7b0683 100644 --- a/akarpov/templates/files/list.html +++ b/akarpov/templates/files/list.html @@ -57,8 +57,8 @@ +

    @@ -72,11 +72,36 @@
    + +

    {{ file.modified | naturaltime }}

    {% endfor %} +{% if page_obj.has_other_pages %} +
    + {% if page_obj.has_previous %} + « + {% endif %} + + {% for page_number in page_obj.paginator.page_range %} + {% if page_obj.number == page_number %} + + {% else %} + + {{ page_number }} + + {% endif %} + {% endfor %} + + {% if page_obj.has_next %} + » + {% endif %} +
    +{% endif %} {% endblock %} {% block inline_javascript %} @@ -157,15 +182,14 @@ add: function(e, data) { // Called before starting upload if (cur !== true){ cur = true - console.log("add", data.files[0]) form_data.splice(1); calculate_md5(data.files[0], 1000000); data.submit(); $("#progress").css("display", "flex"); + $("#progress-message").text("loading file: " + data.files[0].name); } }, chunkdone: function (e, data) { // Called after uploading each chunk - console.log("chunkdone", data.files[0]) if (form_data.length < 2) { form_data.push( {"name": "upload_id", "value": data.result.upload_id} @@ -177,7 +201,10 @@ sel.text(progress + "%"); }, done: function (e, data) { // Called when the file has completely uploaded - console.log("done", data.files[0]) + var progress = 100; + let sel = $("#progress-bar"); + sel.css("width", progress + "%"); + sel.text(progress + "%"); $.ajax({ type: "POST", url: "{% url 'files:api_chunked_upload_complete' %}", diff --git a/akarpov/templates/files/view.html b/akarpov/templates/files/view.html new file mode 100644 index 0000000..3d8b2a2 --- /dev/null +++ b/akarpov/templates/files/view.html @@ -0,0 +1,33 @@ +{% extends 'base.html' %} + +{% block javascript %} +{% autoescape off %} +{{ preview_static }} +{% endautoescape %} +{% endblock %} + +{% block content %} +
    +
    +

    {{ file.name }}

    + {% if not has_perm %} +

    Uploaded by: + {% if file.user.image_cropped %} {% endif %} + {{ file.user.username }}

    + {% endif %} +

    Last updated: {{ file.modified|date:"d.m.Y" }} {{ file.modified|time:"H:i" }}

    +

    File size: {{ file.file_size | filesizeformat }}

    + {% if file.file_type %} +

    File type: {{ file.file_type }}

    + {% endif %} + {% if file.description %} +

    {{ file.description }}

    + {% endif %} +
    +
    + {% autoescape off %} + {{ preview_content }} + {% endautoescape %} +
    +
    +{% endblock %}