Integrate nav

- other minor refactors
This commit is contained in:
geoffrey45
2022-04-14 11:30:19 +03:00
parent 90d646d674
commit 85c59b4cba
28 changed files with 266 additions and 141 deletions
-1
View File
@@ -58,7 +58,6 @@ def get_subdirs(foldername: str) -> List[models.Folder]:
if str1 is not None:
subdirs.add(foldername + "/" + str1)
return [create_folder(dir) for dir in subdirs]
+51 -9
View File
@@ -35,8 +35,7 @@ def add_track(playlistid: str, trackid: str):
try:
playlist.add_track(track)
instances.playlist_instance.add_track_to_playlist(
playlistid, track)
instances.playlist_instance.add_track_to_playlist(playlistid, track)
return
except TrackExistsInPlaylist as error:
raise error
@@ -61,6 +60,25 @@ def create_all_playlists():
_bar.next()
_bar.finish()
validate_images()
def create_thumbnail(image: any, img_path: str) -> str:
"""
Creates a 250 x 250 thumbnail from a playlist image
"""
thumb_path = "thumb_" + img_path
full_thumb_path = os.path.join(settings.APP_DIR, "images", "playlists", thumb_path)
aspect_ratio = image.width / image.height
new_w = round(250 * aspect_ratio)
thumb = image.resize((new_w, 250), Image.ANTIALIAS)
thumb.save(full_thumb_path, "webp")
return thumb_path
def save_p_image(file: datastructures.FileStorage, pid: str):
"""
@@ -68,11 +86,11 @@ def save_p_image(file: datastructures.FileStorage, pid: str):
"""
img = Image.open(file)
random_str = "".join(
random.choices(string.ascii_letters + string.digits, k=5))
random_str = "".join(random.choices(string.ascii_letters + string.digits, k=5))
img_path = pid + str(random_str) + ".webp"
full_path = os.path.join(settings.APP_DIR, "images", "playlists", img_path)
full_img_path = os.path.join(settings.APP_DIR, "images", "playlists", img_path)
if file.content_type == "image/gif":
frames = []
@@ -80,9 +98,33 @@ def save_p_image(file: datastructures.FileStorage, pid: str):
for frame in ImageSequence.Iterator(img):
frames.append(frame.copy())
frames[0].save(full_path, save_all=True, append_images=frames[1:])
return img_path
frames[0].save(full_img_path, save_all=True, append_images=frames[1:])
thumb_path = create_thumbnail(img, img_path=img_path)
img.save(full_path, "webp")
return img_path, thumb_path
return img_path
img.save(full_img_path, "webp")
thumb_path = create_thumbnail(img, img_path=img_path)
return img_path, thumb_path
def validate_images():
"""
Removes all unused images in the images/playlists folder.
"""
images = []
for playlist in api.PLAYLISTS:
if playlist.image:
img_path = playlist.image.split("/")[-1]
thumb_path = playlist.thumb.split("/")[-1]
images.append(img_path)
images.append(thumb_path)
p_path = os.path.join(settings.APP_DIR, "images", "playlists")
for image in os.listdir(p_path):
if image not in images:
os.remove(os.path.join(p_path, image))