From d76fe63c7f70ee1028897553aa09343ae7248609 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sat, 5 Nov 2022 23:01:23 +0200 Subject: [PATCH] constantify image extensions and fix imgur .gif bug --- files/assets/js/submit.js | 5 ++--- files/helpers/const.py | 6 +++--- files/helpers/jinja2.py | 2 +- files/helpers/regex.py | 3 ++- files/helpers/sanitize.py | 2 +- files/routes/asset_submissions.py | 4 ++-- files/routes/static.py | 12 ++++-------- files/templates/header.html | 4 ++++ files/templates/settings_profile.html | 4 ++-- files/templates/sub/settings.html | 6 +++--- files/templates/submit_hats.html | 5 ++--- files/templates/submit_marseys.html | 5 ++--- files/templates/update_assets.html | 5 ++--- 13 files changed, 30 insertions(+), 33 deletions(-) diff --git a/files/assets/js/submit.js b/files/assets/js/submit.js index d9afeffcb..a02d7d027 100644 --- a/files/assets/js/submit.js +++ b/files/assets/js/submit.js @@ -79,13 +79,12 @@ document.onpaste = function(event) { f.files = files; document.getElementById('filename-show').textContent = filename; document.getElementById('urlblock').classList.add('d-none'); - if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".png") || filename.endsWith(".gif") || filename.endsWith(".webp")) + if (IMAGE_FORMATS.some(s => filename.endsWith(s))) { var fileReader = new FileReader(); fileReader.readAsDataURL(f.files[0]); fileReader.addEventListener("load", function () {document.getElementById('image-preview').setAttribute('src', this.result);}); } - document.getElementById('file-upload').setAttribute('required', 'false'); document.getElementById('post-url').value = null; localStorage.setItem("post-url", "") document.getElementById('image-upload-block').classList.remove('d-none') @@ -99,7 +98,7 @@ document.getElementById('file-upload').addEventListener('change', function(){ document.getElementById('urlblock').classList.add('d-none'); document.getElementById('filename-show').textContent = document.getElementById('file-upload').files[0].name.substr(0, 20); filename = f.files[0].name.toLowerCase() - if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".png") || filename.endsWith(".gif") || filename.endsWith(".webp")) + if (IMAGE_FORMATS.some(s => filename.endsWith(s))) { var fileReader = new FileReader(); fileReader.readAsDataURL(f.files[0]); diff --git a/files/helpers/const.py b/files/helpers/const.py index 7b6bef96b..b17b02148 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -1598,6 +1598,6 @@ BOOSTED_SITES = { 'forums.red' } -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') +IMAGE_FORMATS = ['png','gif','jpg','jpeg','webp'] +VIDEO_FORMATS = ['mp4','webm','mov','avi','mkv','flv','m4v','3gp'] +AUDIO_FORMATS = ['mp3','wav','ogg','aac','m4a','flac'] diff --git a/files/helpers/jinja2.py b/files/helpers/jinja2.py index 281b34426..bf4da773d 100644 --- a/files/helpers/jinja2.py +++ b/files/helpers/jinja2.py @@ -53,5 +53,5 @@ def inject_constants(): "CONTENT_SECURITY_POLICY_DEFAULT":CONTENT_SECURITY_POLICY_DEFAULT, "CONTENT_SECURITY_POLICY_HOME":CONTENT_SECURITY_POLICY_HOME, "TRUESCORE_DONATE_LIMIT":TRUESCORE_DONATE_LIMIT, - "BAN_EVASION_DOMAIN":BAN_EVASION_DOMAIN, "HOUSE_JOIN_COST":HOUSE_JOIN_COST, "HOUSE_SWITCH_COST":HOUSE_SWITCH_COST + "BAN_EVASION_DOMAIN":BAN_EVASION_DOMAIN, "HOUSE_JOIN_COST":HOUSE_JOIN_COST, "HOUSE_SWITCH_COST":HOUSE_SWITCH_COST, "IMAGE_FORMATS":IMAGE_FORMATS } diff --git a/files/helpers/regex.py b/files/helpers/regex.py index 08ac83e55..b67b50e03 100644 --- a/files/helpers/regex.py +++ b/files/helpers/regex.py @@ -77,8 +77,9 @@ video_sub_regex = re.compile(f'(

[^<]*)(https:\/\/([a-z0-9-]+\.)*({hosts})\/[\ 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 = re.compile("(^|\s)(https:\/\/[\w\-.#&/=\?@%;+,:]{5,250}(\.png|\.jpg|\.jpeg|\.gif|\.webp)(\?[\w\-.#&/=\?@%;+,:]*)?)($|\s)", flags=re.I|re.A) image_regex_extensions = '|'.join(IMAGE_FORMATS) +image_regex = re.compile(f"(^|\s)(https:\/\/[\w\-.#&/=\?@%;+,:]{{5,250}}\.({image_regex_extensions})(\?[\w\-.#&/=\?@%;+,:]*)?)($|\s)", flags=re.I|re.A) +image_regex_extensions = image_regex_extensions.replace('|gif', '') imgur_regex = re.compile(f'(https:\/\/i\.imgur\.com\/[a-z0-9]+)\.({image_regex_extensions})', flags=re.I|re.A) giphy_regex = re.compile('(https:\/\/media\.giphy\.com\/media\/[a-z0-9]+\/giphy)\.gif', flags=re.I|re.A) diff --git a/files/helpers/sanitize.py b/files/helpers/sanitize.py index 1559dfc4d..8b5f46b9b 100644 --- a/files/helpers/sanitize.py +++ b/files/helpers/sanitize.py @@ -474,7 +474,7 @@ def normalize_url(url): .replace("https://nitter.42l.fr/", "https://twitter.com/") \ .replace("https://nitter.lacontrevoie.fr/", "https://twitter.com/") - url = imgur_regex.sub(r'\1_d.webp?maxwidth=9999&fidelity=high', url) + url = imgur_regex.sub(r'\1_d.webp?maxwidth=9999&fidelity=grand', url) url = giphy_regex.sub(r'\1.webp', url) return url diff --git a/files/routes/asset_submissions.py b/files/routes/asset_submissions.py index 2248d23de..82052a622 100644 --- a/files/routes/asset_submissions.py +++ b/files/routes/asset_submissions.py @@ -375,7 +375,7 @@ if SITE not in ('pcmemes.net', 'watchpeopledie.tv'): if not file.content_type.startswith('image/'): return error("You need to submit an image!") - for x in ('png','jpeg','webp','gif'): + for x in IMAGE_FORMATS: if path.isfile(f'/asset_submissions/marseys/original/{name}.{x}'): os.remove(f'/asset_submissions/marseys/original/{name}.{x}') @@ -449,7 +449,7 @@ if SITE not in ('pcmemes.net', 'watchpeopledie.tv'): format = i.format.lower() new_path = f'/asset_submissions/hats/original/{name}.{format}' - for x in ('png','jpeg','webp','gif'): + for x in IMAGE_FORMATS: if path.isfile(f'/asset_submissions/hats/original/{name}.{x}'): os.remove(f'/asset_submissions/hats/original/{name}.{x}') diff --git a/files/routes/static.py b/files/routes/static.py index 59d53596d..2b1a8b32d 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -34,14 +34,10 @@ def marseys(v): original = os.listdir("/asset_submissions/marseys/original") for marsey, user in marseys: - if f'{marsey.name}.png' in original: - marsey.og = f'{marsey.name}.png' - elif f'{marsey.name}.webp' in original: - marsey.og = f'{marsey.name}.webp' - elif f'{marsey.name}.gif' in original: - marsey.og = f'{marsey.name}.gif' - elif f'{marsey.name}.jpeg' in original: - marsey.og = f'{marsey.name}.jpeg' + for x in IMAGE_FORMATS: + if f'{marsey.name}.{x}' in original: + marsey.og = f'{marsey.name}.{x}' + break else: marseys = g.db.query(Marsey).filter(Marsey.submitter_id==None).order_by(Marsey.count.desc()) diff --git a/files/templates/header.html b/files/templates/header.html index a0f530fdd..f3e192233 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -367,6 +367,10 @@

{{v.formkey}}
{% endif %} + + {% if not v %}