feat:global image cache api

This commit is contained in:
jxxghp
2024-08-28 17:53:06 +08:00
parent bd7fc2d4ff
commit eea8b9a8a6
4 changed files with 29 additions and 1 deletions

View File

@@ -30,7 +30,7 @@ router = APIRouter()
@router.get("/img/{proxy}", summary="图片代理")
def get_img(imgurl: str, proxy: bool = False) -> Any:
def proxy_img(imgurl: str, proxy: bool = False) -> Any:
"""
通过图片代理(使用代理服务器)
"""
@@ -45,6 +45,30 @@ def get_img(imgurl: str, proxy: bool = False) -> Any:
return None
@router.get("/cache/image", summary="图片缓存")
def cache_img(url: str) -> Any:
"""
本地缓存图片文件,可选是否使用代理服务器下载图片
"""
# 获取Url中除域名外的路径
url_path = "/".join(url.split('/')[3:])
# 生成缓存文件路径
cache_path = settings.TEMP_PATH / url_path
# 如果缓存文件不存在,下载图片并保存
if not cache_path.exists():
response = RequestUtils(ua=settings.USER_AGENT).get_res(url=url)
if response:
if not cache_path.parent.exists():
cache_path.parent.mkdir(parents=True)
with open(cache_path, 'wb') as f:
f.write(response.content)
return Response(content=response.content, media_type="image/jpeg")
else:
return None
else:
return Response(content=cache_path.read_bytes(), media_type="image/jpeg")
@router.get("/env", summary="查询系统环境变量", response_model=schemas.Response)
def get_env_setting(_: User = Depends(get_current_active_superuser)):
"""

View File

@@ -158,6 +158,8 @@ class Settings(BaseSettings):
REPO_GITHUB_TOKEN: Optional[str] = None
# 大内存模式
BIG_MEMORY_MODE: bool = False
# 全局图片缓存
GLOBAL_IMAGE_CACHE: bool = False
@validator("SUBSCRIBE_RSS_INTERVAL",
"COOKIECLOUD_INTERVAL",

View File

@@ -64,6 +64,7 @@ class Qbittorrent:
"""
try:
# 登录
logger.info(f"正在连接 qbittorrent{self._host}:{self._port}")
qbt = qbittorrentapi.Client(host=self._host,
port=self._port,
username=self._username,

View File

@@ -46,6 +46,7 @@ class Transmission:
"""
try:
# 登录
logger.info(f"正在连接 transmission{self._host}:{self._port}")
trt = transmission_rpc.Client(host=self._host,
port=self._port,
username=self._username,