diff --git a/server/app/api/playlist.py b/server/app/api/playlist.py index 0b5a0c33..de44580b 100644 --- a/server/app/api/playlist.py +++ b/server/app/api/playlist.py @@ -1,8 +1,7 @@ """ Contains all the playlist routes. """ -from copy import deepcopy -from typing import List + from flask import Blueprint, request from app import instances, api from app.lib import playlistlib @@ -68,13 +67,46 @@ def add_track_to_playlist(playlist_id: str): def get_single_p_info(playlistid: str): for p in api.PLAYLISTS: if p.playlistid == playlistid: - print(p) tracks = p.get_tracks() return {"info": serializer.Playlist(p), "tracks": tracks} return {"info": {}, "tracks": []} +@playlist_bp.route("/playlist//update", methods=["PUT"]) +def update_playlist(playlistid: str): + image = None + + if "image" in request.files: + image = request.files["image"] + + data = request.form + + print(type(image)) + print(image.content_type) + + playlist = { + "name": str(data.get("name")).strip(), + "description": str(data.get("description").strip()), + "lastUpdated": str(data.get("lastUpdated")), + "image": None, + } + + if image: + playlist["image"] = playlistlib.save_p_image(image, playlistid) + + for p in api.PLAYLISTS: + if p.playlistid == playlistid: + p.update_playlist(playlist) + instances.playlist_instance.update_playlist(playlistid, playlist) + + return { + "data": serializer.Playlist(p), + } + + return {"msg": "Something shady happened"}, 500 + + # @playlist_bp.route("/playlist//info") # def get_playlist_track(playlist_id: str): # tracks = playlistlib.get_playlist_tracks(playlist_id) diff --git a/server/app/db/playlists.py b/server/app/db/playlists.py index b1b32a54..94430be4 100644 --- a/server/app/db/playlists.py +++ b/server/app/db/playlists.py @@ -58,3 +58,12 @@ class Playlists(db.Mongo): """ playlist = self.collection.find_one({"name": name}) return convert_one(playlist) + + def update_playlist(self, playlistid: str, playlist: dict) -> None: + """ + Updates a playlist. + """ + return self.collection.update_one( + {"_id": ObjectId(playlistid)}, + {"$set": playlist}, + ) \ No newline at end of file diff --git a/server/app/lib/playlistlib.py b/server/app/lib/playlistlib.py index 07e86204..feb502ed 100644 --- a/server/app/lib/playlistlib.py +++ b/server/app/lib/playlistlib.py @@ -1,8 +1,15 @@ """ This library contains all the functions related to playlists. """ +import base64 +import io +import os +import random +import string +from PIL import Image, ImageSequence from progress.bar import Bar -from app import api, instances, models, exceptions, helpers +from werkzeug import datastructures +from app import api, instances, models, exceptions, settings from app.lib import trackslib @@ -48,3 +55,28 @@ def create_all_playlists(): api.PLAYLISTS.append(models.Playlist(playlist)) _bar.next() _bar.finish() + + +def save_p_image(file: datastructures.FileStorage, pid: str): + """ + Saves the image of a playlist to the database. + """ + img = Image.open(file) + + 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) + + if file.content_type == "image/gif": + frames = [] + + 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 + + img.save(full_path, "webp") + + return img_path diff --git a/server/app/models.py b/server/app/models.py index 740a53c4..72ef86e3 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -100,10 +100,10 @@ class Playlist: playlistid: str name: str - image: str tracks: List[Track] _pre_tracks: list = field(init=False, repr=False) lastUpdated: int + image: str description: str = "" count: int = 0 """A list of track objects in the playlist""" @@ -112,7 +112,7 @@ class Playlist: self.playlistid = data["_id"]["$oid"] self.name = data["name"] self.description = data["description"] - self.image = "" + self.image = self.create_img_link(data["image"]) self._pre_tracks = data["pre_tracks"] self.tracks = [] self.lastUpdated = data["lastUpdated"] @@ -124,8 +124,14 @@ class Playlist: """ return create_playlist_tracks(self._pre_tracks) + def create_img_link(self, image: str): + if image: + return settings.IMG_PLAYLIST_URI + image + + return settings.IMG_PLAYLIST_URI + "" + def update_count(self): - self.count = len(self.tracks) + self.count = len(self._pre_tracks) def add_track(self, track): if track not in self._pre_tracks: @@ -137,6 +143,14 @@ class Playlist: def update_desc(self, desc): self.description = desc + def update_playlist(self, data: dict): + self.name = data["name"] + self.description = data["description"] + self.lastUpdated = data["lastUpdated"] + + if data["image"]: + self.image = self.create_img_link(data["image"]) + @dataclass class Folder: diff --git a/server/app/prep.py b/server/app/prep.py index 9fdfa920..db1d3b5d 100644 --- a/server/app/prep.py +++ b/server/app/prep.py @@ -13,27 +13,20 @@ def create_config_dir() -> None: _home_dir = os.path.expanduser("~") config_folder = os.path.join(_home_dir, settings.CONFIG_FOLDER) + print(config_folder) dirs = [ "", "images", os.path.join("images", "artists"), os.path.join("images", "thumbnails"), + os.path.join("images", "playlists"), ] for _dir in dirs: path = os.path.join(config_folder, _dir) + exists = os.path.exists(path) - try: - os.path.exists(path) - except FileNotFoundError: + if not exists: os.makedirs(path) os.chmod(path, 0o755) - - if _dir == dirs[3]: - default_thumbnails_path = "../setup/default-images/thumbnails" - - try: - os.path.exists(os.path.join(path, "defaults")) - except FileNotFoundError: - pass diff --git a/server/app/settings.py b/server/app/settings.py index eea4b28f..6fbc2494 100644 --- a/server/app/settings.py +++ b/server/app/settings.py @@ -9,10 +9,13 @@ HOME_DIR = os.path.expanduser("~") APP_DIR = os.path.join(HOME_DIR, CONFIG_FOLDER) THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails") + # URL IMG_BASE_URI = "http://127.0.0.1:8900/images/" IMG_ARTIST_URI = IMG_BASE_URI + "artists/" IMG_THUMB_URI = IMG_BASE_URI + "thumbnails/" +IMG_PLAYLIST_URI = IMG_BASE_URI + "playlists/" + # defaults DEFAULT_ARTIST_IMG = IMG_ARTIST_URI + "0.webp"