mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-06-07 03:35:35 +00:00
save images in save items as playlist
This commit is contained in:
parent
f28d3f00bd
commit
5cf188dd38
@ -3,9 +3,10 @@ All playlist-related routes.
|
|||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import pathlib
|
||||||
|
|
||||||
from flask import Blueprint, request
|
from flask import Blueprint, request
|
||||||
from PIL import UnidentifiedImageError
|
from PIL import UnidentifiedImageError, Image
|
||||||
|
|
||||||
from app import models
|
from app import models
|
||||||
from app.db.sqlite.playlists import SQLitePlaylistMethods
|
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.store.tracks import TrackStore
|
||||||
from app.utils.dates import create_new_date, date_string_to_time_passed
|
from app.utils.dates import create_new_date, date_string_to_time_passed
|
||||||
from app.utils.remove_duplicates import remove_duplicates
|
from app.utils.remove_duplicates import remove_duplicates
|
||||||
|
from app.settings import Paths
|
||||||
|
|
||||||
api = Blueprint("playlist", __name__, url_prefix="/")
|
api = Blueprint("playlist", __name__, url_prefix="/")
|
||||||
|
|
||||||
@ -88,14 +90,14 @@ def send_all_playlists():
|
|||||||
return {"data": playlists}
|
return {"data": playlists}
|
||||||
|
|
||||||
|
|
||||||
def insert_playlist(name: str):
|
def insert_playlist(name: str, image: str = None):
|
||||||
playlist = {
|
playlist = {
|
||||||
"image": None,
|
"image": image,
|
||||||
"last_updated": create_new_date(),
|
"last_updated": create_new_date(),
|
||||||
"name": name,
|
"name": name,
|
||||||
"trackhashes": json.dumps([]),
|
"trackhashes": json.dumps([]),
|
||||||
"settings": 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:
|
if image:
|
||||||
try:
|
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":
|
if image.content_type == "image/gif":
|
||||||
playlist["settings"]["has_gif"] = True
|
playlist["settings"]["has_gif"] = True
|
||||||
@ -388,7 +395,7 @@ def save_item_as_playlist():
|
|||||||
itemtype = None
|
itemtype = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
itemhash = data["itemhash"]
|
itemhash: str = data["itemhash"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
itemhash = None
|
itemhash = None
|
||||||
|
|
||||||
@ -403,17 +410,34 @@ def save_item_as_playlist():
|
|||||||
trackhashes = get_album_trackhashes(itemhash)
|
trackhashes = get_album_trackhashes(itemhash)
|
||||||
elif itemtype == "artist":
|
elif itemtype == "artist":
|
||||||
trackhashes = get_artist_trackhashes(itemhash)
|
trackhashes = get_artist_trackhashes(itemhash)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
trackhashes = []
|
trackhashes = []
|
||||||
|
|
||||||
if len(trackhashes) == 0:
|
if len(trackhashes) == 0:
|
||||||
return {"error": "No tracks founds"}, 404
|
return {"error": "No tracks founds"}, 404
|
||||||
|
|
||||||
playlist = insert_playlist(playlist_name)
|
playlist = insert_playlist(playlist_name, image=itemhash + ".webp")
|
||||||
|
|
||||||
if playlist is None:
|
if playlist is None:
|
||||||
return {"error": "Playlist could not be created"}, 500
|
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.add_tracks_to_playlist(playlist.id, trackhashes)
|
||||||
PL.update_last_updated(playlist.id)
|
PL.update_last_updated(playlist.id)
|
||||||
|
|
||||||
|
@ -16,7 +16,9 @@ def create_thumbnail(image: Any, img_path: str) -> str:
|
|||||||
Creates a 250 x 250 thumbnail from a playlist image
|
Creates a 250 x 250 thumbnail from a playlist image
|
||||||
"""
|
"""
|
||||||
thumb_path = "thumb_" + img_path
|
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
|
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
|
Creates a 250 x 250 thumbnail from a playlist image
|
||||||
"""
|
"""
|
||||||
thumb_path = "thumb_" + img_path
|
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 = []
|
frames = []
|
||||||
|
|
||||||
@ -50,20 +54,22 @@ def create_gif_thumbnail(image: Any, img_path: str):
|
|||||||
return thumb_path
|
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.
|
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))
|
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)
|
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 = []
|
frames = []
|
||||||
|
|
||||||
for frame in ImageSequence.Iterator(img):
|
for frame in ImageSequence.Iterator(img):
|
||||||
@ -79,6 +85,7 @@ def save_p_image(file, pid: str):
|
|||||||
|
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# class ValidatePlaylistThumbs:
|
# class ValidatePlaylistThumbs:
|
||||||
# """
|
# """
|
||||||
|
@ -25,8 +25,6 @@ class Playlist:
|
|||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
self.trackhashes = json.loads(str(self.trackhashes))
|
self.trackhashes = json.loads(str(self.trackhashes))
|
||||||
# commentted until we need it 👆
|
|
||||||
|
|
||||||
self.count = len(self.trackhashes)
|
self.count = len(self.trackhashes)
|
||||||
|
|
||||||
if isinstance(self.settings, str):
|
if isinstance(self.settings, str):
|
||||||
@ -44,7 +42,7 @@ class Playlist:
|
|||||||
|
|
||||||
def set_duration(self, duration: int):
|
def set_duration(self, duration: int):
|
||||||
self.duration = duration
|
self.duration = duration
|
||||||
|
|
||||||
def set_count(self, count: int):
|
def set_count(self, count: int):
|
||||||
self.count = count
|
self.count = count
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user