From c310dfba83bd6c954387cdab092f44344fe82773 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Tue, 13 Aug 2019 23:15:17 +0200 Subject: [PATCH] Fix cache for video and some cleanup --- templates/utils.html | 2 +- utils/template_filters.py | 37 +++++++++++++++++-------------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/templates/utils.html b/templates/utils.html index edaa8f1..b8c106b 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -129,7 +129,7 @@ {% if (a.mediaType and a.mediaType.startswith("image/")) or (a.type and a.type == 'Image') %} {% elif (a.mediaType and a.mediaType.startswith("video/")) %} -
  • +
  • {% else %}
  • {% if a.filename %}{{ a.filename }}{% else %}{{ a.url }}{% endif %}
  • {% endif %} diff --git a/utils/template_filters.py b/utils/template_filters.py index dd995e0..dd2b7aa 100644 --- a/utils/template_filters.py +++ b/utils/template_filters.py @@ -1,7 +1,9 @@ import logging +import mimetypes import urllib from datetime import datetime from datetime import timezone +from functools import lru_cache from typing import Dict from typing import Optional from typing import Tuple @@ -79,6 +81,15 @@ ALLOWED_TAGS = [ "h4", "h5", "h6", + "table", + "th", + "tr", + "td", + "thead", + "tbody", + "tfoot", + "colgroup", + "caption", ] @@ -219,20 +230,11 @@ def get_total_answers_count(obj, meta): return cnt -_GRIDFS_CACHE: Dict[Tuple[Kind, str, Optional[int]], str] = {} - - -def _get_file_url(url, size, kind): - k = (kind, url, size) - cached = _GRIDFS_CACHE.get(k) - if cached: - return cached - +@lru_cache(512) +def _get_file_url(url, size, kind) -> str: doc = MEDIA_CACHE.get_file(url, size, kind) if doc: - u = f"/media/{str(doc._id)}" - _GRIDFS_CACHE[k] = u - return u + return f"/media/{str(doc._id)}" # MEDIA_CACHE.cache(url, kind) _logger.error(f"cache not available for {url}/{size}/{kind}") @@ -308,15 +310,10 @@ def has_actor_type(doc): return False +@lru_cache(512) def _is_img(filename): - filename = filename.lower() - if ( - filename.endswith(".png") - or filename.endswith(".jpg") - or filename.endswith(".jpeg") - or filename.endswith(".gif") - or filename.endswith(".svg") - ): + mimetype, _ = mimetypes.guess_type(filename.lower()) + if mimetype and mimetype.split("/")[0] in ["image"]: return True return False