Implemented full local image caching, fix GH-99

This commit is contained in:
krateng 2022-02-26 22:36:55 +01:00
parent 631fd941ec
commit b83eee559f
4 changed files with 27 additions and 1 deletions

View File

@ -149,6 +149,7 @@ malojaconfig = Configuration(
"Technical":{ "Technical":{
"cache_expire_positive":(tp.Integer(), "Image Cache Expiration", 300, "Days until images are refetched"), "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"), "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.") "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":{ "Fluff":{

View File

@ -10,6 +10,9 @@ import os
import urllib import urllib
import random import random
import base64 import base64
import requests
import datauri
import io
from threading import Thread, Timer from threading import Thread, Timer
import re import re
import datetime import datetime
@ -72,6 +75,17 @@ def remove_image_from_cache(id,table):
) )
result = conn.execute(op) 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): def get_track_image(track=None,track_id=None,fast=False):
if track_id is None: if track_id is None:
@ -108,6 +122,10 @@ def get_track_image(track=None,track_id=None,fast=False):
# third party # third party
result = thirdparty.get_image_track_all((artists,title)) result = thirdparty.get_image_track_all((artists,title))
# dl image and proxy
result = dl_image(result)
set_image_in_cache(track_id,'tracks',result) set_image_in_cache(track_id,'tracks',result)
if result is not None: return result if result is not None: return result
for a in artists: for a in artists:
@ -148,6 +166,10 @@ def get_artist_image(artist=None,artist_id=None,fast=False):
# third party # third party
result = thirdparty.get_image_artist_all(artist) result = thirdparty.get_image_artist_all(artist)
# dl image and proxy
result = dl_image(result)
set_image_in_cache(artist_id,'artists',result) set_image_in_cache(artist_id,'artists',result)
if result is not None: return result if result is not None: return result
return "" return ""

View File

@ -29,7 +29,8 @@ dependencies = [
"lru-dict>=1.1.6", "lru-dict>=1.1.6",
"css_html_js_minify>=2.5.5", "css_html_js_minify>=2.5.5",
"psutil>=5.8.0", "psutil>=5.8.0",
"sqlalchemy>=1.4" "sqlalchemy>=1.4",
"python-datauri>=1.1.0"
] ]
[project.scripts] [project.scripts]

View File

@ -7,3 +7,5 @@ jinja2>=2.11
lru-dict>=1.1.6 lru-dict>=1.1.6
css_html_js_minify>=2.5.5 css_html_js_minify>=2.5.5
psutil>=5.8.0 psutil>=5.8.0
sqlalchemy>=1.4
python-datauri>=1.1.0