From cc20d9aab20dc64acf8a195ef1b4f5c5f359c8b6 Mon Sep 17 00:00:00 2001 From: Alexandr Karpov Date: Sat, 15 Apr 2023 14:32:28 +0300 Subject: [PATCH] added ogg, otf --- akarpov/files/previews/__init__.py | 30 +++++++- .../files/previews/application/__init__.py | 2 +- akarpov/files/previews/application/odt.py | 16 ++++ akarpov/files/previews/audio/__init__.py | 2 +- akarpov/files/previews/audio/oga.py | 73 +++++++++++++++++++ akarpov/files/previews/font/__init__.py | 0 akarpov/files/previews/font/otf.py | 5 ++ akarpov/files/previews/text/common.py | 3 +- akarpov/files/views.py | 26 ++++--- 9 files changed, 142 insertions(+), 15 deletions(-) create mode 100644 akarpov/files/previews/application/odt.py create mode 100644 akarpov/files/previews/audio/oga.py create mode 100644 akarpov/files/previews/font/__init__.py create mode 100644 akarpov/files/previews/font/otf.py diff --git a/akarpov/files/previews/__init__.py b/akarpov/files/previews/__init__.py index 14dadff..57b1184 100644 --- a/akarpov/files/previews/__init__.py +++ b/akarpov/files/previews/__init__.py @@ -3,26 +3,40 @@ previews = { "application": { "zip": application.zip.view, + "java-archive": application.zip.view, "doc": application.doc.view, "docx": application.docx.view, + "vnd.oasis.opendocument.text": application.odt.view, }, "audio": { "aac": audio.basic.view, "mpeg": audio.basic.view, - "ogg": audio.basic.view, + "ogg": audio.oga.view, "opus": audio.basic.view, "wav": audio.basic.view, "webm": audio.basic.view, }, - "video": {"mp4": video.mp4.view, "quicktime": video.basic.view}, + "video": { + "mp4": video.mp4.view, + "ogg": video.basic.view, + "mpeg": video.basic.view, + "quicktime": video.basic.view, + }, "image": { "jpeg": image.basic.view, "png": image.basic.view, "avif": image.basic.view, "bmp": image.basic.view, + "vnd.microsoft.icon": image.basic.view, "gif": image.gif.view, }, - "text": {"css": text.common.view, "plain": text.plain.view, "csv": text.csv.view}, + "text": { + "css": text.common.view, + "html": text.common.view, + "javascript": text.common.view, + "plain": text.plain.view, + "csv": text.csv.view, + }, } source_code = {} @@ -32,10 +46,20 @@ extensions = { "mp4": video.mp4.view, "mp3": audio.basic.view, + "opus": audio.basic.view, "avif": image.basic.view, "bmp": image.basic.view, + "jpeg": image.basic.view, + "jpg": image.basic.view, + "ico": image.basic.view, "mov": video.basic.view, + "ogv": video.basic.view, "doc": application.doc.view, "docx": application.docx.view, + "odt": application.odt.view, "gif": image.gif.view, + "zip": application.zip.view, + "jar": application.zip.view, + "mpeg": video.mp4.view, + "oga": audio.oga.view, } | source_code diff --git a/akarpov/files/previews/application/__init__.py b/akarpov/files/previews/application/__init__.py index 3a9b9a5..ce5d9df 100644 --- a/akarpov/files/previews/application/__init__.py +++ b/akarpov/files/previews/application/__init__.py @@ -1 +1 @@ -from . import doc, docx, zip # noqa +from . import doc, docx, odt, zip # noqa diff --git a/akarpov/files/previews/application/odt.py b/akarpov/files/previews/application/odt.py new file mode 100644 index 0000000..92932a7 --- /dev/null +++ b/akarpov/files/previews/application/odt.py @@ -0,0 +1,16 @@ +import textract + +from akarpov.files.models import File + + +def view(file: File): + static = "" + content = "" + text = ( + textract.process(file.file.path, extension="odt", output_encoding="utf8") + .decode("utf8") + .replace("\t", " ") + ) + for line in text.split("\n"): + content += f"

{line}

" + return static, content diff --git a/akarpov/files/previews/audio/__init__.py b/akarpov/files/previews/audio/__init__.py index 21123fd..29ac151 100644 --- a/akarpov/files/previews/audio/__init__.py +++ b/akarpov/files/previews/audio/__init__.py @@ -1 +1 @@ -from . import basic # noqa +from . import basic, oga # noqa diff --git a/akarpov/files/previews/audio/oga.py b/akarpov/files/previews/audio/oga.py new file mode 100644 index 0000000..36ce087 --- /dev/null +++ b/akarpov/files/previews/audio/oga.py @@ -0,0 +1,73 @@ +import textract + +from akarpov.files.models import File + + +def view(file: File) -> (str, str): + static = f""" + + """ + text = ( + textract.process(file.file.path, extension="ogg", output_encoding="utf8") + .decode("utf8") + .replace("\t", " ") + ) + content = ( + """ +
+
+
+ +
+ +
""" + + f""" +
{text}
+ """ + + """ + + + + + """ + ) + + return static, content diff --git a/akarpov/files/previews/font/__init__.py b/akarpov/files/previews/font/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/akarpov/files/previews/font/otf.py b/akarpov/files/previews/font/otf.py new file mode 100644 index 0000000..61dafa1 --- /dev/null +++ b/akarpov/files/previews/font/otf.py @@ -0,0 +1,5 @@ +from akarpov.files.models import File + + +def view(file: File): + ... diff --git a/akarpov/files/previews/text/common.py b/akarpov/files/previews/text/common.py index e14a50a..362c3c6 100644 --- a/akarpov/files/previews/text/common.py +++ b/akarpov/files/previews/text/common.py @@ -22,10 +22,11 @@ "scala": "scala", "sc": "scala", "sql": "sql", - "html": "html", "rs": "rust", "pl": "perl", "PL": "perl", + "htm": "html", + "html": "html", } diff --git a/akarpov/files/views.py b/akarpov/files/views.py index 28f4755..51a9980 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -1,5 +1,6 @@ import os +import structlog from django.contrib.auth.mixins import LoginRequiredMixin from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404 @@ -17,6 +18,8 @@ from akarpov.files.models import File, Folder from akarpov.files.previews import extensions, previews +logger = structlog.get_logger(__name__) + class TopFolderView(LoginRequiredMixin, ListView): template_name = "files/list.html" @@ -60,16 +63,21 @@ def get_context_data(self, **kwargs): context["has_perm"] = self.object.user == self.request.user static = "" content = "" - if self.object.file_type: - t1, t2 = self.object.file_type.split("/") - extension = self.object.file.path.split(".")[-1] - loaded = False - if t1 in previews: - if t2 in previews[t1]: - static, content = previews[t1][t2](self.object) - loaded = True - if not loaded and extension in extensions: + extension = self.object.file.path.split(".")[-1] + try: + if self.object.file_type: + t1, t2 = self.object.file_type.split("/") + loaded = False + if t1 in previews: + if t2 in previews[t1]: + static, content = previews[t1][t2](self.object) + loaded = True + if not loaded and extension in extensions: + static, content = extensions[extension](self.object) + elif extension in extensions: static, content = extensions[extension](self.object) + except Exception as e: + logger.error(e) context["preview_static"] = static context["preview_content"] = content return context