1111from apps .admin .dependencies import create_token , verify_token
1212from apps .base .models import DeliveryCode , StorageReservation
1313from apps .base .quota import _sql_placeholders
14- from apps .delivery .storage import validate_storage_config
15- from core .settings import settings
14+ from apps .base .setup_wizard import build_public_config
1615from core .utils import get_now
1716
1817TOKEN_TTL = 900
@@ -46,12 +45,10 @@ async def active_code(code_id: int) -> DeliveryCode:
4645
4746async def create_code (data ):
4847 """创建时保存原文,便于管理员后续查看;访客响应仍不提供任何口令列表。"""
49- validate_storage_config (settings .file_storage if data .storage_type == "system" else data .storage_type )
5048 code = data .code or "" .join (secrets .choice ("ABCDEFGHJKLMNPQRSTUVWXYZ23456789" ) for _ in range (16 ))
5149 try :
5250 record = await DeliveryCode .create (
5351 code_value = code , name = data .name ,
54- storage_type = data .storage_type , target_path = data .target_path ,
5552 expires_at = data .expires_at , max_uploads = data .max_uploads ,
5653 note = data .note , tags = data .tags ,
5754 )
@@ -73,10 +70,10 @@ async def code_summary(record, *, include_code=False):
7370 elif record .expires_at <= now :
7471 state = "expired"
7572 return {
76- "id" : record .id , "name" : record .name , "storage_type" : record . storage_type ,
73+ "id" : record .id , "name" : record .name ,
7774 ** ({"code" : record .code_value } if include_code else {}),
7875 "note" : record .note , "tags" : record .tags if isinstance (record .tags , list ) else [],
79- "target_path" : record . target_path , " expires_at" : record .expires_at ,
76+ "expires_at" : record .expires_at ,
8077 "max_uploads" : record .max_uploads , "used_count" : record .used_count ,
8178 "reserved_count" : record .reserved_count , "enabled" : record .enabled ,
8279 "deleted" : record .deleted , "status" : state , "created_at" : record .created_at ,
@@ -98,14 +95,16 @@ async def verify_code(code: str):
9895
9996async def session_summary (record ):
10097 """验证与续期使用相同白名单响应,不泄露路径、存储密钥或管理员授权。"""
98+ # 老版寄件页面仍读取这些字段;值统一取自原公共配置构建函数,不维护独立规则。
99+ config = build_public_config ()
100+ upload_keys = ("upload_size" , "allowed_file_types" , "expire_style" , "max_save_seconds" , "enable_chunk" )
101101 return {
102102 "token" : create_token ({"purpose" : "delivery" , "delivery_id" : record .id ,
103103 "delivery_version" : record .auth_version }, expires_in = TOKEN_TTL ),
104104 "expires_in" : TOKEN_TTL , "name" : record .name ,
105105 "remaining" : max (0 , record .max_uploads - record .used_count - record .reserved_count ),
106- "expires_at" : record .expires_at , "upload_size" : settings .upload_size ,
107- "allowed_file_types" : settings .allowed_file_types , "expire_style" : settings .expire_style ,
108- "max_save_seconds" : settings .max_save_seconds , "enable_chunk" : settings .enable_chunk ,
106+ "expires_at" : record .expires_at ,
107+ ** {key : config [key ] for key in upload_keys },
109108 }
110109
111110
@@ -135,18 +134,6 @@ async def update_code(code_id, data):
135134 max_uploads = changes .get ("max_uploads" , record .max_uploads )
136135 if record .used_count + record .reserved_count > max_uploads :
137136 raise HTTPException (409 , "上传总次数不能小于已使用次数与上传中占用次数之和,请刷新后重试" )
138- storage_requested = "storage_type" in changes or "target_path" in changes
139- storage_type = changes .get ("storage_type" , record .storage_type )
140- target_path = changes .get ("target_path" , record .target_path )
141- if storage_type == "system" :
142- target_path = ""
143- elif not target_path :
144- raise HTTPException (400 , "自定义存储位置必须填写目标目录" )
145- # 整理名称或元数据时不依赖当前后端配置;仅实际改存储设置才重新校验。
146- if storage_requested and (storage_type != record .storage_type or target_path != record .target_path ):
147- validate_storage_config (settings .file_storage if storage_type == "system" else storage_type )
148- changes ["storage_type" ] = storage_type
149- changes ["target_path" ] = target_path
150137 if new_code and new_code != record .code_value :
151138 # 版本由数据库递增,两个改码请求并发时任一旧令牌都不会被错误复用。
152139 changes .update (code_value = new_code , auth_version = True )
@@ -173,12 +160,10 @@ async def update_code(code_id, data):
173160 return await code_summary (record )
174161
175162
176- async def list_codes (* , page = 1 , page_size = 20 , keyword = "" , status = "all" , storage_type = "all" , tag = "" , sort_by = "created_at" , sort_order = "desc" ):
163+ async def list_codes (* , page = 1 , page_size = 20 , keyword = "" , status = "all" , tag = "" , sort_by = "created_at" , sort_order = "desc" ):
177164 """筛选、计数、排序和分页全部在数据库执行,列表不返回口令原文。"""
178165 if status not in {"all" , "active" , "disabled" , "expired" , "exhausted" }:
179166 raise HTTPException (400 , "不支持的寄件码状态筛选" )
180- if storage_type not in {"all" , "system" , "local" , "s3" , "webdav" }:
181- raise HTTPException (400 , "不支持的存储类型筛选" )
182167 if sort_by not in {"created_at" , "expires_at" , "name" , "used_count" , "max_uploads" } or sort_order not in {"asc" , "desc" }:
183168 raise HTTPException (400 , "不支持的排序方式" )
184169 query = DeliveryCode .filter (deleted = False )
@@ -191,8 +176,6 @@ async def list_codes(*, page=1, page_size=20, keyword="", status="all", storage_
191176 query = query .filter (enabled = True , expires_at__lte = now , used_count__lt = F ("max_uploads" ))
192177 elif status == "active" :
193178 query = query .filter (enabled = True , expires_at__gt = now , used_count__lt = F ("max_uploads" ))
194- if storage_type != "all" :
195- query = query .filter (storage_type = storage_type )
196179 if keyword .strip ():
197180 query = query .filter (Q (name__icontains = keyword .strip ()) | Q (note__icontains = keyword .strip ()))
198181 if tag .strip ():
@@ -206,7 +189,7 @@ async def list_codes(*, page=1, page_size=20, keyword="", status="all", storage_
206189 order = ("-" if sort_order == "desc" else "" ) + sort_by
207190 # 只选取管理展示字段,口令原文仅由单独管理接口按需返回。
208191 records = await query .order_by (order , "-id" ).offset ((page - 1 ) * page_size ).limit (page_size ).only (
209- "id" , "name" , "note" , "tags" , "storage_type" , "target_path" , " expires_at" ,
192+ "id" , "name" , "note" , "tags" , "expires_at" ,
210193 "max_uploads" , "used_count" , "reserved_count" , "enabled" , "deleted" , "created_at" ,
211194 )
212195 return {"items" : [await code_summary (record ) for record in records ], "total" : total }
0 commit comments