"
+ with file.file.open("r") as f:
+ lines = f.readlines()
+ style = False
+ js = False
+ for line in lines:
+ if "" in line:
+ style = False
+
+ if "" in line:
+ js = False
+
+ if style:
+ content += f"""
{html.escape(line)}
"""
+ elif js:
+ content += (
+ f"""
{html.escape(line)}
"""
+ )
+ else:
+ content += f"""
{html.escape(line)}
"""
+
+ content += """
+
+
+
+
+
+ """
+
+ return static, content
+
+
+def meta(file: File):
+ descr = ""
+ i = 0
+ with file.file.open("r") as f:
+ lines = f.readlines()
+ for line in lines:
+ if i == 0:
+ descr += line + "\n"
+ else:
+ descr += line + " "
+ i += 1
+ if i > 20:
+ descr += "..."
+ break
+ url = file.get_absolute_url()
+ section = ""
+ if file.file_type:
+ section = file.file_type.split("/")[0]
+
+ meat_f = f"""
+
+
+
+
+
+
+
+
+
+ """
+ return meat_f
diff --git a/akarpov/files/services/preview.py b/akarpov/files/services/preview.py
index af61183..4b761c3 100644
--- a/akarpov/files/services/preview.py
+++ b/akarpov/files/services/preview.py
@@ -4,6 +4,8 @@
from PIL import Image, ImageDraw, ImageFont
from preview_generator.manager import PreviewManager
+from akarpov.files.models import File
+
cache_path = "/tmp/preview_cache"
PIL_GRAYSCALE = "L"
PIL_WIDTH_INDEX = 0
@@ -92,3 +94,26 @@ def get_description(file_path: str) -> str:
if manager.has_text_preview(file_path):
return manager.get_text_preview(file_path)
return ""
+
+
+def get_base_meta(file: File):
+ preview = file.preview.url if file.preview else ""
+ description = file.description if file.description else ""
+ return f"""
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ """
diff --git a/akarpov/files/views.py b/akarpov/files/views.py
index 695536d..3f58af4 100644
--- a/akarpov/files/views.py
+++ b/akarpov/files/views.py
@@ -18,6 +18,7 @@
from akarpov.files.forms import FileForm
from akarpov.files.models import File, Folder
from akarpov.files.previews import extensions, meta, meta_extensions, previews
+from akarpov.files.services.preview import get_base_meta
logger = structlog.get_logger(__name__)
@@ -69,38 +70,47 @@ def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
- context = super().get_context_data(**kwargs)
- context["has_perm"] = self.object.user == self.request.user
static = ""
content = ""
meta_s = ""
extension = self.object.file.path.split(".")[-1]
+
try:
+ meta_loaded = False
if self.object.file_type:
t1, t2 = self.object.file_type.split("/")
loaded = False
+
+ # get static and content for file view by mimetype
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)
- if self.object.file_type:
- t1, t2 = self.object.file_type.split("/")
- loaded = False
+ # get meta tags by mimetype
if t1 in meta:
if t2 in meta[t1]:
meta_s = meta[t1][t2](self.object)
- loaded = True
- if not loaded and extension in meta_extensions:
+ meta_loaded = True
+ if not meta_loaded and extension in meta_extensions:
meta_s = meta_extensions[extension](self.object)
- elif extension in meta_extensions:
- meta_s = meta_extensions[extension](self.object)
+ else:
+ # get static and content for file view by file extension
+ if extension in extensions:
+ static, content = extensions[extension](self.object)
+ # get meta tags by file extension
+ if extension in meta_extensions:
+ meta_s = meta_extensions[extension](self.object)
+ meta_loaded = True
+ if not meta_loaded:
+ meta_s = get_base_meta(self.object)
except Exception as e:
logger.error(e)
+
+ context = super().get_context_data(**kwargs)
+ context["has_perm"] = self.object.user == self.request.user
context["preview_static"] = static
context["preview_content"] = content
context["meta"] = meta_s
diff --git a/akarpov/users/signals.py b/akarpov/users/signals.py
new file mode 100644
index 0000000..b7667cd
--- /dev/null
+++ b/akarpov/users/signals.py
@@ -0,0 +1,11 @@
+from django.db.models.signals import pre_save
+from django.dispatch import receiver
+
+from akarpov.users.models import User
+
+
+@receiver(pre_save, sender=User)
+def user_create(sender, instance: User, **kwargs):
+ if instance.id is None:
+ # give user some space on file share on register
+ instance.left_file_upload += 100 * 1024 * 1024