mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-22 06:16:34 +03:00
added json, fixed big files preview
This commit is contained in:
parent
85ae29696e
commit
062b794005
|
@ -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
|
||||
|
|
|
@ -1 +1 @@
|
|||
from . import doc, docx, odt, pdf, zip # noqa
|
||||
from . import doc, docx, json, odt, pdf, zip # noqa
|
||||
|
|
84
akarpov/files/previews/application/json.py
Normal file
84
akarpov/files/previews/application/json.py
Normal 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, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
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
|
|
@ -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]
|
||||
|
|
|
@ -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">
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user