From 5cf188dd38738b0dace58edb7029fab3b4381792 Mon Sep 17 00:00:00 2001 From: mungai-njoroge Date: Tue, 8 Aug 2023 12:12:33 +0300 Subject: [PATCH] save images in save items as playlist --- app/api/playlist.py | 38 +++++++++++++++++++++++++++++++------- app/lib/playlistlib.py | 21 ++++++++++++++------- app/models/playlist.py | 4 +--- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/app/api/playlist.py b/app/api/playlist.py index 165c80a..67f0727 100644 --- a/app/api/playlist.py +++ b/app/api/playlist.py @@ -3,9 +3,10 @@ All playlist-related routes. """ import json from datetime import datetime +import pathlib from flask import Blueprint, request -from PIL import UnidentifiedImageError +from PIL import UnidentifiedImageError, Image from app import models from app.db.sqlite.playlists import SQLitePlaylistMethods @@ -15,6 +16,7 @@ from app.store.albums import AlbumStore from app.store.tracks import TrackStore from app.utils.dates import create_new_date, date_string_to_time_passed from app.utils.remove_duplicates import remove_duplicates +from app.settings import Paths api = Blueprint("playlist", __name__, url_prefix="/") @@ -88,14 +90,14 @@ def send_all_playlists(): return {"data": playlists} -def insert_playlist(name: str): +def insert_playlist(name: str, image: str = None): playlist = { - "image": None, + "image": image, "last_updated": create_new_date(), "name": name, "trackhashes": json.dumps([]), "settings": json.dumps( - {"has_gif": False, "banner_pos": 50, "square_img": False} + {"has_gif": False, "banner_pos": 50, "square_img": True if image else False} ), } @@ -259,7 +261,12 @@ def update_playlist_info(playlistid: str): if image: try: - playlist["image"] = playlistlib.save_p_image(image, playlistid) + pil_image = Image.open(image) + content_type = image.content_type + + playlist["image"] = playlistlib.save_p_image( + pil_image, playlistid, content_type + ) if image.content_type == "image/gif": playlist["settings"]["has_gif"] = True @@ -388,7 +395,7 @@ def save_item_as_playlist(): itemtype = None try: - itemhash = data["itemhash"] + itemhash: str = data["itemhash"] except KeyError: itemhash = None @@ -403,17 +410,34 @@ def save_item_as_playlist(): trackhashes = get_album_trackhashes(itemhash) elif itemtype == "artist": trackhashes = get_artist_trackhashes(itemhash) + else: trackhashes = [] if len(trackhashes) == 0: return {"error": "No tracks founds"}, 404 - playlist = insert_playlist(playlist_name) + playlist = insert_playlist(playlist_name, image=itemhash + ".webp") if playlist is None: return {"error": "Playlist could not be created"}, 500 + if itemtype != "folder": + filename = itemhash + ".webp" + + base_path = ( + Paths.get_artist_img_lg_path() + if itemtype == "artist" + else Paths.get_lg_thumb_path() + ) + img_path = pathlib.Path(base_path + "/" + filename) + + if img_path.exists(): + img = Image.open(img_path) + playlistlib.save_p_image( + img, str(playlist.id), "image/webp", filename=filename + ) + PL.add_tracks_to_playlist(playlist.id, trackhashes) PL.update_last_updated(playlist.id) diff --git a/app/lib/playlistlib.py b/app/lib/playlistlib.py index 2db8040..393adc5 100644 --- a/app/lib/playlistlib.py +++ b/app/lib/playlistlib.py @@ -16,7 +16,9 @@ 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.Paths.get_app_dir(), "images", "playlists", thumb_path) + full_thumb_path = os.path.join( + settings.Paths.get_app_dir(), "images", "playlists", thumb_path + ) aspect_ratio = image.width / image.height @@ -33,7 +35,9 @@ def create_gif_thumbnail(image: Any, img_path: str): Creates a 250 x 250 thumbnail from a playlist image """ thumb_path = "thumb_" + img_path - full_thumb_path = os.path.join(settings.Paths.get_app_dir(), "images", "playlists", thumb_path) + full_thumb_path = os.path.join( + settings.Paths.get_app_dir(), "images", "playlists", thumb_path + ) frames = [] @@ -50,20 +54,22 @@ def create_gif_thumbnail(image: Any, img_path: str): return thumb_path -def save_p_image(file, pid: str): +def save_p_image( + img: Image, pid: str, content_type: str = None, filename: str = None +) -> str: """ Saves a playlist banner image and returns the filepath. """ - img = Image.open(file) - + # img = Image.open(file) random_str = "".join(random.choices(string.ascii_letters + string.digits, k=5)) - filename = pid + str(random_str) + ".webp" + if not filename: + filename = pid + str(random_str) + ".webp" full_img_path = os.path.join(settings.Paths.get_playlist_img_path(), filename) - if file.content_type == "image/gif": + if content_type == "image/gif": frames = [] for frame in ImageSequence.Iterator(img): @@ -79,6 +85,7 @@ def save_p_image(file, pid: str): return filename + # # class ValidatePlaylistThumbs: # """ diff --git a/app/models/playlist.py b/app/models/playlist.py index 9c15bef..21807dd 100644 --- a/app/models/playlist.py +++ b/app/models/playlist.py @@ -25,8 +25,6 @@ class Playlist: def __post_init__(self): self.trackhashes = json.loads(str(self.trackhashes)) - # commentted until we need it 👆 - self.count = len(self.trackhashes) if isinstance(self.settings, str): @@ -44,7 +42,7 @@ class Playlist: def set_duration(self, duration: int): self.duration = duration - + def set_count(self, count: int): self.count = count