vid refactor

remotes/1693045480750635534/spooky-22
Aevann1 2022-05-22 18:13:19 +02:00
parent fca3249706
commit 969d1898b5
11 changed files with 64 additions and 104 deletions

View File

@ -10,7 +10,7 @@ COPY requirements.txt /etc/requirements.txt
RUN pip3 install -r /etc/requirements.txt
RUN mkdir /images && mkdir /songs
RUN mkdir /images && mkdir /songs && mkdir /videos
EXPOSE 80/tcp

View File

@ -2,7 +2,7 @@ from sqlalchemy.orm import deferred, aliased
from secrets import token_hex
import pyotp
from files.helpers.discord import remove_user
from files.helpers.images import *
from files.helpers.media import *
from files.helpers.const import *
from .alts import Alt
from .saves import *

View File

@ -4,6 +4,27 @@ from webptools import gifwebp
import subprocess
import os
from flask import abort
import requests
import time
from .const import *
def process_video(file):
name = f'/videos/{time.time()}'.replace('.','')
file.save(name)
os.system(f'ffmpeg -y -loglevel warning -i {name} -map_metadata -1 {name}.mp4')
os.remove(name)
size = os.stat(f'{name}.mp4').st_size
with open(f"{name}.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=20).json()
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}
return url
if SITE_NAME != 'rDrama' or size > 8 * 1024 * 1024: os.remove(f"{name}.mp4")
def process_image(patron, filename=None, resize=0):
size = os.stat(filename).st_size

View File

@ -8,7 +8,7 @@ from files.helpers.alerts import *
from files.helpers.sanitize import *
from files.helpers.security import *
from files.helpers.get import *
from files.helpers.images import *
from files.helpers.media import *
from files.helpers.const import *
from files.classes import *
from flask import *

View File

@ -27,7 +27,6 @@ def chat( v):
@app.get('/chat.js')
@limiter.exempt
def chatjs():
resp = make_response(send_from_directory('assets', 'js/chat.js'))
return resp

View File

@ -1,6 +1,6 @@
from files.helpers.wrappers import *
from files.helpers.alerts import *
from files.helpers.images import *
from files.helpers.media import *
from files.helpers.const import *
from files.helpers.slots import *
from files.helpers.blackjack import *
@ -307,17 +307,9 @@ def api_comment(v):
return {"error": str(e)}, 400
body += f"\n\n![]({image})"
elif file.content_type.startswith('video/'):
if file.content_type == 'video/webm':
file.save("video.mp4")
else:
file.save("unsanitized.mp4")
os.system(f'ffmpeg -y -loglevel warning -i unsanitized.mp4 -map_metadata -1 -c:v copy -c:a copy video.mp4')
with open("video.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=5).json()
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}, 400
body += f"\n\n{url}"
value = process_video(file)
if type(value) is str: body += f"\n\n{value}"
else: return value
else: return {"error": "Image/Video files only"}, 400
body = body.strip()
@ -773,17 +765,9 @@ def edit_comment(cid, v):
url = process_image(v.patron, name)
body += f"\n\n![]({url})"
elif file.content_type.startswith('video/'):
if file.content_type == 'video/webm':
file.save("video.mp4")
else:
file.save("unsanitized.mp4")
os.system(f'ffmpeg -y -loglevel warning -i unsanitized.mp4 -map_metadata -1 -c:v copy -c:a copy video.mp4')
with open("video.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=5).json()
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}, 400
body += f"\n\n{url}"
value = process_video(file)
if type(value) is str: body += f"\n\n{value}"
else: return value
else: return {"error": "Image/Video files only"}, 400
body = body.strip()

View File

@ -464,17 +464,9 @@ def edit_post(pid, v):
url = process_image(v.patron, name)
body += f"\n\n![]({url})"
elif file.content_type.startswith('video/'):
if file.content_type == 'video/webm':
file.save("video.mp4")
else:
file.save("unsanitized.mp4")
os.system(f'ffmpeg -y -loglevel warning -i unsanitized.mp4 -map_metadata -1 -c:v copy -c:a copy video.mp4')
with open("video.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=5).json()
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}, 400
body += f"\n\n{url}"
value = process_video(file)
if type(value) is str: body += f"\n\n{value}"
else: return value
else: return {"error": "Image/Video files only"}, 400
body = body.strip()
@ -1083,17 +1075,9 @@ def submit_post(v, sub=None):
file.save(name)
body += f"\n\n![]({process_image(v.patron, name)})"
elif file.content_type.startswith('video/'):
if file.content_type == 'video/webm':
file.save("video.mp4")
else:
file.save("unsanitized.mp4")
os.system(f'ffmpeg -y -loglevel warning -i unsanitized.mp4 -map_metadata -1 -c:v copy -c:a copy video.mp4')
with open("video.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=5).json()
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}, 400
body += f"\n\n{url}"
value = process_video(file)
if type(value) is str: body += f"\n\n{value}"
else: return error(value['error'])
else:
return error("Image/Video files only.")
@ -1194,17 +1178,9 @@ def submit_post(v, sub=None):
copyfile(name, name2)
post.thumburl = process_image(v.patron, name2, resize=100)
elif file.content_type.startswith('video/'):
if file.content_type == 'video/webm':
file.save("video.mp4")
else:
file.save("unsanitized.mp4")
os.system(f'ffmpeg -y -loglevel warning -i unsanitized.mp4 -map_metadata -1 -c:v copy -c:a copy video.mp4')
with open("video.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=5).json()
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}, 400
post.url = url
value = process_video(file)
if type(value) is str: post.url = value
else: return error(value['error'])
else:
return error("Image/Video files only.")

View File

@ -217,17 +217,9 @@ def settings_profile_post(v):
url = process_image(v.patron, name)
bio += f"\n\n![]({url})"
elif file.content_type.startswith('video/'):
if file.content_type == 'video/webm':
file.save("video.mp4")
else:
file.save("unsanitized.mp4")
os.system(f'ffmpeg -y -loglevel warning -i unsanitized.mp4 -map_metadata -1 -c:v copy -c:a copy video.mp4')
with open("video.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=5).json()
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}, 400
bio += f"\n\n{url}"
value = process_video(file)
if type(value) is str: bio += f"\n\n{value}"
else: return value
else:
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "Image/Video files only"}, 400
return render_template("settings_profile.html", v=v, error="Image/Video files only."), 400

View File

@ -373,9 +373,9 @@ def log_item(id, v):
@app.get("/api")
@auth_required
def api(v):
return render_template("api.html", v=v)
@limiter.limit("1/day")
def api():
return render_template("api.html")
@app.get("/contact")
@app.get("/press")
@ -404,17 +404,9 @@ def submit_contact(v):
url = process_image(v.patron, name)
body_html += f'<img data-bs-target="#expandImageModal" data-bs-toggle="modal" onclick="expandDesktopImage(this.src)" class="img" src="{url}" loading="lazy">'
elif file.content_type.startswith('video/'):
if file.content_type == 'video/webm':
file.save("video.mp4")
else:
file.save("unsanitized.mp4")
os.system(f'ffmpeg -y -loglevel warning -i unsanitized.mp4 -map_metadata -1 -c:v copy -c:a copy video.mp4')
with open("video.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=5).json()
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}, 400
body_html += f"<p>{url}</p>"
value = process_video(file)
if type(value) is str: body_html += f"<p>{value}</p>"
else: return value
else: return {"error": "Image/Video files only"}, 400
@ -449,7 +441,6 @@ def archives(path):
return resp
@app.get('/e/<emoji>')
@limiter.exempt
def emoji(emoji):
if not emoji.endswith('.webp'): abort(404)
resp = make_response(send_from_directory('assets/images/emojis', emoji))
@ -461,7 +452,6 @@ def emoji(emoji):
@app.get('/assets/<path:path>')
@app.get('/static/assets/<path:path>')
@limiter.exempt
def static_service(path):
resp = make_response(send_from_directory('assets', path))
if request.path.endswith('.webp') or request.path.endswith('.gif') or request.path.endswith('.ttf') or request.path.endswith('.woff2'):
@ -477,14 +467,21 @@ def static_service(path):
@app.get('/images/<path>')
@app.get('/hostedimages/<path>')
@app.get("/static/images/<path>")
@limiter.exempt
def images(path):
resp = make_response(send_from_directory('/images', path.replace('.WEBP','.webp')))
resp.headers.remove("Cache-Control")
resp.headers.add("Cache-Control", "public, max-age=3153600")
if request.path.endswith('.webp'):
resp.headers.remove("Content-Type")
resp.headers.add("Content-Type", "image/webp")
resp.headers.remove("Content-Type")
resp.headers.add("Content-Type" ,"image/webp")
return resp
@app.get('/videos/<path>')
def videos(path):
resp = make_response(send_from_directory('/videos', path.replace('.MP4','.mp4')))
resp.headers.remove("Cache-Control")
resp.headers.add("Cache-Control", "public, max-age=3153600")
resp.headers.remove("Content-Type")
resp.headers.add("Content-Type", "video/mp4")
return resp
@app.get("/robots.txt")

View File

@ -701,17 +701,9 @@ def messagereply(v):
url = process_image(v.patron, name)
body_html += f'<img data-bs-target="#expandImageModal" data-bs-toggle="modal" onclick="expandDesktopImage(this.src)" class="img" src="{url}" loading="lazy">'
elif file.content_type.startswith('video/'):
if file.content_type == 'video/webm':
file.save("video.mp4")
else:
file.save("unsanitized.mp4")
os.system(f'ffmpeg -y -loglevel warning -i unsanitized.mp4 -map_metadata -1 -c:v copy -c:a copy video.mp4')
with open("video.mp4", 'rb') as f:
try: req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=5).json()
except requests.exceptions.ConnectionError: return {"error": "Video upload timed out, please try again!"}
try: url = req['files'][0]['url']
except: return {"error": req['description']}, 400
body_html += f"<p>{url}</p>"
value = process_video(file)
if type(value) is str: body_html += f"<p>{value}</p>"
else: return value
else: return {"error": "Image/Video files only"}, 400
@ -1149,7 +1141,6 @@ def remove_follow(username, v):
@app.get("/logged_out/pp/<id>")
@app.get("/logged_out/uid/<id>/pic")
@app.get("/logged_out/uid/<id>/pic/profile")
@limiter.exempt
@auth_desired
def user_profile_uid(v, id):
if not v and not request.path.startswith('/logged_out'): return redirect(f"/logged_out{request.full_path}")
@ -1164,7 +1155,6 @@ def user_profile_uid(v, id):
return redirect(x.profile_url)
@app.get("/@<username>/pic")
@limiter.exempt
@auth_required
def user_profile_name(v, username):
x = get_user(username)

View File

@ -11,6 +11,7 @@ service postgresql restart
pip3 install -r requirements.txt
mkdir /songs
mkdir /images
mkdir /videos
snap install opera-proxy
ufw allow ssh
ufw allow from 173.245.48.0/20