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":{
"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":{

View File

@ -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 ""

View File

@ -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]

View File

@ -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