forked from rDrama/rDrama
1
0
Fork 0

allow uploading of all types of files (using lain.la)

+ allow multiple file upload in bios and messaging admins
master
Aevann1 2022-06-18 17:53:34 +02:00
parent 56f8cf793e
commit ccdabcba29
12 changed files with 81 additions and 105 deletions

View File

@ -9,6 +9,32 @@ import time
from .const import * from .const import *
def process_files():
body = ''
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
files = request.files.getlist('file')[:4]
for file in files:
if file.content_type.startswith('image/'):
name = f'/images/{time.time()}'.replace('.','') + '.webp'
file.save(name)
url = process_image(v.patron, name)
body += f"\n\n![]({url})"
elif file.content_type.startswith('video/'):
value = process_video(file)
if type(value) is str: body += f"\n\n{value}"
else: return value
elif file.content_type.startswith('audio/'):
body += f"\n\n{process_audio(file)}"
else:
body += f"\n\n{process_other(file)}"
return body
def process_other(file):
req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': file}, timeout=20).json()
return req['files'][0]['url']
def process_audio(file): def process_audio(file):
name = f'/audio/{time.time()}'.replace('.','') + '.mp3' name = f'/audio/{time.time()}'.replace('.','') + '.mp3'
file.save(name) file.save(name)

View File

@ -165,6 +165,8 @@ def sanitize(sanitized, alert=False, edit=False):
signal.signal(signal.SIGALRM, handler) signal.signal(signal.SIGALRM, handler)
signal.alarm(1) signal.alarm(1)
sanitized = sanitized.strip()
if '```' not in sanitized and '<pre>' not in sanitized: if '```' not in sanitized and '<pre>' not in sanitized:
sanitized = linefeeds_regex.sub(r'\1\n\n\2', sanitized) sanitized = linefeeds_regex.sub(r'\1\n\n\2', sanitized)

View File

@ -298,7 +298,8 @@ def api_comment(v):
else: return value else: return value
elif file.content_type.startswith('audio/'): elif file.content_type.startswith('audio/'):
body += f"\n\n{process_audio(file)}" body += f"\n\n{process_audio(file)}"
else: return {"error": "Image/Video/Audio files only"}, 400 else:
body += f"\n\n{process_other(file)}"
body = body.strip() body = body.strip()
@ -697,8 +698,6 @@ def edit_comment(cid, v):
) )
g.db.add(c_choice) g.db.add(c_choice)
body_html = sanitize(body, edit=True)
if '!slots' not in body.lower() and '!blackjack' not in body.lower() and '!wordle' not in body.lower() and AGENDAPOSTER_PHRASE not in body.lower(): if '!slots' not in body.lower() and '!blackjack' not in body.lower() and '!wordle' not in body.lower() and AGENDAPOSTER_PHRASE not in body.lower():
now = int(time.time()) now = int(time.time())
cutoff = now - 60 * 60 * 24 cutoff = now - 60 * 60 * 24
@ -733,25 +732,11 @@ def edit_comment(cid, v):
return {"error": "Too much spam!"}, 403 return {"error": "Too much spam!"}, 403
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": body += process_files()
files = request.files.getlist('file')[:4]
for file in files:
if file.content_type.startswith('image/'):
name = f'/images/{time.time()}'.replace('.','') + '.webp'
file.save(name)
url = process_image(v.patron, name)
body += f"\n\n![]({url})"
elif file.content_type.startswith('video/'):
value = process_video(file)
if type(value) is str: body += f"\n\n{value}"
else: return value
elif file.content_type.startswith('audio/'):
body += f"\n\n{process_audio(file)}"
else: return {"error": "Image/Video/Audio files only"}, 400
body = body.strip() body = body.strip()
body_html = sanitize(body, edit=True) body_html = sanitize(body, edit=True)
if len(body_html) > 20000: abort(400) if len(body_html) > 20000: abort(400)

View File

@ -465,21 +465,7 @@ def edit_post(pid, v):
p.title = title[:500] p.title = title[:500]
p.title_html = title_html p.title_html = title_html
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": body += process_files()
files = request.files.getlist('file')[:4]
for file in files:
if file.content_type.startswith('image/'):
name = f'/images/{time.time()}'.replace('.','') + '.webp'
file.save(name)
url = process_image(v.patron, name)
body += f"\n\n![]({url})"
elif file.content_type.startswith('video/'):
value = process_video(file)
if type(value) is str: body += f"\n\n{value}"
else: return value
elif file.content_type.startswith('audio/'):
body += f"\n\n{process_audio(file)}"
else: return {"error": "Image/Video/Audio files only"}, 400
body = body.strip() body = body.strip()
@ -973,21 +959,7 @@ def submit_post(v, sub=None):
if v.agendaposter and not v.marseyawarded: body = torture_ap(body, v.username) if v.agendaposter and not v.marseyawarded: body = torture_ap(body, v.username)
if request.files.get("file2") and request.headers.get("cf-ipcountry") != "T1": body += process_files()
files = request.files.getlist('file2')[:4]
for file in files:
if file.content_type.startswith('image/'):
name = f'/images/{time.time()}'.replace('.','') + '.webp'
file.save(name)
body += f"\n\n![]({process_image(v.patron, name)})"
elif file.content_type.startswith('video/'):
value = process_video(file)
if type(value) is str: body += f"\n\n{value}"
else: return error(value['error'])
elif file.content_type.startswith('audio/'):
body += f"\n\n{process_audio(file)}"
else:
return error("Image/Video/Audio files only.")
body = body.strip() body = body.strip()
@ -1075,9 +1047,9 @@ def submit_post(v, sub=None):
) )
g.db.add(vote) g.db.add(vote)
if request.files.get('file') and request.headers.get("cf-ipcountry") != "T1": if request.files.get('file-url') and request.headers.get("cf-ipcountry") != "T1":
file = request.files['file'] file = request.files['file-url']
if file.content_type.startswith('image/'): if file.content_type.startswith('image/'):
name = f'/images/{time.time()}'.replace('.','') + '.webp' name = f'/images/{time.time()}'.replace('.','') + '.webp'
@ -1094,7 +1066,7 @@ def submit_post(v, sub=None):
elif file.content_type.startswith('audio/'): elif file.content_type.startswith('audio/'):
post.url = process_audio(file) post.url = process_audio(file)
else: else:
return error("Image/Video/Audio files only.") post.url = process_other(file)
if not post.thumburl and post.url: if not post.thumburl and post.url:
gevent.spawn(thumbnail_thread, post.id) gevent.spawn(thumbnail_thread, post.id)
@ -1241,6 +1213,8 @@ def submit_post(v, sub=None):
body += f'* [ghostarchive.org](https://ghostarchive.org/search?term={quote(href)}) (click to archive)\n\n' body += f'* [ghostarchive.org](https://ghostarchive.org/search?term={quote(href)}) (click to archive)\n\n'
gevent.spawn(archiveorg, href) gevent.spawn(archiveorg, href)
body = body.strip()
body_html = sanitize(body) body_html = sanitize(body)
if len(body_html) < 40000: if len(body_html) < 40000:

View File

@ -215,25 +215,10 @@ def settings_profile_post(v):
msg="Your enemies list has been updated.") msg="Your enemies list has been updated.")
elif request.values.get("bio") or request.files.get('file') and request.headers.get("cf-ipcountry") != "T1": elif request.values.get("bio") or request.files.get('file'):
bio = request.values.get("bio")[:1500] bio = request.values.get("bio")[:1500]
if request.files.get('file'): bio += process_files()
file = request.files['file']
if file.content_type.startswith('image/'):
name = f'/images/{time.time()}'.replace('.','') + '.webp'
file.save(name)
url = process_image(v.patron, name)
bio += f"\n\n![]({url})"
elif file.content_type.startswith('video/'):
value = process_video(file)
if type(value) is str: bio += f"\n\n{value}"
else: return value
elif file.content_type.startswith('audio/'):
bio += f"\n\n{process_audio(file)}"
else:
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "Image/Video/Audio files only"}, 400
return render_template("settings_profile.html", v=v, error="Image/Video/Audio files only."), 400
bio = bio.strip() bio = bio.strip()

View File

@ -199,19 +199,21 @@ def submit_contact(v):
body_html = sanitize(body) body_html = sanitize(body)
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
file=request.files["file"] files = request.files.getlist('file')[:4]
if file.content_type.startswith('image/'): for file in files:
name = f'/images/{time.time()}'.replace('.','') + '.webp' if file.content_type.startswith('image/'):
file.save(name) name = f'/images/{time.time()}'.replace('.','') + '.webp'
url = process_image(v.patron, name) file.save(name)
body_html += f'<img data-bs-target="#expandImageModal" data-bs-toggle="modal" onclick="expandDesktopImage(this.src)" class="img" src="{url}" loading="lazy">' url = process_image(v.patron, name)
elif file.content_type.startswith('video/'): body_html += f'<img data-bs-target="#expandImageModal" data-bs-toggle="modal" onclick="expandDesktopImage(this.src)" class="img" src="{url}" loading="lazy">'
value = process_video(file) elif file.content_type.startswith('video/'):
if type(value) is str: body_html += f"<p>{value}</p>" value = process_video(file)
else: return value if type(value) is str: body_html += f"<p>{value}</p>"
elif file.content_type.startswith('audio/'): else: return value
body_html += f"<p>{process_audio(file)}</p>" elif file.content_type.startswith('audio/'):
else: return {"error": "Image/Video/Audio files only"}, 400 body_html += f"<p>{process_audio(file)}</p>"
else:
body_html += f"<p>{process_other(file)}</p>"

View File

@ -726,19 +726,21 @@ def messagereply(v):
body_html = sanitize(message) body_html = sanitize(message)
if parent.sentto == 2 and request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": if parent.sentto == 2 and request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
file=request.files["file"] files = request.files.getlist('file')[:4]
if file.content_type.startswith('image/'): for file in files:
name = f'/images/{time.time()}'.replace('.','') + '.webp' if file.content_type.startswith('image/'):
file.save(name) name = f'/images/{time.time()}'.replace('.','') + '.webp'
url = process_image(v.patron, name) file.save(name)
body_html += f'<img data-bs-target="#expandImageModal" data-bs-toggle="modal" onclick="expandDesktopImage(this.src)" class="img" src="{url}" loading="lazy">' url = process_image(v.patron, name)
elif file.content_type.startswith('video/'): body_html += f'<img data-bs-target="#expandImageModal" data-bs-toggle="modal" onclick="expandDesktopImage(this.src)" class="img" src="{url}" loading="lazy">'
value = process_video(file) elif file.content_type.startswith('video/'):
if type(value) is str: body_html += f"<p>{value}</p>" value = process_video(file)
else: return value if type(value) is str: body_html += f"<p>{value}</p>"
elif file.content_type.startswith('audio/'): else: return value
body_html += f"<p>{process_audio(file)}</p>" elif file.content_type.startswith('audio/'):
else: return {"error": "Image/Video/Audio files only"}, 400 body_html += f"<p>{process_audio(file)}</p>"
else:
body_html += f"<p>{process_other(file)}</p>"
c = Comment(author_id=v.id, c = Comment(author_id=v.id,

View File

@ -329,7 +329,7 @@
<label class="btn btn-secondary format m-0" for="file-edit-reply-{{c.id}}"> <label class="btn btn-secondary format m-0" for="file-edit-reply-{{c.id}}">
<div id="filename-edit-reply-{{c.id}}"><i class="fas fa-file"></i></div> <div id="filename-edit-reply-{{c.id}}"><i class="fas fa-file"></i></div>
<input autocomplete="off" id="file-edit-reply-{{c.id}}" type="file" multiple="multiple" name="file" accept="image/*, video/*, audio/*" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-edit-reply-{{c.id}}','file-edit-reply-{{c.id}}')" hidden> <input autocomplete="off" id="file-edit-reply-{{c.id}}" type="file" multiple="multiple" name="file" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-edit-reply-{{c.id}}','file-edit-reply-{{c.id}}')" hidden>
</label> </label>
</div> </div>
<a id="edit-btn-{{c.id}}" role="button" form="comment-edit-form-{{c.id}}" class="btn btn-primary ml-2 fl-r commentmob" onclick="comment_edit('{{c.id}}')">Save Edit</a> <a id="edit-btn-{{c.id}}" role="button" form="comment-edit-form-{{c.id}}" class="btn btn-primary ml-2 fl-r commentmob" onclick="comment_edit('{{c.id}}')">Save Edit</a>
@ -581,7 +581,7 @@
&nbsp; &nbsp;
<label class="btn btn-secondary format m-0" for="file-upload-reply-{{c.fullname}}"> <label class="btn btn-secondary format m-0" for="file-upload-reply-{{c.fullname}}">
<div id="filename-show-reply-{{c.fullname}}"><i class="fas fa-file"></i></div> <div id="filename-show-reply-{{c.fullname}}"><i class="fas fa-file"></i></div>
<input autocomplete="off" id="file-upload-reply-{{c.fullname}}" type="file" multiple="multiple" name="file" accept="image/*, video/*, audio/*" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show-reply-{{c.fullname}}','file-upload-reply-{{c.fullname}}')" hidden> <input autocomplete="off" id="file-upload-reply-{{c.fullname}}" type="file" multiple="multiple" name="file" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show-reply-{{c.fullname}}','file-upload-reply-{{c.fullname}}')" hidden>
</label> </label>
</div> </div>
<a id="save-reply-to-{{c.fullname}}" class="btn btn-primary ml-2 fl-r commentmob" onclick="post_comment('{{c.fullname}}', '{{c.post.id}}')"role="button">Comment</a> <a id="save-reply-to-{{c.fullname}}" class="btn btn-primary ml-2 fl-r commentmob" onclick="post_comment('{{c.fullname}}', '{{c.post.id}}')"role="button">Comment</a>
@ -620,7 +620,7 @@
{% if c.sentto == 2 %} {% if c.sentto == 2 %}
<label class="btn btn-secondary m-0 mt-3" for="file-upload"> <label class="btn btn-secondary m-0 mt-3" for="file-upload">
<div id="filename"><i class="fas fa-file"></i></div> <div id="filename"><i class="fas fa-file"></i></div>
<input autocomplete="off" id="file-upload" type="file" name="file" accept="image/*, video/*, audio/*" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename','file-upload')" hidden> <input autocomplete="off" id="file-upload" type="file" name="file" multiple="multiple" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename','file-upload')" hidden>
</label> </label>
{% endif %} {% endif %}
</div> </div>

View File

@ -36,7 +36,7 @@
</label> </label>
<label class="btn btn-secondary m-0 mt-3" for="file-upload"> <label class="btn btn-secondary m-0 mt-3" for="file-upload">
<div id="filename"><i class="fas fa-file"></i></div> <div id="filename"><i class="fas fa-file"></i></div>
<input autocomplete="off" id="file-upload" type="file" name="file" accept="image/*, video/*, audio/*" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename','file-upload')" hidden> <input autocomplete="off" id="file-upload" type="file" name="file" multiple="multiple" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename','file-upload')" hidden>
</label> </label>
<input type="submit" value="Submit" class="btn btn-primary mt-3"> <input type="submit" value="Submit" class="btn btn-primary mt-3">
</form> </form>

View File

@ -606,7 +606,7 @@
&nbsp; &nbsp;
<label class="btn btn-secondary format d-inline-block m-0"> <label class="btn btn-secondary format d-inline-block m-0">
<div id="filename-show"><i class="fas fa-file"></i></div> <div id="filename-show"><i class="fas fa-file"></i></div>
<input autocomplete="off" id="file-upload" type="file" name="file" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} accept="image/*, video/*, audio/*" onchange="changename('filename-show','file-upload')" hidden> <input autocomplete="off" id="file-upload" type="file" name="file" multiple="multiple" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show','file-upload')" hidden>
</label> </label>
</div> </div>
<pre></pre> <pre></pre>

View File

@ -878,7 +878,7 @@
<label class="format btn btn-secondary m-0 ml-1 {% if v %}d-inline-block{% else %}d-none{% endif %}" for="file-upload-edit-{{p.id}}"> <label class="format btn btn-secondary m-0 ml-1 {% if v %}d-inline-block{% else %}d-none{% endif %}" for="file-upload-edit-{{p.id}}">
<div id="filename-show-edit-{{p.id}}"><i class="fas fa-file"></i></div> <div id="filename-show-edit-{{p.id}}"><i class="fas fa-file"></i></div>
<input autocomplete="off" id="file-upload-edit-{{p.id}}" type="file" multiple="multiple" name="file" accept="image/*, video/*, audio/*" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show-edit-{{p.id}}','file-upload-edit-{{p.id}}')" hidden> <input autocomplete="off" id="file-upload-edit-{{p.id}}" type="file" multiple="multiple" name="file" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show-edit-{{p.id}}','file-upload-edit-{{p.id}}')" hidden>
</label> </label>
<small class="format d-none"><i class="fas fa-link" aria-hidden="true"></i></small> <small class="format d-none"><i class="fas fa-link" aria-hidden="true"></i></small>
@ -1075,7 +1075,7 @@
&nbsp; &nbsp;
<label class="format btn btn-secondary m-0 ml-1 {% if v %}d-inline-block{% else %}d-none{% endif %}" for="file-upload-reply-{{p.fullname}}"> <label class="format btn btn-secondary m-0 ml-1 {% if v %}d-inline-block{% else %}d-none{% endif %}" for="file-upload-reply-{{p.fullname}}">
<div id="filename-show-reply-{{p.fullname}}"><i class="fas fa-file"></i></div> <div id="filename-show-reply-{{p.fullname}}"><i class="fas fa-file"></i></div>
<input autocomplete="off" id="file-upload-reply-{{p.fullname}}" type="file" multiple="multiple" name="file" accept="image/*, video/*, audio/*" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show-reply-{{p.fullname}}','file-upload-reply-{{p.fullname}}')" hidden> <input autocomplete="off" id="file-upload-reply-{{p.fullname}}" type="file" multiple="multiple" name="file" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show-reply-{{p.fullname}}','file-upload-reply-{{p.fullname}}')" hidden>
</label> </label>
</div> </div>
<a id="save-reply-to-{{p.fullname}}" role="button" form="reply-to-{{p.fullname}}" class="btn btn-primary text-whitebtn ml-auto fl-r" onclick="post_comment('{{p.fullname}}', '{{p.id}}')">Comment</a> <a id="save-reply-to-{{p.fullname}}" role="button" form="reply-to-{{p.fullname}}" class="btn btn-primary text-whitebtn ml-auto fl-r" onclick="post_comment('{{p.fullname}}', '{{p.id}}')">Comment</a>

View File

@ -111,7 +111,7 @@
<img loading="lazy" id="image-preview" style="max-width:50%"> <img loading="lazy" id="image-preview" style="max-width:50%">
<label class="btn btn-secondary m-0" for="file-upload"> <label class="btn btn-secondary m-0" for="file-upload">
<div id="filename-show">Select File</div> <div id="filename-show">Select File</div>
<input autocomplete="off" id="file-upload" type="file" name="file" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} accept="image/*, video/*, audio/*" hidden> <input autocomplete="off" id="file-upload" type="file" name="file-url" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} hidden>
</label> </label>
<small class="form-text text-muted">Optional if you have text.</small> <small class="form-text text-muted">Optional if you have text.</small>
<small class="form-text text-muted">You can upload images or videos up to 60 seconds.</small> <small class="form-text text-muted">You can upload images or videos up to 60 seconds.</small>
@ -147,7 +147,7 @@
<label class="format btn btn-secondary m-0 ml-1 {% if v %}d-inline-block{% else %}d-none{% endif %}" for="file-upload-submit"> <label class="format btn btn-secondary m-0 ml-1 {% if v %}d-inline-block{% else %}d-none{% endif %}" for="file-upload-submit">
<div id="filename-show-submit"><i class="fas fa-file"></i></div> <div id="filename-show-submit"><i class="fas fa-file"></i></div>
<input autocomplete="off" id="file-upload-submit" multiple="multiple" type="file" name="file2" accept="image/*, video/*, audio/*" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show-submit','file-upload-submit');checkForRequired()" hidden> <input autocomplete="off" id="file-upload-submit" multiple="multiple" type="file" name="file" {% if request.headers.get('cf-ipcountry')=="T1" %}disabled{% endif %} onchange="changename('filename-show-submit','file-upload-submit');checkForRequired()" hidden>
</label> </label>
<div id="preview" class="preview my-3"></div> <div id="preview" class="preview my-3"></div>