From 062b794005fc669575db93790209549140a21107 Mon Sep 17 00:00:00 2001 From: Alexandr Karpov Date: Tue, 25 Apr 2023 11:42:06 +0300 Subject: [PATCH] added json, fixed big files preview --- akarpov/files/previews/__init__.py | 2 + .../files/previews/application/__init__.py | 2 +- akarpov/files/previews/application/json.py | 84 +++++++++++++++++++ akarpov/files/previews/text/common.py | 2 + akarpov/files/previews/text/html.py | 2 + akarpov/files/previews/text/plain.py | 2 + 6 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 akarpov/files/previews/application/json.py diff --git a/akarpov/files/previews/__init__.py b/akarpov/files/previews/__init__.py index 3420969..9adc404 100644 --- a/akarpov/files/previews/__init__.py +++ b/akarpov/files/previews/__init__.py @@ -9,6 +9,7 @@ "docx": application.docx.view, "vnd.oasis.opendocument.text": application.odt.view, "x-httpd-php": text.common.view, + "json": application.json.view, }, "audio": { "aac": audio.basic.view, @@ -77,6 +78,7 @@ "oga": audio.oga.view, "pdf": application.pdf.view, "html": text.html.view, + "json": application.json.view, } | source_code | fonts_ext diff --git a/akarpov/files/previews/application/__init__.py b/akarpov/files/previews/application/__init__.py index ce34ad6..1fa0548 100644 --- a/akarpov/files/previews/application/__init__.py +++ b/akarpov/files/previews/application/__init__.py @@ -1 +1 @@ -from . import doc, docx, odt, pdf, zip # noqa +from . import doc, docx, json, odt, pdf, zip # noqa diff --git a/akarpov/files/previews/application/json.py b/akarpov/files/previews/application/json.py new file mode 100644 index 0000000..11a4fd1 --- /dev/null +++ b/akarpov/files/previews/application/json.py @@ -0,0 +1,84 @@ +from akarpov.files.models import File + + +def view(file: File): + static = """ + + """ + if file.file_size < 200 * 1024: + req = ( + f""" + getJSON('{file.file.url}', + """ + + """ + function(err, data) { + if (err !== null) { + console.log('Something went wrong: ' + err); + } else { + var str = JSON.stringify(data, undefined, 4); + output(syntaxHighlight(str)); + } + }); + """ + ) + else: + req = """ + output("file is too large, download to view") + """ + + content = ( + """ +
+ + """ + ) + return static, content diff --git a/akarpov/files/previews/text/common.py b/akarpov/files/previews/text/common.py index 8b98e23..2071bd5 100644 --- a/akarpov/files/previews/text/common.py +++ b/akarpov/files/previews/text/common.py @@ -30,6 +30,8 @@ def view(file: File) -> (str, str): + if file.file_size > 10 * 1024 * 1024: + return "", "file is too large to view" extension = file.file.path.split(".")[-1] if extension in language_previews: extension = language_previews[extension] diff --git a/akarpov/files/previews/text/html.py b/akarpov/files/previews/text/html.py index 7db7e21..c1f36ca 100644 --- a/akarpov/files/previews/text/html.py +++ b/akarpov/files/previews/text/html.py @@ -4,6 +4,8 @@ def view(file: File) -> (str, str): + if file.file_size > 10 * 1024 * 1024: + return "", "file is too large to view" static = f""" diff --git a/akarpov/files/previews/text/plain.py b/akarpov/files/previews/text/plain.py index 03437e9..b8ac6a0 100644 --- a/akarpov/files/previews/text/plain.py +++ b/akarpov/files/previews/text/plain.py @@ -6,6 +6,8 @@ def view(file: File) -> (str, str): + if file.file_size > 10 * 1024 * 1024: + return "", "file is too large to view" extension = file.file.path.split(".")[-1] if hasattr(text, extension): return getattr(text, extension).view(file)