@@ -19,16 +19,18 @@ async def delete_file(self, file_id: int):
19
19
await self .file_storage .delete_file (file_code )
20
20
await file_code .delete ()
21
21
22
- async def list_files (self , page : int , size : int , keyword : str = '' ):
22
+ async def list_files (self , page : int , size : int , keyword : str = "" ):
23
23
offset = (page - 1 ) * size
24
- files = await FileCodes .filter (prefix__icontains = keyword ).limit (size ).offset (offset )
24
+ files = (
25
+ await FileCodes .filter (prefix__icontains = keyword ).limit (size ).offset (offset )
26
+ )
25
27
total = await FileCodes .filter (prefix__icontains = keyword ).count ()
26
28
return files , total
27
29
28
30
async def download_file (self , file_id : int ):
29
31
file_code = await FileCodes .filter (id = file_id ).first ()
30
32
if not file_code :
31
- raise HTTPException (status_code = 404 , detail = ' 文件不存在' )
33
+ raise HTTPException (status_code = 404 , detail = " 文件不存在" )
32
34
if file_code .text :
33
35
return APIResponse (detail = file_code .text )
34
36
else :
@@ -37,10 +39,12 @@ async def download_file(self, file_id: int):
37
39
async def share_local_file (self , item ):
38
40
local_file = LocalFileClass (item .filename )
39
41
if not await local_file .exists ():
40
- raise HTTPException (status_code = 404 , detail = ' 文件不存在' )
42
+ raise HTTPException (status_code = 404 , detail = " 文件不存在" )
41
43
42
44
text = await local_file .read ()
43
- expired_at , expired_count , used_count , code = await get_expire_info (item .expire_value , item .expire_style )
45
+ expired_at , expired_count , used_count , code = await get_expire_info (
46
+ item .expire_value , item .expire_style
47
+ )
44
48
path , suffix , prefix , uuid_file_name , save_path = await get_file_path_name (item )
45
49
46
50
await self .file_storage .save_file (text , save_path )
@@ -58,8 +62,8 @@ async def share_local_file(self, item):
58
62
)
59
63
60
64
return {
61
- ' code' : code ,
62
- ' name' : local_file .file ,
65
+ " code" : code ,
66
+ " name" : local_file .file ,
63
67
}
64
68
65
69
@@ -68,54 +72,67 @@ def get_config(self):
68
72
return settings .items ()
69
73
70
74
async def update_config (self , data : dict ):
71
- admin_token = data .get (' admin_token' )
72
- if admin_token is None or admin_token == '' :
73
- raise HTTPException (status_code = 400 , detail = ' 管理员密码不能为空' )
75
+ admin_token = data .get (" admin_token" )
76
+ if admin_token is None or admin_token == "" :
77
+ raise HTTPException (status_code = 400 , detail = " 管理员密码不能为空" )
74
78
75
79
for key , value in data .items ():
76
80
if key not in settings .default_config :
77
81
continue
78
- if key in ['errorCount' , 'errorMinute' , 'max_save_seconds' , 'onedrive_proxy' , 'openUpload' , 'port' , 's3_proxy' , 'uploadCount' , 'uploadMinute' , 'uploadSize' ]:
82
+ if key in [
83
+ "errorCount" ,
84
+ "errorMinute" ,
85
+ "max_save_seconds" ,
86
+ "onedrive_proxy" ,
87
+ "openUpload" ,
88
+ "port" ,
89
+ "s3_proxy" ,
90
+ "uploadCount" ,
91
+ "uploadMinute" ,
92
+ "uploadSize" ,
93
+ ]:
79
94
data [key ] = int (value )
80
- elif key in [' opacity' ]:
95
+ elif key in [" opacity" ]:
81
96
data [key ] = float (value )
82
97
else :
83
98
data [key ] = value
84
99
85
- await KeyValue .filter (key = ' settings' ).update (value = data )
100
+ await KeyValue .filter (key = " settings" ).update (value = data )
86
101
for k , v in data .items ():
87
102
settings .__setattr__ (k , v )
88
103
89
104
90
105
class LocalFileService :
91
106
async def list_files (self ):
92
107
files = []
93
- if not os .path .exists (data_root / ' local' ):
94
- os .makedirs (data_root / ' local' )
95
- for file in os .listdir (data_root / ' local' ):
108
+ if not os .path .exists (data_root / " local" ):
109
+ os .makedirs (data_root / " local" )
110
+ for file in os .listdir (data_root / " local" ):
96
111
files .append (LocalFileClass (file ))
97
112
return files
98
113
99
114
async def delete_file (self , filename : str ):
100
115
file = LocalFileClass (filename )
101
116
if await file .exists ():
102
117
await file .delete ()
103
- return ' 删除成功'
104
- raise HTTPException (status_code = 404 , detail = ' 文件不存在' )
118
+ return " 删除成功"
119
+ raise HTTPException (status_code = 404 , detail = " 文件不存在" )
105
120
106
121
107
122
class LocalFileClass :
108
123
def __init__ (self , file ):
109
124
self .file = file
110
- self .path = data_root / 'local' / file
111
- self .ctime = time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime (os .path .getctime (self .path )))
125
+ self .path = data_root / "local" / file
126
+ self .ctime = time .strftime (
127
+ "%Y-%m-%d %H:%M:%S" , time .localtime (os .path .getctime (self .path ))
128
+ )
112
129
self .size = os .path .getsize (self .path )
113
130
114
131
async def read (self ):
115
- return open (self .path , 'rb' )
132
+ return open (self .path , "rb" )
116
133
117
134
async def write (self , data ):
118
- with open (self .path , 'w' ) as f :
135
+ with open (self .path , "w" ) as f :
119
136
f .write (data )
120
137
121
138
async def delete (self ):
0 commit comments