added json, fixed big files preview

This commit is contained in:
Alexander Karpov 2023-04-25 11:42:06 +03:00
parent 85ae29696e
commit 062b794005
6 changed files with 93 additions and 1 deletions

View File

@ -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

View File

@ -1 +1 @@
from . import doc, docx, odt, pdf, zip # noqa
from . import doc, docx, json, odt, pdf, zip # noqa

View File

@ -0,0 +1,84 @@
from akarpov.files.models import File
def view(file: File):
static = """
<style>
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
</style>
"""
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 = (
"""
<div id="json" class="col-auto"></div>
<script>
function output(inp) {
document.getElementById("json").appendChild(document.createElement('pre')).innerHTML = inp;
}
function syntaxHighlight(json) {
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\\s*:)?|\b(true|false|null)\b|-?\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?)/g,
function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
"""
+ req
+ """
</script>
"""
)
return static, content

View File

@ -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]

View File

@ -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"""
<meta property="og:title" content="{file.name}" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/atom-one-light.min.css">

View File

@ -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)