diff --git a/files/helpers/media.py b/files/helpers/media.py index aaddf33c5..abfe8771f 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -35,7 +35,7 @@ def media_ratelimit(v): print(STARS, flush=True) abort(500) -def process_files(files, v, body): +def process_files(files, v, body, is_dm=False, dm_user=None): if g.is_tor or not files.get("file"): return body files = files.getlist('file')[:20] @@ -51,14 +51,22 @@ def process_files(files, v, body): name = f'/images/{time.time()}'.replace('.','') + '.webp' file.save(name) url = process_image(name, v) - body = body.replace(f'[{file.filename}]', f' {url} ', 1) elif file.content_type.startswith('video/'): - body = body.replace(f'[{file.filename}]', f' {process_video(file, v)} ', 1) + url = f' {process_video(file, v)} ' elif file.content_type.startswith('audio/'): - body = body.replace(f'[{file.filename}]', f' {SITE_FULL}{process_audio(file, v)} ', 1) + url = f' {SITE_FULL}{process_audio(file, v)} ' else: abort(415) - + + body = body.replace(f'[{file.filename}]', f' {url} ', 1) + + if is_dm: + with open(f"{LOG_DIRECTORY}/dm_images.log", "a+", encoding="utf-8") as f: + if dm_user: + f.write(f'{url}, {v.username}, {v.id}, {dm_user.username}, {dm_user.id}, {int(time.time())}\n') + else: + f.write(f'{url}, {v.username}, {v.id}, Modmail, Modmail, {int(time.time())}\n') + return body.replace('\n ', '\n') @@ -267,59 +275,3 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in db.add(media) return f'{SITE_FULL_IMAGES}{filename}' - - -def process_dm_images(v, user, body): - if not request.files.get("file") or g.is_tor or not get_setting("dm_images"): - return body - - files = request.files.getlist('file')[:20] - - - for file in files: - if f'[{file.filename}]' not in body: - body += f'\n[{file.filename}]' - - if file.content_type.startswith('image/'): - filename = f'/dm_images/{time.time()}'.replace('.','') + '.webp' - file.save(filename) - - try: - with Image.open(filename) as i: - pass - except: - os.remove(filename) - abort(415) - - size = os.stat(filename).st_size - patron = bool(v.patron) - - if size > MAX_IMAGE_AUDIO_SIZE_MB_PATRON * 1024 * 1024 or not patron and size > MAX_IMAGE_AUDIO_SIZE_MB * 1024 * 1024: - os.remove(filename) - abort(413, f"Max image/audio size is {MAX_IMAGE_AUDIO_SIZE_MB} MB ({MAX_IMAGE_AUDIO_SIZE_MB_PATRON} MB for paypigs)") - - with open(filename, 'rb') as f: - os.remove(filename) - try: - req = requests.request( - "POST", - "https://pomf2.lain.la/upload.php", - files={'files[]': f}, - timeout=20, - proxies=proxies - ).json() - except: - abort(400, "Image upload timed out, please try again!") - - try: url = req['files'][0]['url'] - except: abort(400, req['description']) - - body = body.replace(f'[{file.filename}]', f' {url} ', 1) - - with open(f"{LOG_DIRECTORY}/dm_images.log", "a+", encoding="utf-8") as f: - if user: - f.write(f'{url}, {v.username}, {v.id}, {user.username}, {user.id}, {int(time.time())}\n') - else: - f.write(f'{url}, {v.username}, {v.id}, Modmail, Modmail, {int(time.time())}\n') - - return body.strip() diff --git a/files/routes/users.py b/files/routes/users.py index 64878b27b..da1e53166 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -524,13 +524,15 @@ def message2(v:User, username:str): if v.admin_level <= PERMS['MESSAGE_BLOCKED_USERS'] and hasattr(user, 'is_blocked') and user.is_blocked: abort(403, f"@{user.username} is blocking you!") - message = sanitize_raw_body(request.values.get("message"), False) + body = sanitize_raw_body(request.values.get("message"), False) - message = process_dm_images(v, user, message) + if not g.is_tor and get_setting("dm_images"): + body = process_files(request.files, v, body, is_dm=True, dm_user=user) + body = body.strip()[:COMMENT_BODY_LENGTH_LIMIT] #process_files potentially adds characters to the post - if not message: abort(400, "Message is empty!") + if not body: abort(400, "Message is empty!") - body_html = sanitize(message) + body_html = sanitize(body) existing = g.db.query(Comment.id).filter( Comment.author_id == v.id, @@ -544,7 +546,7 @@ def message2(v:User, username:str): parent_submission=None, level=1, sentto=user.id, - body=message, + body=body, body_html=body_html ) g.db.add(c) @@ -565,7 +567,7 @@ def message2(v:User, username:str): url = f'{SITE_FULL}/notifications/messages' - push_notif({user.id}, title, message, url) + push_notif({user.id}, title, body, url) return {"message": "Message sent!"} @@ -601,9 +603,9 @@ def messagereply(v:User): and hasattr(user, 'is_blocked') and user.is_blocked): abort(403, f"You're blocked by @{user.username}") - body = process_dm_images(v, user, body) - - body = body.strip()[:COMMENT_BODY_LENGTH_LIMIT] + if not g.is_tor and get_setting("dm_images"): + body = process_files(request.files, v, body, is_dm=True, dm_user=user) + body = body.strip()[:COMMENT_BODY_LENGTH_LIMIT] #process_files potentially adds characters to the post if not body: abort(400, "Message is empty!") diff --git a/files/templates/admin/dm_images.html b/files/templates/admin/dm_images.html index b8bd9fa95..b7088306d 100644 --- a/files/templates/admin/dm_images.html +++ b/files/templates/admin/dm_images.html @@ -16,7 +16,14 @@ {% for item in items %} - {{item[0]}} + {% set url=item[0] %} + {% if url.endswith('.webp') %} + {{url}} + {% else %} + + {{url}} + + {% endif %} {% if item[1] != "Unknown" %} diff --git a/files/templates/comments.html b/files/templates/comments.html index 786665557..ddd177340 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -545,9 +545,7 @@
- {% set upload_disabled = g.is_tor or not get_setting('dm_images') %} - - {{macros.file_input('file-upload-reply-' ~ c.fullname, True)}} + {{macros.file_input('file-upload-reply-' ~ c.fullname, False, not get_setting('dm_images'))}}
diff --git a/files/templates/userpage/banner.html b/files/templates/userpage/banner.html index 7d9f6797b..dfd924a9a 100644 --- a/files/templates/userpage/banner.html +++ b/files/templates/userpage/banner.html @@ -157,9 +157,7 @@ - {% set upload_disabled = g.is_tor or not get_setting('dm_images') %} - - {{macros.file_input('file-upload-macro', True)}} + {{macros.file_input('file-upload-macro', False, not get_setting('dm_images'))}} diff --git a/files/templates/util/macros.html b/files/templates/util/macros.html index 94e2d8f70..a9de2cc5a 100644 --- a/files/templates/util/macros.html +++ b/files/templates/util/macros.html @@ -99,10 +99,10 @@ {% endmacro %} -{% macro file_input(id, image_only) %} -