Cache video

This commit is contained in:
Thomas Sileo 2019-08-11 14:58:09 +02:00
parent 9768e673d7
commit 35f9275456
3 changed files with 29 additions and 7 deletions

View file

@ -230,6 +230,17 @@ def task_cache_attachments() -> _Response:
obj = activity.get_object()
if obj.has_type(ap.ActivityType.VIDEO):
if isinstance(obj.url, list):
for link in obj.url:
if link.get("mimeType", "").startswith("video/"):
config.MEDIA_CACHE.cache_attachment({"url": link["href"]}, iri)
break
elif isinstance(obj.url, str):
config.MEDIA_CACHE.cache_attachment({"url": obj.url}, iri)
else:
app.logger.warning(f"failed to parse video link {obj!r} for {iri}")
# Iter the attachments
for attachment in obj._data.get("attachment", []):
try:
@ -284,9 +295,12 @@ def task_cache_actor() -> _Response:
# )
app.logger.info(f"actor cached for {iri}")
if activity.has_type(
[ap.ActivityType.CREATE, ap.ActivityType.ANNOUNCE]
) and activity.get_object()._data.get("attachment", []):
if not activity.has_type([ap.ActivityType.CREATE, ap.ActivityType.ANNOUNCE]):
return ""
if activity.get_object()._data.get(
"attachment", []
) or activity.get_object().has_type(ap.ActivityType.VIDEO):
Tasks.cache_attachments(iri)
except (ActivityGoneError, ActivityNotFoundError):

View file

@ -67,7 +67,7 @@
{% if obj.summary %}<p class="p-summary">{{ obj.summary | clean | safe }}</p>{% endif %}
{% if obj | has_type('Video') %}
<div class="note-video">
<video controls preload="metadata" src="{{ obj.url | get_video_link }}" width="480">
<video controls preload="metadata" src="{{ obj.url | get_video_url }}" width="480">
</video>
</div>
{% endif %}

View file

@ -249,6 +249,11 @@ def get_attachment_url(url, size):
return _get_file_url(url, size, Kind.ATTACHMENT)
@filters.app_template_filter()
def get_video_url(url):
return _get_file_url(url, None, Kind.ATTACHMENT)
@filters.app_template_filter()
def get_og_image_url(url, size=100):
try:
@ -271,9 +276,12 @@ def remove_mongo_id(dat):
@filters.app_template_filter()
def get_video_link(data):
if isinstance(data, list):
for link in data:
if link.get("mimeType", "").startswith("video/"):
return link.get("href")
elif isinstance(data, str):
return data
return None