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

View File

@@ -3,7 +3,7 @@ import shutil
from typing import Annotated, Any, List, Optional
import aiofiles
from aiopath import AsyncPath
from anyio import Path as AsyncPath
from fastapi import APIRouter, Depends, Header, HTTPException
from fastapi.concurrency import run_in_threadpool
from starlette import status

View File

@@ -9,7 +9,7 @@ from typing import Optional, Union, Annotated
import aiofiles
import pillow_avif # noqa 用于自动注册AVIF支持
from PIL import Image
from aiopath import AsyncPath
from anyio import Path as AsyncPath
from app.helper.sites import SitesHelper # noqa # noqa
from fastapi import APIRouter, Body, Depends, HTTPException, Header, Request, Response
from fastapi.responses import StreamingResponse
@@ -84,7 +84,7 @@ async def fetch_image(
# 目前暂不考虑磁盘缓存文件是否过期,后续通过缓存清理机制处理
if cache_path and await cache_path.exists():
try:
async with cache_path.open('rb') as f:
async with aiofiles.open(cache_path, 'rb') as f:
content = await f.read()
etag = HashUtils.md5(content)
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
# 读取历史日志
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:
# 只读取最后100KB的内容
@@ -408,7 +408,7 @@ async def get_logging(request: Request, length: Optional[int] = 50, logfile: Opt
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)
# 记录初始文件大小
@@ -445,7 +445,7 @@ async def get_logging(request: Request, length: Optional[int] = 50, logfile: Opt
return Response(content="日志文件不存在!", media_type="text/plain")
try:
# 使用 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 = "\n".join(text.split("\n")[::-1])

View File

@@ -2,7 +2,8 @@ import gzip
import json
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.responses import PlainTextResponse
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"
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)
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()
if read_content == content:
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")
# 读取文件
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()
data = json.loads(read_content.encode("utf-8"))
return data

View File

@@ -10,7 +10,7 @@ from typing import Optional, Any, Tuple, List, Set, Union, Dict
from fastapi.concurrency import run_in_threadpool
import aiofiles
from aiopath import AsyncPath
from anyio import Path as AsyncPath
from qbittorrentapi import TorrentFilesList
from transmission_rpc import File

View File

@@ -5,7 +5,7 @@ from typing import List, Optional
import aiofiles
import pillow_avif # noqa 用于自动注册AVIF支持
from PIL import Image
from aiopath import AsyncPath
from anyio import Path as AsyncPath
from app.chain import ChainBase
from app.chain.bangumi import BangumiChain

View File

@@ -12,7 +12,7 @@ import io
import aiofiles
import aioshutil
import httpx
from aiopath import AsyncPath
from anyio import Path as AsyncPath
from packaging.specifiers import SpecifierSet, InvalidSpecifier
from packaging.version import Version, InvalidVersion
from pkg_resources import Requirement, working_set

View File

@@ -3,7 +3,7 @@ from pathlib import Path
from typing import List, Optional, Set, Union
from urllib.parse import quote, urlparse
from aiopath import AsyncPath
from anyio import Path as AsyncPath
from app.log import logger

View File

@@ -62,7 +62,7 @@ Pinyin2Hanzi~=0.1.1
pywebpush~=2.0.3
python-cookietools==0.0.4
aiofiles~=24.1.0
aiopath~=0.7.7
aiopathlib~=0.6.0
asynctempfile~=0.5.0
aiosqlite~=0.21.0
jieba~=0.42.1