From b9c482712a463857f723df9193e1798a20a03393 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sun, 30 Oct 2022 16:55:43 +0200 Subject: [PATCH] constantify file extensions --- files/classes/submission.py | 6 +++--- files/helpers/const.py | 4 ++++ files/helpers/regex.py | 11 ++++++++--- files/routes/posts.py | 10 +++------- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/files/classes/submission.py b/files/classes/submission.py index 0946e2e41..a552c8581 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -388,17 +388,17 @@ class Submission(Base): @property @lazy def is_video(self): - return self.url and any((self.url.lower().endswith(x) for x in ('.mp4','.webm','.mov'))) and is_safe_url(self.url) + return self.url and any((self.url.lower().split('?')[0].endswith(f'.{x}') for x in VIDEO_FORMATS)) and is_safe_url(self.url) @property @lazy def is_audio(self): - return self.url and any((self.url.lower().endswith(x) for x in ('.mp3','.wav','.ogg','.aac','.m4a','.flac'))) and is_safe_url(self.url) + return self.url and any((self.url.lower().split('?')[0].endswith(f'.{x}') for x in AUDIO_FORMATS)) and is_safe_url(self.url) @property @lazy def is_image(self): - return self.url and any((self.url.lower().endswith(x) for x in ('.webp','.jpg','.png','.gif','.jpeg','?maxwidth=9999','&fidelity=high'))) and is_safe_url(self.url) + return self.url and any((self.url.lower().split('?')[0].endswith(f'.{x}') for x in IMAGE_FORMATS)) and is_safe_url(self.url) @lazy def filtered_flags(self, v): diff --git a/files/helpers/const.py b/files/helpers/const.py index fa87d5e3b..d26acdc2a 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -1388,3 +1388,7 @@ BOOSTED_SITES = { 't.me', 'web.telegram.org' } + +IMAGE_FORMATS = ('webp','jpg','jpeg','png','gif','gifv','tif','tiff') +VIDEO_FORMATS = ('mp4','webm','mov','avi','mkv','flv','m4v','3gp') +AUDIO_FORMATS = ('mp3','wav','ogg','aac','m4a','flac') diff --git a/files/helpers/regex.py b/files/helpers/regex.py index 0065c36bc..e6a826307 100644 --- a/files/helpers/regex.py +++ b/files/helpers/regex.py @@ -66,10 +66,15 @@ torture_regex_exclude = re.compile('^\s*>', flags=re.A) image_check_regex = re.compile(f'!\[\]\(((?!(https:\/\/([a-z0-9-]+\.)*({hosts})\/|\/)).*?)\)', flags=re.A) -video_sub_regex = re.compile(f'(

[^<]*)(https:\/\/([a-z0-9-]+\.)*({hosts})\/[\w:~,()\-.#&\/=?@%;+]*?\.(mp4|webm|mov))', flags=re.A) -audio_sub_regex = re.compile(f'(

[^<]*)(https:\/\/([a-z0-9-]+\.)*({hosts})\/[\w:~,()\-.#&\/=?@%;+]*?\.(mp3|wav|ogg|aac|m4a|flac))', flags=re.A) +video_regex_extensions = '|'.join(VIDEO_FORMATS) +video_sub_regex = re.compile(f'(

[^<]*)(https:\/\/([a-z0-9-]+\.)*({hosts})\/[\w:~,()\-.#&\/=?@%;+]*?\.({video_regex_extensions}))', flags=re.A) + +audio_regex_extensions = '|'.join(AUDIO_FORMATS) +audio_sub_regex = re.compile(f'(

[^<]*)(https:\/\/([a-z0-9-]+\.)*({hosts})\/[\w:~,()\-.#&\/=?@%;+]*?\.({audio_regex_extensions}))', flags=re.A) + +image_regex_extensions = '|'.join(IMAGE_FORMATS) +imgur_regex = re.compile(f'(https:\/\/i\.imgur\.com\/[a-z0-9]+)\.({image_regex_extensions})', flags=re.I|re.A) -imgur_regex = re.compile('(https:\/\/i\.imgur\.com\/[a-z0-9]+)\.(jpg|png|jpeg|webp)', flags=re.I|re.A) giphy_regex = re.compile('(https:\/\/media\.giphy\.com\/media\/[a-z0-9]+\/giphy)\.gif', flags=re.I|re.A) youtube_regex = re.compile('(

[^<]*)(https:\/\/youtube\.com\/watch\?v\=([a-z0-9-_]{5,20})[\w\-.#&/=\?@%+]*)', flags=re.I|re.A) diff --git a/files/routes/posts.py b/files/routes/posts.py index a3cde9eb4..98548077d 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -1068,11 +1068,7 @@ def pin_post(post_id, v): return abort(404, "Post not found!") -extensions = ( - '.webp','.jpg','.png','.jpeg','.gif','.gifv','.tif', '.tiff', - '.mp4','.webm','.mov', - '.mp3','.wav','.ogg','.aac','.m4a','.flac' -) +extensions = IMAGE_FORMATS + VIDEO_FORMATS + AUDIO_FORMATS @app.get("/submit/title") @limiter.limit("3/minute") @@ -1083,8 +1079,8 @@ def get_post_title(v): url = request.values.get("url") if not url or '\\' in url: abort(400) - checking_url = url.lower().rstrip('%3F').rstrip('?') - if any((checking_url.endswith(x) for x in extensions)): + checking_url = url.lower().split('?')[0].split('%3F')[0] + if any((checking_url.endswith(f'.{x}') for x in extensions)): abort(400) try: x = requests.get(url, headers=titleheaders, timeout=5, proxies=proxies)