From 5ad72d2faf5c120c2b76e98ee474430a05138a35 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 1 Feb 2025 12:38:46 +0530 Subject: [PATCH] animated preview --- content/models.py | 51 ++++++++++++++++++++++++++- content/templates/content-header.html | 3 +- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/content/models.py b/content/models.py index c9a4942..dc5f2c2 100644 --- a/content/models.py +++ b/content/models.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- - +import html +import os +import subprocess from django.conf import settings from django.core.validators import MaxLengthValidator @@ -162,6 +164,53 @@ class Content(models.Model): if src: return mark_safe(''.format(self.get_absolute_url(), src)) + def og_meta_headers(self): + headers = [] + values = [ + ('og:title', self.title), + ] + if self.photo and self.photo.image.url.endswith('.gif'): + video_path = c.photo.image.path.replace('.gif', '.mp4') + image_path = c.photo.image.path.replace('.gif', '.jpg') + if not os.path.exists(video_path): + cmd = [ + 'ffmpeg', + '-hide_banner', '-nostats', '-loglevel', 'error', + '-i', c.photo.image.path, video_path + ] + subprocess.call(cmd) + if not os.path.exists(image_path): + cmd = [ + 'ffmpeg', + '-hide_banner', '-nostats', '-loglevel', 'error', + '-i', c.photo.image.path, '-frames:v', '1', image_path + ] + subprocess.call(cmd) + if os.path.exists(video_path): + video_url = self.photo.image.url.replace('.gif', '.mp4') + if video_url.startswith('/'): + video_url = 'https://studio.camp' + video_url + image_url = video_url.replace('.mp4', '.jpg') + width = self.photo.image.width + height = self.photo.image.height + values = [ + ('og:video', video_url), + ('og:video:type', "video/mp4"), + ('og:video:width', str(width)), + ('og:video:height', str(height)), + ('og:image', image_url), + ] + values + else: + values += [ + ('og:image', 'https://studio.camp%s' % self.image_url), + ] + + for key, value in values: + headers.append( + '' % (html.escape(key, True), html.escape(value, True)) + ) + return mark_safe("\n".join(headers)) + @cached_property def image_url(self): if self.photo: diff --git a/content/templates/content-header.html b/content/templates/content-header.html index 69c9b0c..0f7e68e 100644 --- a/content/templates/content-header.html +++ b/content/templates/content-header.html @@ -1,7 +1,6 @@ {{ content.title }} - CAMP - +{{ content.og_meta_headers }} -