feat(cloud): harden multi-tenant runtime resources

This commit is contained in:
Junyan Qin
2026-07-29 11:32:26 +08:00
parent 32abbb636f
commit ae85ac2b16
211 changed files with 14963 additions and 1968 deletions
@@ -1,5 +1,6 @@
from __future__ import annotations
import asyncio
import os
import aiofiles
import shutil
@@ -40,10 +41,9 @@ class LocalStorageProvider(provider.StorageProvider):
key: str,
value: bytes,
):
resolved = _safe_resolve(LOCAL_STORAGE_PATH, key)
resolved = await asyncio.to_thread(_safe_resolve, LOCAL_STORAGE_PATH, key)
parent = os.path.dirname(resolved)
if not os.path.exists(parent):
os.makedirs(parent)
await asyncio.to_thread(os.makedirs, parent, exist_ok=True)
async with aiofiles.open(resolved, 'wb') as f:
await f.write(value)
@@ -51,7 +51,7 @@ class LocalStorageProvider(provider.StorageProvider):
self,
key: str,
) -> bytes:
resolved = _safe_resolve(LOCAL_STORAGE_PATH, key)
resolved = await asyncio.to_thread(_safe_resolve, LOCAL_STORAGE_PATH, key)
async with aiofiles.open(resolved, 'rb') as f:
return await f.read()
@@ -59,28 +59,31 @@ class LocalStorageProvider(provider.StorageProvider):
self,
key: str,
) -> bool:
resolved = _safe_resolve(LOCAL_STORAGE_PATH, key)
return os.path.exists(resolved)
resolved = await asyncio.to_thread(_safe_resolve, LOCAL_STORAGE_PATH, key)
return await asyncio.to_thread(os.path.exists, resolved)
async def delete(
self,
key: str,
):
resolved = _safe_resolve(LOCAL_STORAGE_PATH, key)
os.remove(resolved)
resolved = await asyncio.to_thread(_safe_resolve, LOCAL_STORAGE_PATH, key)
await asyncio.to_thread(os.remove, resolved)
async def size(
self,
key: str,
) -> int:
resolved = _safe_resolve(LOCAL_STORAGE_PATH, key)
return os.path.getsize(resolved)
resolved = await asyncio.to_thread(_safe_resolve, LOCAL_STORAGE_PATH, key)
return await asyncio.to_thread(os.path.getsize, resolved)
async def delete_dir_recursive(
self,
dir_path: str,
):
resolved = _safe_resolve(LOCAL_STORAGE_PATH, dir_path)
# 直接删除整个目录
if os.path.exists(resolved):
shutil.rmtree(resolved)
resolved = await asyncio.to_thread(
_safe_resolve,
LOCAL_STORAGE_PATH,
dir_path,
)
if await asyncio.to_thread(os.path.exists, resolved):
await asyncio.to_thread(shutil.rmtree, resolved)