mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-10 05:00:59 +00:00
feat(cloud): harden multi-tenant runtime resources
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user