forked from rDrama/rDrama
don't use pomf for DM images anymore and allow for video and audio in DMs
parent
db6ac78658
commit
0dc8b5ccb1
|
@ -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()
|
||||
|
|
|
@ -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!")
|
||||
|
||||
|
|
|
@ -16,7 +16,14 @@
|
|||
{% for item in items %}
|
||||
<tr>
|
||||
<td>
|
||||
<img alt="{{item[0]}}" src="{{item[0]}}" loading="lazy" class="dm_img" data-nonce="{{g.nonce}}" data-onclick="expandImage()">
|
||||
{% set url=item[0] %}
|
||||
{% if url.endswith('.webp') %}
|
||||
<img alt="{{url}}" src="{{url}}" loading="lazy" class="dm_img" data-nonce="{{g.nonce}}" data-onclick="expandImage()">
|
||||
{% else %}
|
||||
<a href="{{url}}">
|
||||
{{url}}
|
||||
</a>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
{% if item[1] != "Unknown" %}
|
||||
|
|
|
@ -545,9 +545,7 @@
|
|||
<div class="comment-format" id="comment-format-bar-{{c.id}}">
|
||||
<button type="button" data-nonce="{{g.nonce}}" data-onclick="loadEmojis('reply-form-body-{{c.id}}')" class="btn btn-secondary format m-0" data-bs-toggle="modal" data-bs-target="#emojiModal" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Add Emoji"><i class="fas fa-smile-beam"></i></button>
|
||||
|
||||
{% 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'))}}
|
||||
</div>
|
||||
|
||||
<div class="ml-auto">
|
||||
|
|
|
@ -157,9 +157,7 @@
|
|||
|
||||
<button type="button" class="btn btn-secondary format d-inline-block m-0 py-2 fas fa-smile-beam" data-nonce="{{g.nonce}}" data-onclick="loadEmojis('input-message')" data-bs-toggle="modal" data-bs-target="#emojiModal" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Add Emoji"></button>
|
||||
|
||||
{% 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'))}}
|
||||
|
||||
<input type="submit" data-nonce="{{g.nonce}}" data-onclick="remove_dialog()" value="Submit" class="btn btn-primary fl-r">
|
||||
</form>
|
||||
|
|
|
@ -99,10 +99,10 @@
|
|||
{% endmacro %}
|
||||
|
||||
|
||||
{% macro file_input(id, image_only) %}
|
||||
<label class="btn btn-secondary format m-0 ml-2" for="{{id}}">
|
||||
{% macro file_input(id, image_only, disabled) %}
|
||||
<label class="btn btn-secondary format m-0 ml-2" for="{{id}}" {% if g.is_tor or disabled %}disabled{% endif %}>
|
||||
<i class="fas fa-{% if image_only %}image{% else %}file{% endif %}"></i><span></span>
|
||||
<input autocomplete="off" id="{{id}}" accept="image/*{% if not image_only %}, video/*, audio/*{% endif %}" type="file" multiple="multiple" name="file" {% if g.is_tor %}disabled{% endif %} hidden>
|
||||
<input autocomplete="off" id="{{id}}" accept="image/*{% if not image_only %}, video/*, audio/*{% endif %}" type="file" multiple="multiple" name="file" {% if g.is_tor or disabled %}disabled{% endif %} hidden>
|
||||
</label>
|
||||
<button type="button" class="text-danger text-lg font-weight-bold ml-2 remove-files d-none" data-bs-toggle="tooltip" title="Remove Files">X</button>
|
||||
{% endmacro %}
|
||||
|
|
Loading…
Reference in New Issue