11import hashlib
22import os
33import time
4+ import uuid
45from datetime import datetime , timedelta
56from typing import Any , Optional
67
1516from core .security import INTERNAL_CONFIG_KEYS , generate_jwt_secret
1617from apps .base .models import FileCodes , KeyValue
1718from apps .base .utils import get_expire_info , get_file_path_name
19+ from apps .base .quota import release_storage , reserve_storage
1820from fastapi import HTTPException
1921from core .settings import data_root
2022from core .utils import get_now , hash_password , is_password_hashed
@@ -1472,25 +1474,36 @@ async def share_local_file(self, item):
14721474 if not await local_file .exists ():
14731475 raise HTTPException (status_code = 404 , detail = "文件不存在" )
14741476
1475- text = await local_file .read ()
1476- expired_at , expired_count , used_count , code = await get_expire_info (
1477- item .expire_value , item .expire_style
1478- )
1479- path , suffix , prefix , uuid_file_name , save_path = await get_file_path_name (item )
1480-
1481- await self .file_storage .save_file (text , save_path )
1482-
1483- await FileCodes .create (
1484- code = code ,
1485- prefix = prefix ,
1486- suffix = suffix ,
1487- uuid_file_name = uuid_file_name ,
1488- file_path = path ,
1489- size = local_file .size ,
1490- expired_at = expired_at ,
1491- expired_count = expired_count ,
1492- used_count = used_count ,
1493- )
1477+ reservation_token = f"local:{ uuid .uuid4 ().hex } "
1478+ await reserve_storage (reservation_token , local_file .size , ttl_seconds = 3600 )
1479+ try :
1480+ text = await local_file .read ()
1481+ expired_at , expired_count , used_count , code = await get_expire_info (
1482+ item .expire_value , item .expire_style
1483+ )
1484+ path , suffix , prefix , uuid_file_name , save_path = await get_file_path_name (
1485+ item
1486+ )
1487+ await self .file_storage .save_file (text , save_path )
1488+ try :
1489+ await FileCodes .create (
1490+ code = code ,
1491+ prefix = prefix ,
1492+ suffix = suffix ,
1493+ uuid_file_name = uuid_file_name ,
1494+ file_path = path ,
1495+ size = local_file .size ,
1496+ expired_at = expired_at ,
1497+ expired_count = expired_count ,
1498+ used_count = used_count ,
1499+ )
1500+ except Exception :
1501+ await self .file_storage .delete_file (
1502+ FileCodes (file_path = path , uuid_file_name = uuid_file_name )
1503+ )
1504+ raise
1505+ finally :
1506+ await release_storage (reservation_token )
14941507
14951508 return {
14961509 "code" : code ,
@@ -1512,6 +1525,7 @@ class ConfigService:
15121525 "serverPort" ,
15131526 "serverWorkers" ,
15141527 "showAdminAddr" ,
1528+ "storageLimit" ,
15151529 "uploadCount" ,
15161530 "uploadMinute" ,
15171531 "uploadSize" ,
@@ -1576,6 +1590,12 @@ async def update_config(self, data: dict):
15761590 )
15771591 next_config ["adminSessionExpire" ] = session_expire
15781592
1593+ if int (next_config .get ("storageLimit" , 0 )) < 0 :
1594+ raise HTTPException (
1595+ status_code = 400 ,
1596+ detail = "storageLimit 不能小于 0" ,
1597+ )
1598+
15791599 if admin_password_changed :
15801600 next_config ["jwt_secret" ] = generate_jwt_secret ()
15811601
0 commit comments