diff --git a/maloja/__init__.py b/maloja/__init__.py index bc4eedc..2b76141 100644 --- a/maloja/__init__.py +++ b/maloja/__init__.py @@ -7,7 +7,7 @@ author = { "email":"maloja@krateng.dev", "github": "krateng" } -version = 2,1,3 +version = 2,1,4 versionstr = ".".join(str(n) for n in version) diff --git a/maloja/data_files/settings/default.ini b/maloja/data_files/settings/default.ini index b245bc7..b3a069f 100644 --- a/maloja/data_files/settings/default.ini +++ b/maloja/data_files/settings/default.ini @@ -16,6 +16,8 @@ SPOTIFY_API_ID = "ASK" SPOTIFY_API_SECRET = "ASK" CACHE_EXPIRE_NEGATIVE = 30 # after how many days negative results should be tried again CACHE_EXPIRE_POSITIVE = 300 # after how many days positive results should be refreshed +THUMBOR_SERVER = None +THUMBOR_SECRET = "" # Can be 'YouTube', 'YouTube Music', 'Google Play Music', 'Spotify', 'Tidal', 'SoundCloud', 'Deezer', 'Amazon Music' # Omit or set to none to disable diff --git a/maloja/globalconf.py b/maloja/globalconf.py new file mode 100644 index 0000000..16764b4 --- /dev/null +++ b/maloja/globalconf.py @@ -0,0 +1,13 @@ +from doreah.settings import get_settings + +THUMBOR_SERVER, THUMBOR_SECRET = get_settings("THUMBOR_SERVER","THUMBOR_SECRET") +try: + USE_THUMBOR = THUMBOR_SERVER is not None and THUMBOR_SECRET is not None + if USE_THUMBOR: + from libthumbor import CryptoURL + THUMBOR_GENERATOR = CryptoURL(key=THUMBOR_SECRET) + OWNURL = get_settings("PUBLIC_URL") + assert OWNURL is not None +except: + USE_THUMBOR = False + log("Thumbor could not be initialized. Is libthumbor installed?") diff --git a/maloja/server.py b/maloja/server.py index f97ea9d..90f1d31 100755 --- a/maloja/server.py +++ b/maloja/server.py @@ -18,6 +18,7 @@ from . import utilities from .utilities import resolveImage from .urihandler import uri_to_internal, remove_identical from . import urihandler +from . import globalconf # doreah toolkit from doreah import settings from doreah.logging import log @@ -120,6 +121,10 @@ def dynamic_image(): @webserver.route("/images/") @webserver.route("/images/") def static_image(pth): + if globalconf.USE_THUMBOR: + return static_file("images/" + pth,root="") + + type = pth.split(".")[-1] small_pth = pth + "-small" if os.path.exists("images/" + small_pth): response = static_file("images/" + small_pth,root="") @@ -141,6 +146,7 @@ def static_image(pth): #response = static_file("images/" + pth,root="") response.set_header("Cache-Control", "public, max-age=86400") + response.set_header("Content-Type", "image/" + type) return response diff --git a/maloja/utilities.py b/maloja/utilities.py index 86f6e07..bee18a0 100644 --- a/maloja/utilities.py +++ b/maloja/utilities.py @@ -16,6 +16,7 @@ from doreah.regular import yearly, daily from .external import api_request_track, api_request_artist from .__init__ import version +from . import globalconf @@ -126,10 +127,20 @@ def consistentRulestate(folder,checksums): ##### +if globalconf.USE_THUMBOR: + def thumborize(url): + if url.startswith("/"): url = globalconf.OWNURL + url + encrypted_url = globalconf.THUMBOR_GENERATOR.generate( + width=300, + height=300, + smart=True, + image_url=url + ) + return globalconf.THUMBOR_SERVER + encrypted_url - - - +else: + def thumborize(url): + return url @@ -297,7 +308,7 @@ def getArtistImage(artist,fast=False): if settings.get_settings("USE_LOCAL_IMAGES"): try: - return local_artist_cache.get(artist) + return thumborize(local_artist_cache.get(artist)) # Local cached image except: # Get all local images, select one if present @@ -306,14 +317,14 @@ def getArtistImage(artist,fast=False): #return random.choice(images) res = random.choice(images) local_artist_cache.add(artist,res) - return urllib.parse.quote(res) + return thumborize(urllib.parse.quote(res)) # if no local images (or setting to not use them) try: # check cache for foreign image result = artist_cache.get(artist) - if result is not None: return result + if result is not None: return thumborize(result) else: return "" # none means non-existence is cached, return empty except: @@ -337,7 +348,7 @@ def getArtistImage(artist,fast=False): #cachedArtists[artist] = result artist_cache.add(artist,result) #cache_artist(artist,result) - if result is not None: return result + if result is not None: return thumborize(result) else: return "" def getTrackImages(trackobjectlist,fast=False):