make remove_media() to fix 500 error

pull/138/head
Aevann 2023-03-06 21:32:08 +02:00
parent 5be3a0fe8d
commit 873292d60d
6 changed files with 46 additions and 34 deletions

View File

@ -20,6 +20,18 @@ from files.helpers.settings import get_setting
from .config.const import *
def remove_media(path):
img_prefix = f'https://i.{SITE}'
if path.startswith(img_prefix):
path = path.split(img_prefix, 1)[1]
video_prefix = f'https://videos.{SITE}'
if path.startswith(video_prefix):
path = path.split(video_prefix, 1)[1]
os.remove(path)
def media_ratelimit(v):
t = time.time() - 86400
count = g.db.query(Media).filter(Media.user_id == v.id, Media.created_utc > t).count()
@ -63,7 +75,7 @@ def process_audio(file, v):
size = os.stat(name).st_size
if size > MAX_IMAGE_AUDIO_SIZE_MB_PATRON * 1024 * 1024 or not v.patron and size > MAX_IMAGE_AUDIO_SIZE_MB * 1024 * 1024:
os.remove(name)
remove_media(name)
abort(413, f"Max image/audio size is {MAX_IMAGE_AUDIO_SIZE_MB} MB ({MAX_IMAGE_AUDIO_SIZE_MB_PATRON} MB for {patron.lower()}s)")
media = g.db.query(Media).filter_by(filename=name, kind='audio').one_or_none()
@ -84,7 +96,7 @@ def webm_to_mp4(old, new, vid, db):
tmp = new.replace('.mp4', '-t.mp4')
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-threads:v", "1", "-i", old, "-map_metadata", "-1", tmp], check=True, stderr=subprocess.STDOUT)
os.replace(tmp, new)
os.remove(old)
remove_media(old)
media = db.query(Media).filter_by(filename=new, kind='video').one_or_none()
if media: db.delete(media)
@ -111,7 +123,7 @@ def process_video(file, v):
if (SITE_NAME != 'WPD' and
(size > MAX_VIDEO_SIZE_MB_PATRON * 1024 * 1024
or not v.patron and size > MAX_VIDEO_SIZE_MB * 1024 * 1024)):
os.remove(old)
remove_media(old)
abort(413, f"Max video size is {MAX_VIDEO_SIZE_MB} MB ({MAX_VIDEO_SIZE_MB_PATRON} MB for paypigs)")
name_original = secure_filename(file.filename)
@ -125,7 +137,7 @@ def process_video(file, v):
gevent.spawn(webm_to_mp4, old, new, v.id, db)
else:
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new], check=True)
os.remove(old)
remove_media(old)
media = g.db.query(Media).filter_by(filename=new, kind='video').one_or_none()
if media: g.db.delete(media)
@ -150,7 +162,7 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in
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)
remove_media(filename)
if has_request:
abort(413, f"Max image/audio size is {MAX_IMAGE_AUDIO_SIZE_MB} MB ({MAX_IMAGE_AUDIO_SIZE_MB_PATRON} MB for paypigs)")
return None
@ -168,7 +180,7 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in
except UnidentifiedImageError as e:
print(f"Couldn't identify an image for {filename}; deleting... (user {v.id if v else '-no user-'})")
try:
os.remove(filename)
remove_media(filename)
except: pass
if has_request:
abort(415)
@ -185,7 +197,7 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in
if resize:
if os.stat(filename).st_size > MAX_IMAGE_SIZE_BANNER_RESIZED_MB * 1024 * 1024:
os.remove(filename)
remove_media(filename)
if has_request:
abort(413, f"Max size for site assets is {MAX_IMAGE_SIZE_BANNER_RESIZED_MB} MB")
return None
@ -216,7 +228,7 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in
i_hash = str(imagehash.phash(i))
if i_hash in hashes.keys():
os.remove(filename)
remove_media(filename)
if has_request:
abort(409, "Image already exists! " + hashes[i_hash].split('/')[-1])
return None
@ -257,11 +269,11 @@ def process_dm_images(v, user, body):
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)
remove_media(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)
remove_media(filename)
try:
req = requests.request(
"POST",

View File

@ -1771,7 +1771,7 @@ def delete_media_post(v):
if not os.path.isfile(path):
return render_template("admin/delete_media.html", v=v, url=url, error="File not found on the server!")
os.remove(path)
remove_media(path)
ma=ModAction(
kind="delete_media",

View File

@ -205,8 +205,8 @@ def remove_asset(cls, type_name:str, v:User, name:str) -> dict[str, str]:
g.db.add(ma)
g.db.delete(asset)
os.remove(f"/asset_submissions/{type_name}s/{name}.webp")
os.remove(f"/asset_submissions/{type_name}s/{name}")
remove_media(f"/asset_submissions/{type_name}s/{name}.webp")
remove_media(f"/asset_submissions/{type_name}s/{name}")
return {"message": f"'{name}' removed!"}
@ -271,7 +271,7 @@ def submit_hat(v:User):
with Image.open(highquality) as i:
if i.width > 100 or i.height > 130:
os.remove(highquality)
remove_media(highquality)
return error("Images must be 100x130")
if len(list(Iterator(i))) > 1: price = 1000
@ -417,7 +417,7 @@ def update_marsey(v):
for x in IMAGE_FORMATS:
if path.isfile(f'/asset_submissions/marseys/original/{name}.{x}'):
os.remove(f'/asset_submissions/marseys/original/{name}.{x}')
remove_media(f'/asset_submissions/marseys/original/{name}.{x}')
highquality = f"/asset_submissions/marseys/{name}"
file.save(highquality)
@ -483,7 +483,7 @@ def update_hat(v):
with Image.open(highquality) as i:
if i.width > 100 or i.height > 130:
os.remove(highquality)
remove_media(highquality)
return error("Images must be 100x130")
format = i.format.lower()
@ -491,7 +491,7 @@ def update_hat(v):
for x in IMAGE_FORMATS:
if path.isfile(f'/asset_submissions/hats/original/{name}.{x}'):
os.remove(f'/asset_submissions/hats/original/{name}.{x}')
remove_media(f'/asset_submissions/hats/original/{name}.{x}')
rename(highquality, new_path)

View File

@ -12,7 +12,7 @@ from files.helpers.alerts import *
from files.helpers.config.const import *
from files.helpers.get import *
from files.helpers.mail import *
from files.helpers.media import process_files, process_image
from files.helpers.media import *
from files.helpers.regex import *
from files.helpers.sanitize import *
from files.helpers.sanitize import filter_emojis_only
@ -45,7 +45,7 @@ def settings_personal(v:User):
def remove_background(v):
if v.background:
if v.background.startswith('/images/'):
os.remove(v.background)
remove_media(v.background)
v.background = None
g.db.add(v)
return {"message": "Background removed!"}
@ -69,7 +69,7 @@ def upload_custom_background(v):
if background:
if v.background and v.background.startswith('/images/'):
os.remove(v.background)
remove_media(v.background)
v.background = background
g.db.add(v)
@ -91,7 +91,7 @@ def upload_profile_background(v):
if background:
if v.profile_background and path.isfile(v.profile_background):
os.remove(v.profile_background)
remove_media(v.profile_background)
v.profile_background = background
g.db.add(v)
badge_grant(badge_id=193, user=v)
@ -103,7 +103,7 @@ def upload_profile_background(v):
@auth_required
def delete_profile_background(v):
if v.profile_background:
os.remove(v.profile_background)
remove_media(v.profile_background)
v.profile_background = None
return {"message": "Profile background removed!"}
@ -551,10 +551,10 @@ def settings_images_profile(v):
if not imageurl: abort(400)
if v.highres and '/images/' in v.highres and path.isfile(v.highres):
os.remove(v.highres)
remove_media(v.highres)
if v.profileurl and '/images/' in v.profileurl and path.isfile(v.profileurl):
os.remove(v.profileurl)
remove_media(v.profileurl)
v.highres = highres
v.profileurl = imageurl
@ -584,7 +584,7 @@ def settings_images_banner(v):
if bannerurl:
if v.bannerurl and '/images/' in v.bannerurl and path.isfile(v.bannerurl):
os.remove(v.bannerurl)
remove_media(v.bannerurl)
v.bannerurl = bannerurl
g.db.add(v)
@ -751,11 +751,11 @@ def settings_song_change_mp3(v):
size = os.stat(name).st_size
if size > 8 * 1024 * 1024:
os.remove(name)
remove_media(name)
return redirect("/settings/personal?error=MP3 file must be smaller than 8MB")
if path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User).filter_by(song=v.song).count() == 1:
os.remove(f"/songs/{v.song}.mp3")
remove_media(f"/songs/{v.song}.mp3")
v.song = song
g.db.add(v)
@ -768,7 +768,7 @@ def _change_song_youtube(vid, id):
v = db.get(User, vid)
if v.song and path.isfile(f"/songs/{v.song}.mp3") and db.query(User).filter_by(song=v.song).count() == 1:
os.remove(f"/songs/{v.song}.mp3")
remove_media(f"/songs/{v.song}.mp3")
ydl_opts = {
'cookiefile': '/cookies',
@ -809,7 +809,7 @@ def settings_song_change(v):
if song == "" and v.song:
if path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User).filter_by(song=v.song).count() == 1:
os.remove(f"/songs/{v.song}.mp3")
remove_media(f"/songs/{v.song}.mp3")
v.song = None
g.db.add(v)
return redirect("/settings/personal?msg=Profile Anthem successfully removed!")

View File

@ -2,7 +2,7 @@ import os
from shutil import copyfile
from sqlalchemy import func
from files.helpers.media import process_files
from files.helpers.media import *
import files.helpers.stats as statshelper
from files.classes.award import AWARDS

View File

@ -541,7 +541,7 @@ def delete_sub_banner(v:User, sub:str, index:int):
abort(404, f'Banner not found (banner index {index} is not between 0 and {len(sub.bannerurls)})')
banner = sub.bannerurls[index]
try:
os.remove(banner)
remove_media(banner)
except FileNotFoundError:
pass
del sub.bannerurls[index]
@ -567,7 +567,7 @@ def delete_all_sub_banners(v:User, sub:str):
if v.shadowbanned: return redirect(f'/h/{sub}/settings')
for banner in sub.banner_urls:
try:
os.remove(banner)
remove_media(banner)
except FileNotFoundError:
pass
sub.bannerurls = []
@ -602,7 +602,7 @@ def sub_sidebar(v:User, sub):
if sidebarurl:
if sub.sidebarurl:
os.remove(sub.sidebarurl)
remove_media(sub.sidebarurl)
sub.sidebarurl = sidebarurl
g.db.add(sub)
@ -634,7 +634,7 @@ def sub_marsey(v:User, sub):
if marseyurl:
if sub.marseyurl:
os.remove(sub.marseyurl)
remove_media(sub.marseyurl)
sub.marseyurl = marseyurl
g.db.add(sub)