From 6f16ba3be03c92b1fda42f5c4f007ffef4d16cf5 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 28 Jul 2023 20:01:56 +0300 Subject: [PATCH] enforce subprocess consistency --- files/helpers/config/const.py | 1 - files/helpers/media.py | 11 +++++++---- files/routes/posts.py | 5 ++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/files/helpers/config/const.py b/files/helpers/config/const.py index 3ed63fb8f..c03a0476d 100644 --- a/files/helpers/config/const.py +++ b/files/helpers/config/const.py @@ -602,7 +602,6 @@ MAX_IMAGE_AUDIO_SIZE_MB = 8 MAX_IMAGE_AUDIO_SIZE_MB_PATRON = 16 MAX_VIDEO_SIZE_MB = 32 MAX_VIDEO_SIZE_MB_PATRON = 100 -SUBPROCESS_TIMEOUT_DURATION = 30 # seconds ANTISPAM_BYPASS_IDS = set() diff --git a/files/helpers/media.py b/files/helpers/media.py index 694065d88..0f2fc19ee 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -19,6 +19,9 @@ from files.helpers.settings import get_setting from .config.const import * +def subprocess_run(params): + subprocess.run(params, check=True, timeout=30) + def remove_media_using_link(path): if SITE in path: path = path.split(SITE, 1)[1] @@ -85,7 +88,7 @@ def process_audio(file, v): new = old + '.' + extension try: - subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-i", old, "-map_metadata", "-1", "-c:a", "copy", new], check=True, timeout=SUBPROCESS_TIMEOUT_DURATION) + subprocess_run(["ffmpeg", "-y", "-i", old, "-map_metadata", "-1", "-c:a", "copy", new]) except: os.remove(old) if os.path.isfile(new): @@ -108,7 +111,7 @@ def process_audio(file, v): def convert_to_mp4(old, new): tmp = new.replace('.mp4', '-t.mp4') try: - subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-threads:v", "1", "-i", old, "-map_metadata", "-1", tmp], check=True, stderr=subprocess.STDOUT, timeout=SUBPROCESS_TIMEOUT_DURATION) + subprocess_run(["ffmpeg", "-y", "-threads:v", "1", "-i", old, "-map_metadata", "-1", tmp]) except: os.remove(old) if os.path.isfile(tmp): @@ -148,7 +151,7 @@ def process_video(file, v): gevent.spawn(convert_to_mp4, old, new) else: try: - subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new], check=True, timeout=SUBPROCESS_TIMEOUT_DURATION) + subprocess_run(["ffmpeg", "-y", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new]) except: os.remove(old) if os.path.isfile(new): @@ -204,7 +207,7 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in params.append(filename) try: - subprocess.run(params, timeout=SUBPROCESS_TIMEOUT_DURATION) + subprocess_run(params) except: os.remove(filename) if has_request: diff --git a/files/routes/posts.py b/files/routes/posts.py index 74e9bac83..fae0b15d9 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -22,6 +22,7 @@ from files.helpers.sanitize import * from files.helpers.settings import get_setting from files.helpers.slots import * from files.helpers.sorting_and_time import * +from files.helpers.media import subprocess_run from files.routes.routehelpers import execute_shadowban_viewers_and_voters from files.routes.wrappers import * @@ -655,9 +656,7 @@ def submit_post(v:User, sub=None): p.url = process_video(file, v) name = f'/images/{time.time()}'.replace('.','') + '.webp' try: - subprocess.run(['ffmpeg', '-y', '-loglevel', 'warning', - '-i', p.url, '-vf', "scale='iw':-2", - '-q:v', '3', '-frames:v', '1', name], check=True, timeout=SUBPROCESS_TIMEOUT_DURATION) + subprocess_run(["ffmpeg", "-y", "-i", p.url, "-vf", "scale='iw':-2", "-q:v", "3", "-frames:v", "1", name]) except: if os.path.isfile(name): os.remove(name)