added ogg, otf

This commit is contained in:
Alexander Karpov 2023-04-15 14:32:28 +03:00
parent 2a9d0d7011
commit cc20d9aab2
9 changed files with 142 additions and 15 deletions

View File

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

View File

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

View File

@ -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"<p class='mt-1'>{line}</p>"
return static, content

View File

@ -1 +1 @@
from . import basic # noqa
from . import basic, oga # noqa

View File

@ -0,0 +1,73 @@
import textract
from akarpov.files.models import File
def view(file: File) -> (str, str):
static = f"""
<meta property="og:title" content="{file.name}" />
"""
text = (
textract.process(file.file.path, extension="ogg", output_encoding="utf8")
.decode("utf8")
.replace("\t", " ")
)
content = (
"""
<div id="waveform">
</div>
<div id="wave-timeline"> </div>
<div class="controls mt-4 d-flex align-items-center justify-content-center text-center">
<button class="btn btn-primary" data-action="play">
<i class="glyphicon glyphicon-play"></i>
Play
/
<i class="glyphicon glyphicon-pause"></i>
Pause
</button>
</div>"""
+ f"""
<div>{text}</div>
"""
+ """
<script src="https://unpkg.com/wavesurfer.js@6.6.3/dist/wavesurfer.js"></script>
<script src="https://unpkg.com/wavesurfer.js@6.6.3/dist/plugin/wavesurfer.timeline.min.js"></script>
<script src="https://unpkg.com/wavesurfer.js@6.6.3/dist/plugin/wavesurfer.regions.min.js"></script>
<script>
window.onload = function() {
wavesurfer = WaveSurfer.create({
container: document.querySelector('#waveform'),
waveColor: '#D9DCFF',
progressColor: '#4353FF',
cursorColor: '#4353FF',
barWidth: 3,
barRadius: 3,
cursorWidth: 1,
height: 200,
barGap: 3,
plugins: [
WaveSurfer.regions.create({}),
WaveSurfer.timeline.create({
container: "#wave-timeline"
})
]
});
wavesurfer.on('error', function(e) {
console.warn(e);
});
"""
+ f"""
wavesurfer.load('{file.file.url}');
"""
+ """
document
.querySelector('[data-action="play"]')
.addEventListener('click', wavesurfer.playPause.bind(wavesurfer));
}
</script>
"""
)
return static, content

View File

View File

@ -0,0 +1,5 @@
from akarpov.files.models import File
def view(file: File):
...

View File

@ -22,10 +22,11 @@
"scala": "scala",
"sc": "scala",
"sql": "sql",
"html": "html",
"rs": "rust",
"pl": "perl",
"PL": "perl",
"htm": "html",
"html": "html",
}

View File

@ -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,9 +63,10 @@ def get_context_data(self, **kwargs):
context["has_perm"] = self.object.user == self.request.user
static = ""
content = ""
extension = self.object.file.path.split(".")[-1]
try:
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]:
@ -70,6 +74,10 @@ def get_context_data(self, **kwargs):
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