From 80c0ed3959f779a3e7596557b36703bd12d06a02 Mon Sep 17 00:00:00 2001
From: Aevann
Date: Sat, 6 Apr 2024 09:36:01 +0200
Subject: [PATCH] add poster images to inline videos
---
files/helpers/bleach_body.py | 1 +
files/helpers/media.py | 15 +++++++++++----
files/helpers/regex.py | 8 ++++++++
files/helpers/sanitize.py | 2 +-
files/routes/posts.py | 11 ++---------
5 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/files/helpers/bleach_body.py b/files/helpers/bleach_body.py
index b91d5f177..e461a139f 100644
--- a/files/helpers/bleach_body.py
+++ b/files/helpers/bleach_body.py
@@ -47,6 +47,7 @@ def allowed_attributes(tag, name, value):
if name == 'controls' and value == '': return True
if name == 'preload' and value == 'none': return True
if name == 'src': return is_safe_url(value)
+ if name == 'poster': return is_safe_url(value)
if tag == 'audio':
if name == 'src': return is_safe_url(value)
diff --git a/files/helpers/media.py b/files/helpers/media.py
index 7a6c9c55d..ec6c1bcc6 100644
--- a/files/helpers/media.py
+++ b/files/helpers/media.py
@@ -48,6 +48,8 @@ def media_ratelimit(v):
def process_files(files, v, body, is_dm=False, dm_user=None, is_badge_thread=False, comment_body=None):
if g.is_tor or not files.get("file"): return body
+ g.posterurls = {}
+
files = files.getlist('file')[:20]
if files:
@@ -65,7 +67,8 @@ def process_files(files, v, body, is_dm=False, dm_user=None, is_badge_thread=Fal
if is_badge_thread:
process_badge_entry(name, v, comment_body)
elif file.content_type.startswith('video/'):
- url = process_video(file, v)
+ url, posterurl, name = process_video(file, v)
+ g.posterurls[url] = posterurl
elif file.content_type.startswith('audio/'):
url = f'{SITE_FULL}{process_audio(file, v)}'
elif has_request_context():
@@ -202,14 +205,18 @@ def process_video(file, v):
if SITE == 'watchpeopledie.tv' and v and v.username.lower().startswith("icosaka"):
gevent.spawn(delete_file, new, f'https://videos.{SITE}' + new.split('/videos')[1])
- return f'https://videos.{SITE}' + new.split('/videos')[1]
+ return f'https://videos.{SITE}' + new.split('/videos')[1], None, None
+
+ name = f'/images/{time.time()}'.replace('.','') + '.webp'
+ ffmpeg.input(new).output(name, loglevel="quiet", map_metadata=-1, **{"vf":"scale='iw':-2", 'q:v':3, 'frames:v':1}).run()
+ posterurl = SITE_FULL_IMAGES + name
if SITE == 'watchpeopledie.tv':
if not is_reencoding:
gevent.spawn(rclone_copy, new)
- return f'https://videos.{SITE}' + new.split('/videos')[1]
+ return f'https://videos.{SITE}' + new.split('/videos')[1], posterurl, name
else:
- return f"{SITE_FULL}{new}"
+ return f"{SITE_FULL}{new}", posterurl, name
def process_image(filename, v, resize=0, trim=False, uploader_id=None):
# thumbnails are processed in a thread and not in the request context
diff --git a/files/helpers/regex.py b/files/helpers/regex.py
index 16e4d1014..d1c7678e6 100644
--- a/files/helpers/regex.py
+++ b/files/helpers/regex.py
@@ -1,5 +1,6 @@
import random
import re
+from flask import g
from .config.const import *
@@ -104,6 +105,13 @@ image_check_regex = re.compile(f'!\[\]\(((?!(https:\/\/({hosts})\/|\/)).*?)\)',
video_regex_extensions = '|'.join(VIDEO_FORMATS)
video_sub_regex = re.compile(f'(?
'
+ return f''
+
audio_regex_extensions = '|'.join(AUDIO_FORMATS)
audio_sub_regex = re.compile(f'(?', sanitized)
+ sanitized = video_sub_regex.sub(video_sub_regex_matcher, sanitized)
sanitized = audio_sub_regex.sub(r'', sanitized)
if count_emojis:
diff --git a/files/routes/posts.py b/files/routes/posts.py
index d15f82563..54b47980b 100644
--- a/files/routes/posts.py
+++ b/files/routes/posts.py
@@ -660,15 +660,8 @@ def submit_post(v, hole=None):
copyfile(name, name2)
p.thumburl = process_image(name2, v, resize=99)
elif file.content_type.startswith('video/'):
- p.url = process_video(file, v)
- name = f'/images/{time.time()}'.replace('.','') + '.webp'
- try:
- x = ffmpeg.input(p.url).output(name, loglevel="quiet", map_metadata=-1, **{"vf":"scale='iw':-2", 'q:v':3, 'frames:v':1}).run()
- except:
- if os.path.isfile(name):
- os.remove(name)
- else:
- p.posterurl = SITE_FULL_IMAGES + name
+ p.url, p.posterurl, name = process_video(file, v)
+ if p.posterurl:
name2 = name.replace('.webp', 'r.webp')
copyfile(name, name2)
p.thumburl = process_image(name2, v, resize=99)