5
5
from fbox .utils import get_now
6
6
from fbox .files .models import Box , File , IPUser
7
7
from fbox .files .choices import StatusChoice
8
+ from fbox .files .storage import storage
8
9
from fbox .cards .models import Card
9
10
10
11
11
12
class BoxDatabaseMixin :
12
13
boxes : dict [str , Box ] = {}
13
14
expired_boxes : list [Box ] = []
14
15
15
- def init_boxes (self ) -> None :
16
- box_data = settings .DATA_ROOT / "box"
16
+ async def init_boxes (self ) -> None :
17
17
logger .info (f"Initialize boxes" )
18
+ box_codes = await storage .get_dir_filenames ("box" )
18
19
19
- for box_path in box_data .iterdir ():
20
- box_code = box_path .name
21
- box_dir = box_data / box_code
22
- box_json = box_dir / "box.json"
23
-
24
- if not box_json .exists ():
25
- shutil .rmtree (box_dir )
20
+ for box_code in box_codes :
21
+ box = await storage .get_box (box_code )
22
+ if box is None :
23
+ await storage .remove_box (box_code )
26
24
continue
27
25
28
- box = Box .parse_file (box_json )
29
-
30
26
if self .check_box_expire (box ):
31
27
logger .debug (f"Box { box .code } expire" )
32
-
33
28
self .expire_box (box )
34
29
continue
35
30
@@ -38,27 +33,19 @@ def init_boxes(self) -> None:
38
33
39
34
logger .info (f"Initialize boxes finishied" )
40
35
41
- def archive_box (self , box : Box ) -> None :
42
- logger .debug (f"Archive box { box .code } " )
43
-
44
- now = get_now ().date ().isoformat ()
45
- current = settings .DATA_ROOT / "box" / box .code
46
- target = settings .LOGS_ROOT / "box" / box .code / now
47
- target .mkdir (parents = True )
48
-
49
- for f in current .iterdir ():
50
- shutil .move (f , target )
51
- current .rmdir ()
52
-
53
36
async def clean_expired_boxes (self ) -> None :
54
- logger .info (f"Box count { len (self .boxes .keys ())} " )
55
- logger .info (f"Clean { len (self .expired_boxes )} box" )
37
+ logger .info (f"Check { len (self .boxes )} box" )
38
+ box_codes = self .boxes .keys ()
39
+ for code in box_codes :
40
+ box = self .boxes [code ]
41
+ if self .check_box_expire (box ):
42
+ self .expire_box (box )
56
43
44
+ logger .info (f"Box count { len (self .boxes )} , expired { len (self .expired_boxes )} " )
57
45
for box in self .expired_boxes :
58
- await asyncio . to_thread ( self . archive_box , box )
46
+ await storage . archive_box ( box )
59
47
60
48
self .expired_boxes .clear ()
61
-
62
49
logger .info (f"Clean box finished" )
63
50
64
51
def check_box_expire (self , box : Box ) -> bool :
@@ -93,14 +80,9 @@ def get_boxes(self, expired: bool) -> list[Box]:
93
80
return self .expired_boxes
94
81
return list (self .boxes .values ())
95
82
96
- def save_box_file (self , box : Box ) -> None :
97
- box_file = settings .DATA_ROOT / "box" / box .code / "box.json"
98
- with open (box_file , "w" ) as f :
99
- f .write (box .json ())
100
-
101
83
async def save_box (self , box : Box ) -> None :
102
84
self .boxes [box .code ] = box
103
- await asyncio . to_thread ( self . save_box_file , box )
85
+ await storage . save_box ( box )
104
86
105
87
def expire_box (self , box : Box ) -> None :
106
88
self .expired_boxes .append (box )
@@ -135,17 +117,19 @@ class CardDatabaseMixin:
135
117
cards : dict [str , Card ] = {}
136
118
expired_cards : list [str ] = []
137
119
138
- def init_cards (self ) -> None :
139
- card_data = settings .DATA_ROOT / "card"
120
+ async def init_cards (self ) -> None :
140
121
logger .info (f"Initialize cards" )
141
122
142
- for card_path in card_data .iterdir ():
143
- card_json = card_data / card_path .name
144
- card = Card .parse_file (card_json )
123
+ card_json_names = await storage .get_dir_filenames ("card" )
124
+
125
+ for card_json_name in card_json_names :
126
+ card_code = card_json_name .split ("." )[0 ]
127
+ card = await storage .get_card (card_code )
128
+ if card is None :
129
+ continue
145
130
146
131
if self .check_card_expire (card ):
147
132
logger .debug (f"Card { card .code } expire" )
148
-
149
133
self .expire_card (card )
150
134
continue
151
135
@@ -158,7 +142,7 @@ def check_card_expire(self, card: Card) -> bool:
158
142
now = int (get_now ().timestamp ())
159
143
160
144
logger .debug (
161
- f"card { card .code } with count { card .count } passed { now - card .created } seconds"
145
+ f"Card { card .code } count { card .count } passed { now - card .created } seconds"
162
146
)
163
147
164
148
if card .created > 0 and (now - card .created ) >= (365 * 24 * 3600 ):
@@ -179,14 +163,8 @@ def get_card(self, code: str) -> Card | None:
179
163
return None
180
164
return card
181
165
182
- def save_card_file (self , card : Card ) -> None :
183
- card_file = settings .DATA_ROOT / "card" / f"{ card .code } .json"
184
- with open (card_file , "w" ) as f :
185
- f .write (card .json ())
186
-
187
166
async def save_card (self , card : Card ) -> None :
188
- self .cards [card .code ] = card
189
- await asyncio .to_thread (self .save_card_file , card )
167
+ await storage .save_card (card )
190
168
191
169
def expire_card (self , card : Card ) -> None :
192
170
self .expired_cards .append (card .code )
@@ -213,7 +191,6 @@ def clean_expire_ip_user(self) -> None:
213
191
f"{ ip_user .ip } error { error_expire } , box { box_expire } , file { file_expire } "
214
192
)
215
193
216
-
217
194
if error_expire and box_expire and file_expire :
218
195
expired .append (ip_user .ip )
219
196
else :
@@ -244,21 +221,11 @@ def save_ip_user(self, ip_user: IPUser) -> None:
244
221
245
222
246
223
class Database (BoxDatabaseMixin , CardDatabaseMixin , IPUserDatabaseMixin ):
247
- def __init__ (self ) -> None :
248
- if not settings .DATA_ROOT .exists ():
249
- settings .DATA_ROOT .mkdir (parents = True )
250
-
251
- box_data = settings .DATA_ROOT / "box"
252
- if not box_data .exists ():
253
- box_data .mkdir (parents = True )
254
- else :
255
- self .init_boxes ()
256
-
257
- card_data = settings .DATA_ROOT / "card"
258
- if not card_data .exists ():
259
- card_data .mkdir (parents = True )
260
- else :
261
- self .init_cards ()
224
+ async def init_db (self ) -> None :
225
+ await storage .init_root ()
226
+
227
+ await self .init_boxes ()
228
+ await self .init_cards ()
262
229
263
230
264
231
db = Database ()
0 commit comments