@@ -19,15 +19,19 @@ class UploadProgress(BaseModel):
1919
2020 @classmethod
2121 async def start (cls , upload_key : str , filename : str , track_space : bool = True ):
22- return await AppState ._update_active (upload_key , filename = filename , track_space = track_space )
22+ return await AppState ._update_active (
23+ upload_key , filename = filename , track_space = track_space
24+ )
2325
2426 @classmethod
2527 async def update (cls , upload_key : str , uploaded_bytes : int ):
2628 return await AppState ._update_active (upload_key , uploaded_bytes = uploaded_bytes )
2729
2830 @classmethod
2931 async def finish (cls , upload_key : str , final_size : int ):
30- return await AppState ._update_active (upload_key , uploaded_bytes = final_size , done = True )
32+ return await AppState ._update_active (
33+ upload_key , uploaded_bytes = final_size , done = True
34+ )
3135
3236 @classmethod
3337 async def cancel (cls , upload_key : str ):
@@ -46,8 +50,14 @@ async def get(cls, redis_client: Redis | None = None) -> "AppState":
4650
4751 @classmethod
4852 async def set (cls , state : "AppState" , redis_client : Redis | None = None ) -> None :
49- await cls ._json_set (settings .STATE_REDIS_KEY , state .model_dump (mode = "json" ), redis_client = redis_client )
50- await cls ._publish (settings .STATE_CHANNEL , state .model_dump_json (), redis_client = redis_client )
53+ await cls ._json_set (
54+ settings .STATE_REDIS_KEY ,
55+ state .model_dump (mode = "json" ),
56+ redis_client = redis_client ,
57+ )
58+ await cls ._publish (
59+ settings .STATE_CHANNEL , state .model_dump_json (), redis_client = redis_client
60+ )
5161
5262 @classmethod
5363 async def _update_active (cls , key : str , remove = False , ** kwargs ) -> "AppState" :
@@ -56,18 +66,20 @@ async def _update_active(cls, key: str, remove=False, **kwargs) -> "AppState":
5666 now = datetime .now (timezone .utc )
5767 upload = next ((u for u in s .active_uploads if u .upload_key == key ), None )
5868
59- # 1. Update Space Tracking (if track_space is True)
69+ # Update Space Tracking (if track_space is True)
6070 if upload and upload .track_space :
6171 if remove :
6272 s .total_space_used = max (0 , s .total_space_used - upload .uploaded_bytes )
6373 elif "uploaded_bytes" in kwargs :
64- s .total_space_used += ( kwargs ["uploaded_bytes" ] - upload .uploaded_bytes )
74+ s .total_space_used += kwargs ["uploaded_bytes" ] - upload .uploaded_bytes
6575
66- # 2. Update Active Uploads List
76+ # Update Active Uploads List
6777 if remove or (upload and kwargs .get ("done" ) and not upload .track_space ):
6878 s .active_uploads = [u for u in s .active_uploads if u .upload_key != key ]
6979 elif not upload :
70- s .active_uploads .append (UploadProgress (upload_key = key , last_updated = now , ** kwargs ))
80+ s .active_uploads .append (
81+ UploadProgress (upload_key = key , last_updated = now , ** kwargs )
82+ )
7183 else :
7284 for k , v in kwargs .items ():
7385 setattr (upload , k , v )
@@ -77,7 +89,9 @@ async def _update_active(cls, key: str, remove=False, **kwargs) -> "AppState":
7789 return s
7890
7991 @classmethod
80- async def evict_files (cls , file_keys : list [str ], freed_bytes : int , redis_client : Redis | None = None ) -> None :
92+ async def evict_files (
93+ cls , file_keys : list [str ], freed_bytes : int , redis_client : Redis | None = None
94+ ) -> None :
8195 async def _evict (client : Redis ):
8296 s = await cls .get (redis_client = client )
8397 keys = set (file_keys )
@@ -88,18 +102,21 @@ async def _evict(client: Redis):
88102 if redis_client :
89103 await _evict (redis_client )
90104 else :
91- async with redis .from_url (settings .REDIS_ENDPOINT , encoding = "utf-8" , decode_responses = True ) as client :
105+ async with redis .from_url (
106+ settings .REDIS_ENDPOINT , encoding = "utf-8" , decode_responses = True
107+ ) as client :
92108 await _evict (client )
93109
94110 @classmethod
95111 async def ensure_created (cls ) -> None :
96112 from sqlmodel import select
97113 from app .db import AsyncSessionLocal
98114 from app .models .config import Config
115+
99116 async with AsyncSessionLocal () as session :
100117 config = (await session .exec (select (Config ))).first ()
101118 limit = config .total_storage_limit if config else None
102-
119+
103120 s = await cls .get ()
104121 s .total_available_space = limit
105122 await cls .set (s )
0 commit comments