diff --git a/files/assets/js/comments_v.js b/files/assets/js/comments_v.js index e9cf04d19..814b2be25 100644 --- a/files/assets/js/comments_v.js +++ b/files/assets/js/comments_v.js @@ -265,64 +265,35 @@ document.onpaste = function(event) { const focused = document.activeElement; const files = structuredClone(event.clipboardData.files); - if (files.length > 4) - { - alert("You can't upload more than 4 files at one time!") - return - } - if (!files.length) return if (focused.id.includes('reply-form-body-')) { const fullname = focused.dataset.fullname; f=document.getElementById('file-upload-reply-' + fullname); - try { - let filename = '' - for (const file of files) - filename += file.name + ', ' - filename = filename.toLowerCase().slice(0, -2) - f.files = files; - document.getElementById('filename-show-reply-' + fullname).textContent = filename; - } - catch(e) {} + f.files = files; + changename('filename-show-reply-' + fullname, f.id, focused.id) } else if (focused.id.includes('comment-edit-body-')) { const id = focused.dataset.id; f=document.getElementById('file-edit-reply-' + id); - let filename = '' - for (const file of files) - filename += file.name + ', ' - filename = filename.toLowerCase().slice(0, -2) f.files = files; - document.getElementById('filename-edit-reply-' + id).textContent = filename; + changename('filename-edit-reply-' + id, f.id, focused.id) } else if (focused.id.includes('post-edit-box-')) { const id = focused.dataset.id; f=document.getElementById('file-upload-edit-' + id); - let filename = '' - for (const file of files) - filename += file.name + ', ' - filename = filename.toLowerCase().slice(0, -2) f.files = files; - document.getElementById('filename-show-edit-' + id).textContent = filename; + changename('filename-show-edit-' + id, f.id, focused.id) } else if (focused.id == "input-message") { f=document.getElementById('file-upload'); - let filename = '' - for (const file of files) - filename += file.name + ', ' - filename = filename.toLowerCase().slice(0, -2) f.files = files; - document.getElementById('filename').textContent = filename; + changename('filename', f.id, focused.id) } else if (focused.id == "input-message-mobile") { f=document.getElementById('file-upload-mobile'); - let filename = '' - for (const file of files) - filename += file.name + ', ' - filename = filename.toLowerCase().slice(0, -2) f.files = files; - document.getElementById('filename-mobile').textContent = filename; + changename('filename-mobile', f.id, focused.id) } } diff --git a/files/assets/js/contact.js b/files/assets/js/contact.js index 8c1aef042..bc6ad51d6 100644 --- a/files/assets/js/contact.js +++ b/files/assets/js/contact.js @@ -1,19 +1,9 @@ document.onpaste = function(event) { const files = structuredClone(event.clipboardData.files); - if (files.length > 4) - { - alert("You can't upload more than 4 files at one time!") - return - } - if (!files.length) return const f = document.getElementById('file-upload'); - let filename = '' - for (const file of files) - filename += file.name + ', ' - filename = filename.toLowerCase().slice(0, -2) f.files = files; - document.getElementById('filename').textContent = filename; + changename('filename', f.id, 'input-message') } diff --git a/files/assets/js/core.js b/files/assets/js/core.js index 91badc1ca..6e2c50134 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -244,7 +244,8 @@ function escapeHTML(unsafe) { return unsafe.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } -function changename(s1,s2) { +function changename(s1,s2,textarea) { + console.log('nigg') const files = document.getElementById(s2).files; if (files.length > 4) { @@ -252,11 +253,24 @@ function changename(s1,s2) { document.getElementById(s2).value = null return } + + const ta = document.getElementById(textarea); + ta.value = ta.value.replace(/\n/g, "") + if (ta.value) { + ta.value += '\n' + } + let filename = ''; for (const e of files) { filename += e.name.substr(0, 22) + ', '; + ta.value += '\n' } document.getElementById(s1).innerHTML = escapeHTML(filename.slice(0, -2)); + + console.log(ta) + autoExpand(ta) + ta.focus() + ta.selectionStart = ta.selectionEnd = ta.value.length; } function showmore(t) { diff --git a/files/assets/js/settings_profile.js b/files/assets/js/settings_profile.js index f538f605f..40bef5df0 100644 --- a/files/assets/js/settings_profile.js +++ b/files/assets/js/settings_profile.js @@ -101,24 +101,14 @@ function updatebgselection(){ document.onpaste = function(event) { const focused = document.activeElement; - if (focused.id == 'bio-text') { + if (focused.id == 'profile-bio-text') { const files = structuredClone(event.clipboardData.files); - if (files.length > 4) - { - alert("You can't upload more than 4 files at one time!") - return - } - if (files.length) { f=document.getElementById('file-upload'); - let filename = '' - for (const file of files) - filename += file.name + ', ' - filename = filename.toLowerCase().slice(0, -2) f.files = files; - document.getElementById('filename-show').textContent = filename; + changename('filename-show', f.id, focused.id) } } } diff --git a/files/assets/js/submit.js b/files/assets/js/submit.js index 4f74e59bb..6fa052ad0 100644 --- a/files/assets/js/submit.js +++ b/files/assets/js/submit.js @@ -78,12 +78,8 @@ document.onpaste = function(event) { { filename = filename.name.toLowerCase() if (document.activeElement.id == 'post-text') { - let filename = '' - for (const file of files) - filename += file.name + ', ' - filename = filename.toLowerCase().slice(0, -2) document.getElementById('file-upload-submit').value = files; - document.getElementById('filename-show-submit').textContent = filename; + changename('filename-show-submit', 'file-upload-submit', 'post-text') } else { f=document.getElementById('file-upload'); diff --git a/files/helpers/media.py b/files/helpers/media.py index 810da839d..68db56cb6 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -25,8 +25,7 @@ def media_ratelimit(v): count = g.db.query(Media).filter(Media.user_id == v.id, Media.created_utc > t).count() if count > 50: abort(500) -def process_files(files, v): - body = '' +def process_files(files, v, body): if g.is_tor or not files.get("file"): return body files = files.getlist('file')[:4] @@ -34,15 +33,17 @@ def process_files(files, v): media_ratelimit(v) for file in files: + if '' not in body: + abort(400, "Missing in text!") if file.content_type.startswith('image/'): name = f'/images/{time.time()}'.replace('.','') + '.webp' file.save(name) url = process_image(name, v) - body += f"\n\n![]({url})" + body = body.replace('', f"![]({url})", 1) elif file.content_type.startswith('video/'): - body += f"\n\n{SITE_FULL}{process_video(file, v)}" + body = body.replace('', f"{SITE_FULL}{process_video(file, v)}", 1) elif file.content_type.startswith('audio/'): - body += f"\n\n{SITE_FULL}{process_audio(file, v)}" + body = body.replace('', f"{SITE_FULL}{process_audio(file, v)}", 1) else: abort(415) return body @@ -229,13 +230,15 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in return filename -def process_dm_images(v, user): +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')[:4] for file in files: + if '' not in body: + abort(400, "Missing in text!") + if file.content_type.startswith('image/'): filename = f'/dm_images/{time.time()}'.replace('.','') + '.webp' file.save(filename) @@ -263,12 +266,12 @@ def process_dm_images(v, user): try: url = req['files'][0]['url'] except: abort(400, req['description']) - body += f'\n\n{url}\n\n' + body = body.replace('', 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') - if body: - with open(f"{LOG_DIRECTORY}/dm_images.log", "a+", encoding="utf-8") as f: - if user: - f.write(f'{body.strip()}, {v.username}, {v.id}, {user.username}, {user.id}, {int(time.time())}\n') - else: - f.write(f'{body.strip()}, {v.username}, {v.id}, Modmail, Modmail, {int(time.time())}\n') - return body + return body.strip() diff --git a/files/routes/comments.py b/files/routes/comments.py index befb2e4b1..de3dd5da4 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -159,6 +159,8 @@ def comment(v:User): media_ratelimit(v) for file in files: + if '' not in body: abort(400, "Missing in text!") + if file.content_type.startswith('image/'): oldname = f'/images/{time.time()}'.replace('.','') + '.webp' file.save(oldname) @@ -201,11 +203,11 @@ def comment(v:User): purge_files_in_cache(f"https://{SITE}/assets/images/badges/{badge.id}.webp") except Exception as e: abort(400, str(e)) - body += f"\n\n![]({image})" + body = body.replace('', f"![]({image})", 1) elif file.content_type.startswith('video/'): - body += f"\n\n{SITE_FULL}{process_video(file, v)}" + body = body.replace('', f"{SITE_FULL}{process_video(file, v)}", 1) elif file.content_type.startswith('audio/'): - body += f"\n\n{SITE_FULL}{process_audio(file, v)}" + body = body.replace('', f"{SITE_FULL}{process_audio(file, v)}", 1) else: abort(415) @@ -405,7 +407,7 @@ def edit_comment(cid, v): execute_antispam_comment_check(body, v) - body += process_files(request.files, v) + body = process_files(request.files, v, body) body = body.strip()[:COMMENT_BODY_LENGTH_LIMIT] # process_files potentially adds characters to the post body_for_sanitize = body diff --git a/files/routes/posts.py b/files/routes/posts.py index 8f449b0ee..7f16bdee1 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -294,7 +294,7 @@ def edit_post(pid, v): p.title = title p.title_html = title_html - body += process_files(request.files, v) + body = process_files(request.files, v, body) body = body.strip()[:POST_BODY_LENGTH_LIMIT] # process_files() may be adding stuff to the body if body != p.body: @@ -655,7 +655,7 @@ def submit_post(v:User, sub=None): body, bets, options, choices = sanitize_poll_options(v, body, True) - body += process_files(request.files, v) + body = process_files(request.files, v, body) body = body.strip()[:POST_BODY_LENGTH_LIMIT] # process_files() adds content to the body, so we need to re-strip torture = (v.agendaposter and not v.marseyawarded and sub != 'chudrama') diff --git a/files/routes/settings.py b/files/routes/settings.py index 66b01fe53..61e875d52 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -298,7 +298,7 @@ def settings_personal_post(v): elif not updated and FEATURES['USERS_PROFILE_BODYTEXT'] and \ (request.values.get("bio") or request.files.get('file')): bio = request.values.get("bio")[:1500] - bio += process_files(request.files, v) + bio = process_files(request.files, v, bio) bio = bio.strip() bio_html = sanitize(bio, blackjack="bio") diff --git a/files/routes/static.py b/files/routes/static.py index 73201168d..c78813a93 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -256,7 +256,7 @@ def submit_contact(v): abort(403) body = f'This message has been sent automatically to all admins via [/contact](/contact)\n\nMessage:\n\n{body}' - body += process_files(request.files, v) + body = process_files(request.files, v, body) body = body.strip() body_html = sanitize(body) diff --git a/files/routes/users.py b/files/routes/users.py index d5b3e6fb5..0373e1bfb 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -518,7 +518,7 @@ def message2(v:User, username:str): message = sanitize_raw_body(request.values.get("message"), False) - message += process_dm_images(v, user) + message = process_dm_images(v, user, message) if not message: abort(400, "Message is empty!") @@ -592,9 +592,7 @@ 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.strip() + body = process_dm_images(v, user, body) if not body: abort(400, "Message is empty!") diff --git a/files/templates/chat.html b/files/templates/chat.html index 07622b8b8..5c601ea69 100644 --- a/files/templates/chat.html +++ b/files/templates/chat.html @@ -116,7 +116,7 @@ diff --git a/files/templates/comments.html b/files/templates/comments.html index b779725ec..12f4fbd53 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -273,7 +273,7 @@ Formatting help @@ -540,7 +540,7 @@ diff --git a/files/templates/contact.html b/files/templates/contact.html index 2d1141a54..f67bf9ee5 100644 --- a/files/templates/contact.html +++ b/files/templates/contact.html @@ -16,7 +16,7 @@ diff --git a/files/templates/settings/common.html b/files/templates/settings/common.html index 28e486880..72d0cd082 100644 --- a/files/templates/settings/common.html +++ b/files/templates/settings/common.html @@ -88,7 +88,7 @@ {% if show_file_upload %} {% endif %} diff --git a/files/templates/submission.html b/files/templates/submission.html index 7be5e2862..8d7406c75 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -178,7 +178,7 @@ diff --git a/files/templates/submit.html b/files/templates/submit.html index f89a6f044..bbfb492e8 100644 --- a/files/templates/submit.html +++ b/files/templates/submit.html @@ -62,7 +62,7 @@
diff --git a/files/templates/userpage/banner.html b/files/templates/userpage/banner.html index eab12e59f..cabdf1f15 100644 --- a/files/templates/userpage/banner.html +++ b/files/templates/userpage/banner.html @@ -161,7 +161,7 @@   @@ -468,7 +468,7 @@   diff --git a/files/templates/util/macros.html b/files/templates/util/macros.html index 98be32a43..49a0bf6bf 100644 --- a/files/templates/util/macros.html +++ b/files/templates/util/macros.html @@ -130,7 +130,7 @@ {% if allow_file_upload %} {% endif %}