diff --git a/app/api/endpoints/system.py b/app/api/endpoints/system.py index 41ea1199..aeaf4ccc 100644 --- a/app/api/endpoints/system.py +++ b/app/api/endpoints/system.py @@ -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)): """ diff --git a/app/core/config.py b/app/core/config.py index 9517e589..86fb5ad6 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -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", diff --git a/app/modules/qbittorrent/qbittorrent.py b/app/modules/qbittorrent/qbittorrent.py index 18581a49..f85bbb3f 100644 --- a/app/modules/qbittorrent/qbittorrent.py +++ b/app/modules/qbittorrent/qbittorrent.py @@ -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, diff --git a/app/modules/transmission/transmission.py b/app/modules/transmission/transmission.py index 68bb91e7..288d7283 100644 --- a/app/modules/transmission/transmission.py +++ b/app/modules/transmission/transmission.py @@ -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,