fix deleting media

pull/142/head
Aevann 2023-03-25 17:07:12 +02:00
parent 8e84a3836f
commit f834514e1a
5 changed files with 18 additions and 21 deletions

View File

@ -21,13 +21,8 @@ from files.helpers.settings import get_setting
from .config.const import *
def remove_media_using_link(path):
if path.startswith(SITE_FULL_IMAGES):
path = path.split(SITE_FULL_IMAGES, 1)[1]
video_prefix = f'https://videos.{SITE}'
if path.startswith(video_prefix):
path = path.split(video_prefix, 1)[1]
if SITE in path:
path = path.split(SITE, 1)[1]
os.remove(path)

View File

@ -199,4 +199,6 @@ numbered_list_regex = re.compile('((\s|^)[0-9]+)\. ', flags=re.A)
comment_link_regex = re.compile("/[0-9]+$", flags=re.A)
media_deletion_regex = re.compile(f"{SITE_FULL}\/(chat_)?(images|videos)\/[0-9]{{11,17}}\.(webp|{video_regex_extensions})", flags=re.A)
image_link_regex = re.compile(f"https://(i\.)?{SITE}\/(chat_)?images\/[0-9]{{11,17}}\.webp", flags=re.A)
video_link_regex = re.compile(f"https://(video\.)?{SITE}\/videos\/[0-9]{{11,17}}\.({video_regex_extensions})", flags=re.A)

View File

@ -1787,15 +1787,15 @@ def delete_media_post(v):
if not url:
return render_template("admin/delete_media.html", v=v, url=url, error="No url provided!")
if not media_deletion_regex.fullmatch(url):
if not image_link_regex.fullmatch(url) and not video_link_regex.fullmatch(url):
return render_template("admin/delete_media.html", v=v, url=url, error="Invalid url!")
path = url.split(SITE_FULL)[1]
path = url.split(SITE)[1]
if not os.path.isfile(path):
return render_template("admin/delete_media.html", v=v, url=url, error="File not found on the server!")
remove_media_using_link(path)
os.remove(path)
ma=ModAction(
kind="delete_media",

View File

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

View File

@ -749,11 +749,11 @@ def settings_song_change_mp3(v):
size = os.stat(name).st_size
if size > 8 * 1024 * 1024:
remove_media_using_link(name)
os.remove(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:
remove_media_using_link(f"/songs/{v.song}.mp3")
os.remove(f"/songs/{v.song}.mp3")
v.song = song
g.db.add(v)
@ -766,7 +766,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:
remove_media_using_link(f"/songs/{v.song}.mp3")
os.remove(f"/songs/{v.song}.mp3")
ydl_opts = {
'cookiefile': '/cookies',
@ -807,7 +807,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:
remove_media_using_link(f"/songs/{v.song}.mp3")
os.remove(f"/songs/{v.song}.mp3")
v.song = None
g.db.add(v)
return redirect("/settings/personal?msg=Profile Anthem successfully removed!")