From e7d78d59ecfc5174612bd97204832d920d8deef4 Mon Sep 17 00:00:00 2001 From: Alexandr Karpov Date: Wed, 12 Apr 2023 13:14:34 +0300 Subject: [PATCH] added audio, video previews, fixed image previews --- akarpov/files/previews/__init__.py | 21 +++- akarpov/files/previews/audio/__init__.py | 1 + akarpov/files/previews/audio/basic.py | 61 +++++++++++ akarpov/files/previews/audio/waves.py | 102 ++++++++++++++++++ akarpov/files/previews/image/__init__.py | 2 +- .../previews/image/{jpeg.py => basic.py} | 5 +- akarpov/files/previews/video/mp4.py | 2 +- akarpov/files/views.py | 15 ++- akarpov/templates/files/view.html | 4 +- .../local/django/install_preview_dependencies | 2 +- 10 files changed, 200 insertions(+), 15 deletions(-) create mode 100644 akarpov/files/previews/audio/__init__.py create mode 100644 akarpov/files/previews/audio/basic.py create mode 100644 akarpov/files/previews/audio/waves.py rename akarpov/files/previews/image/{jpeg.py => basic.py} (89%) diff --git a/akarpov/files/previews/__init__.py b/akarpov/files/previews/__init__.py index df2941f..e08a383 100644 --- a/akarpov/files/previews/__init__.py +++ b/akarpov/files/previews/__init__.py @@ -1,3 +1,20 @@ -from . import image, video +from . import audio, image, video -previews = {"video": {"mp4": video.mp4.view}, "image": {"jpeg": image.jpeg.view}} +previews = { + "audio": { + "aac": audio.basic.view, + "mpeg": audio.basic.view, + "ogg": audio.basic.view, + "opus": audio.basic.view, + "wav": audio.basic.view, + "webm": audio.basic.view, + }, + "video": {"mp4": video.mp4.view}, + "image": { + "jpeg": image.basic.view, + "png": image.basic.view, + "avif": image.basic.view, + }, +} + +extensions = {"mp4": video.mp4.view, "mp3": audio.basic.view, "avif": image.basic.view} diff --git a/akarpov/files/previews/audio/__init__.py b/akarpov/files/previews/audio/__init__.py new file mode 100644 index 0000000..21123fd --- /dev/null +++ b/akarpov/files/previews/audio/__init__.py @@ -0,0 +1 @@ +from . import basic # noqa diff --git a/akarpov/files/previews/audio/basic.py b/akarpov/files/previews/audio/basic.py new file mode 100644 index 0000000..4b26ac0 --- /dev/null +++ b/akarpov/files/previews/audio/basic.py @@ -0,0 +1,61 @@ +from akarpov.files.models import File + + +def view(file: File) -> (str, str): + static = """ + """ + content = ( + """ +
+
+
+ +
+ +
+ + + + + """ + ) + + return static, content diff --git a/akarpov/files/previews/audio/waves.py b/akarpov/files/previews/audio/waves.py new file mode 100644 index 0000000..a079633 --- /dev/null +++ b/akarpov/files/previews/audio/waves.py @@ -0,0 +1,102 @@ +from akarpov.files.models import File + + +def view(file: File) -> (str, str): + static = """ + + """ + content = ( + """ +
+ + +
+ """ + + """ + + """ + ) + + return static, content diff --git a/akarpov/files/previews/image/__init__.py b/akarpov/files/previews/image/__init__.py index ae2d45a..21123fd 100644 --- a/akarpov/files/previews/image/__init__.py +++ b/akarpov/files/previews/image/__init__.py @@ -1 +1 @@ -from . import jpeg # noqa +from . import basic # noqa diff --git a/akarpov/files/previews/image/jpeg.py b/akarpov/files/previews/image/basic.py similarity index 89% rename from akarpov/files/previews/image/jpeg.py rename to akarpov/files/previews/image/basic.py index bb7aa50..c3302b0 100644 --- a/akarpov/files/previews/image/jpeg.py +++ b/akarpov/files/previews/image/basic.py @@ -10,6 +10,8 @@ def view(file: File) -> (str, str):
Picture
+
+
""" + """ @@ -23,10 +25,7 @@ def view(file: File) -> (str, str): } }); - // Get the Viewer.js instance after initialized var viewer = $image.data('viewer'); - - // View a list of images $('#images').viewer(); """ diff --git a/akarpov/files/previews/video/mp4.py b/akarpov/files/previews/video/mp4.py index 93864ee..1ab30fa 100644 --- a/akarpov/files/previews/video/mp4.py +++ b/akarpov/files/previews/video/mp4.py @@ -9,7 +9,7 @@ def view(file: File) -> (str, str): content = f""" diff --git a/akarpov/files/views.py b/akarpov/files/views.py index 973ea99..3f9b183 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -11,7 +11,7 @@ ChunkedUploadView, ) from akarpov.files.models import File, Folder -from akarpov.files.previews import previews +from akarpov.files.previews import extensions, previews class TopFolderView(LoginRequiredMixin, ListView): @@ -41,10 +41,15 @@ def get_context_data(self, **kwargs): 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) + if self.object.file_type == "application/octet-stream": + extension = self.object.file.path.split(".")[-1] + if extension in extensions: + static, content = extensions[extension](self.object) + else: + 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 diff --git a/akarpov/templates/files/view.html b/akarpov/templates/files/view.html index 3d8b2a2..a8d2ff6 100644 --- a/akarpov/templates/files/view.html +++ b/akarpov/templates/files/view.html @@ -8,7 +8,7 @@ {% block content %}
-