use apipathlib

This commit is contained in:
jxxghp
2025-08-17 09:00:02 +08:00
parent 086b1f1403
commit 1ab2da74b9
8 changed files with 16 additions and 15 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ import shutil
from typing import Annotated, Any, List, Optional from typing import Annotated, Any, List, Optional
import aiofiles import aiofiles
from aiopath import AsyncPath from anyio import Path as AsyncPath
from fastapi import APIRouter, Depends, Header, HTTPException from fastapi import APIRouter, Depends, Header, HTTPException
from fastapi.concurrency import run_in_threadpool from fastapi.concurrency import run_in_threadpool
from starlette import status from starlette import status
+5 -5
View File
@@ -9,7 +9,7 @@ from typing import Optional, Union, Annotated
import aiofiles import aiofiles
import pillow_avif # noqa 用于自动注册AVIF支持 import pillow_avif # noqa 用于自动注册AVIF支持
from PIL import Image from PIL import Image
from aiopath import AsyncPath from anyio import Path as AsyncPath
from app.helper.sites import SitesHelper # noqa # noqa from app.helper.sites import SitesHelper # noqa # noqa
from fastapi import APIRouter, Body, Depends, HTTPException, Header, Request, Response from fastapi import APIRouter, Body, Depends, HTTPException, Header, Request, Response
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
@@ -84,7 +84,7 @@ async def fetch_image(
# 目前暂不考虑磁盘缓存文件是否过期,后续通过缓存清理机制处理 # 目前暂不考虑磁盘缓存文件是否过期,后续通过缓存清理机制处理
if cache_path and await cache_path.exists(): if cache_path and await cache_path.exists():
try: try:
async with cache_path.open('rb') as f: async with aiofiles.open(cache_path, 'rb') as f:
content = await f.read() content = await f.read()
etag = HashUtils.md5(content) etag = HashUtils.md5(content)
headers = RequestUtils.generate_cache_headers(etag, max_age=86400 * 7) headers = RequestUtils.generate_cache_headers(etag, max_age=86400 * 7)
@@ -381,7 +381,7 @@ async def get_logging(request: Request, length: Optional[int] = 50, logfile: Opt
file_size = file_stat.st_size file_size = file_stat.st_size
# 读取历史日志 # 读取历史日志
async with log_path.open(mode="r", encoding="utf-8", errors="ignore") as f: async with aiofiles.open(log_path, mode="r", encoding="utf-8", errors="ignore") as f:
# 优化大文件读取策略 # 优化大文件读取策略
if file_size > 100 * 1024: if file_size > 100 * 1024:
# 只读取最后100KB的内容 # 只读取最后100KB的内容
@@ -408,7 +408,7 @@ async def get_logging(request: Request, length: Optional[int] = 50, logfile: Opt
yield f"data: {line}\n\n" yield f"data: {line}\n\n"
# 实时监听新日志 # 实时监听新日志
async with log_path.open(mode="r", encoding="utf-8", errors="ignore") as f: async with aiofiles.open(log_path, mode="r", encoding="utf-8", errors="ignore") as f:
# 移动文件指针到文件末尾,继续监听新增内容 # 移动文件指针到文件末尾,继续监听新增内容
await f.seek(0, 2) await f.seek(0, 2)
# 记录初始文件大小 # 记录初始文件大小
@@ -445,7 +445,7 @@ async def get_logging(request: Request, length: Optional[int] = 50, logfile: Opt
return Response(content="日志文件不存在!", media_type="text/plain") return Response(content="日志文件不存在!", media_type="text/plain")
try: try:
# 使用 aiofiles 异步读取文件 # 使用 aiofiles 异步读取文件
async with log_path.open(mode="r", encoding="utf-8", errors="ignore") as file: async with aiofiles.open(log_path, mode="r", encoding="utf-8", errors="ignore") as file:
text = await file.read() text = await file.read()
# 倒序输出 # 倒序输出
text = "\n".join(text.split("\n")[::-1]) text = "\n".join(text.split("\n")[::-1])
+5 -4
View File
@@ -2,7 +2,8 @@ import gzip
import json import json
from typing import Annotated, Callable, Any, Dict, Optional from typing import Annotated, Callable, Any, Dict, Optional
from aiopath import AsyncPath import aiofiles
from anyio import Path as AsyncPath
from fastapi import APIRouter, Depends, HTTPException, Path, Request, Response from fastapi import APIRouter, Depends, HTTPException, Path, Request, Response
from fastapi.responses import PlainTextResponse from fastapi.responses import PlainTextResponse
from fastapi.routing import APIRoute from fastapi.routing import APIRoute
@@ -67,9 +68,9 @@ async def update_cookie(req: schemas.CookieData):
""" """
file_path = AsyncPath(settings.COOKIE_PATH) / f"{req.uuid}.json" file_path = AsyncPath(settings.COOKIE_PATH) / f"{req.uuid}.json"
content = json.dumps({"encrypted": req.encrypted}) content = json.dumps({"encrypted": req.encrypted})
async with file_path.open(encoding="utf-8", mode="w") as file: async with aiofiles.open(file_path, encoding="utf-8", mode="w") as file:
await file.write(content) await file.write(content)
async with file_path.open(encoding="utf-8", mode="r") as file: async with aiofiles.open(file_path, encoding="utf-8", mode="r") as file:
read_content = await file.read() read_content = await file.read()
if read_content == content: if read_content == content:
return {"action": "done"} return {"action": "done"}
@@ -88,7 +89,7 @@ async def load_encrypt_data(uuid: str) -> Dict[str, Any]:
raise HTTPException(status_code=404, detail="Item not found") raise HTTPException(status_code=404, detail="Item not found")
# 读取文件 # 读取文件
async with file_path.open(encoding="utf-8", mode="r") as file: async with aiofiles.open(file_path, encoding="utf-8", mode="r") as file:
read_content = await file.read() read_content = await file.read()
data = json.loads(read_content.encode("utf-8")) data = json.loads(read_content.encode("utf-8"))
return data return data
+1 -1
View File
@@ -10,7 +10,7 @@ from typing import Optional, Any, Tuple, List, Set, Union, Dict
from fastapi.concurrency import run_in_threadpool from fastapi.concurrency import run_in_threadpool
import aiofiles import aiofiles
from aiopath import AsyncPath from anyio import Path as AsyncPath
from qbittorrentapi import TorrentFilesList from qbittorrentapi import TorrentFilesList
from transmission_rpc import File from transmission_rpc import File
+1 -1
View File
@@ -5,7 +5,7 @@ from typing import List, Optional
import aiofiles import aiofiles
import pillow_avif # noqa 用于自动注册AVIF支持 import pillow_avif # noqa 用于自动注册AVIF支持
from PIL import Image from PIL import Image
from aiopath import AsyncPath from anyio import Path as AsyncPath
from app.chain import ChainBase from app.chain import ChainBase
from app.chain.bangumi import BangumiChain from app.chain.bangumi import BangumiChain
+1 -1
View File
@@ -12,7 +12,7 @@ import io
import aiofiles import aiofiles
import aioshutil import aioshutil
import httpx import httpx
from aiopath import AsyncPath from anyio import Path as AsyncPath
from packaging.specifiers import SpecifierSet, InvalidSpecifier from packaging.specifiers import SpecifierSet, InvalidSpecifier
from packaging.version import Version, InvalidVersion from packaging.version import Version, InvalidVersion
from pkg_resources import Requirement, working_set from pkg_resources import Requirement, working_set
+1 -1
View File
@@ -3,7 +3,7 @@ from pathlib import Path
from typing import List, Optional, Set, Union from typing import List, Optional, Set, Union
from urllib.parse import quote, urlparse from urllib.parse import quote, urlparse
from aiopath import AsyncPath from anyio import Path as AsyncPath
from app.log import logger from app.log import logger
+1 -1
View File
@@ -62,7 +62,7 @@ Pinyin2Hanzi~=0.1.1
pywebpush~=2.0.3 pywebpush~=2.0.3
python-cookietools==0.0.4 python-cookietools==0.0.4
aiofiles~=24.1.0 aiofiles~=24.1.0
aiopath~=0.7.7 aiopathlib~=0.6.0
asynctempfile~=0.5.0 asynctempfile~=0.5.0
aiosqlite~=0.21.0 aiosqlite~=0.21.0
jieba~=0.42.1 jieba~=0.42.1