From b83eee559f59e30c2b000033a8ee9f10f0279c6a Mon Sep 17 00:00:00 2001 From: krateng Date: Sat, 26 Feb 2022 22:36:55 +0100 Subject: [PATCH] Implemented full local image caching, fix GH-99 --- maloja/globalconf.py | 1 + maloja/utilities/images.py | 22 ++++++++++++++++++++++ pyproject.toml | 3 ++- requirements.txt | 2 ++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/maloja/globalconf.py b/maloja/globalconf.py index 19c76c7..62d48c6 100644 --- a/maloja/globalconf.py +++ b/maloja/globalconf.py @@ -149,6 +149,7 @@ malojaconfig = Configuration( "Technical":{ "cache_expire_positive":(tp.Integer(), "Image Cache Expiration", 300, "Days until images are refetched"), "cache_expire_negative":(tp.Integer(), "Image Cache Negative Expiration", 30, "Days until failed image fetches are reattempted"), + "proxy_images":(tp.Boolean(), "Image Proxy", False, "Whether third party images should be downloaded and served directly by Maloja (instead of just linking their URL)"), "db_max_memory":(tp.Integer(min=0,max=100), "RAM Percentage soft limit", 80, "RAM Usage in percent at which Maloja should no longer increase its database cache.") }, "Fluff":{ diff --git a/maloja/utilities/images.py b/maloja/utilities/images.py index 276dccd..d6950a7 100644 --- a/maloja/utilities/images.py +++ b/maloja/utilities/images.py @@ -10,6 +10,9 @@ import os import urllib import random import base64 +import requests +import datauri +import io from threading import Thread, Timer import re import datetime @@ -72,6 +75,17 @@ def remove_image_from_cache(id,table): ) result = conn.execute(op) +def dl_image(url): + try: + r = requests.get(url) + mime = r.headers.get('content-type','image/jpg') + data = io.BytesIO(r.content).read() + uri = datauri.DataURI.make(mime,charset='ascii',base64=True,data=data) + return uri + except: + raise + return url + def get_track_image(track=None,track_id=None,fast=False): if track_id is None: @@ -108,6 +122,10 @@ def get_track_image(track=None,track_id=None,fast=False): # third party result = thirdparty.get_image_track_all((artists,title)) + + # dl image and proxy + result = dl_image(result) + set_image_in_cache(track_id,'tracks',result) if result is not None: return result for a in artists: @@ -148,6 +166,10 @@ def get_artist_image(artist=None,artist_id=None,fast=False): # third party result = thirdparty.get_image_artist_all(artist) + + # dl image and proxy + result = dl_image(result) + set_image_in_cache(artist_id,'artists',result) if result is not None: return result return "" diff --git a/pyproject.toml b/pyproject.toml index 1fb2029..441be84 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,8 @@ dependencies = [ "lru-dict>=1.1.6", "css_html_js_minify>=2.5.5", "psutil>=5.8.0", - "sqlalchemy>=1.4" + "sqlalchemy>=1.4", + "python-datauri>=1.1.0" ] [project.scripts] diff --git a/requirements.txt b/requirements.txt index ffc101c..540b79e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,5 @@ jinja2>=2.11 lru-dict>=1.1.6 css_html_js_minify>=2.5.5 psutil>=5.8.0 +sqlalchemy>=1.4 +python-datauri>=1.1.0