Refuse uploads when saves folder exceeds 1 GB
This commit is contained in:
11
app.py
11
app.py
@@ -22,6 +22,13 @@ def get_hash_path(name):
|
|||||||
return os.path.join(SAVES_DIR, safe_filename(name).replace(".puC", ".hash"))
|
return os.path.join(SAVES_DIR, safe_filename(name).replace(".puC", ".hash"))
|
||||||
|
|
||||||
|
|
||||||
|
QUOTA_BYTES = 1 * 1024 ** 3 # 1 GB
|
||||||
|
|
||||||
|
|
||||||
|
def get_dir_size():
|
||||||
|
return sum(os.path.getsize(os.path.join(SAVES_DIR, f)) for f in os.listdir(SAVES_DIR))
|
||||||
|
|
||||||
|
|
||||||
def check_password(name, req):
|
def check_password(name, req):
|
||||||
hash_path = get_hash_path(name)
|
hash_path = get_hash_path(name)
|
||||||
if not os.path.exists(hash_path):
|
if not os.path.exists(hash_path):
|
||||||
@@ -58,6 +65,10 @@ def store_save(name):
|
|||||||
data = request.get_data()
|
data = request.get_data()
|
||||||
if not data:
|
if not data:
|
||||||
abort(400, "No data")
|
abort(400, "No data")
|
||||||
|
existing = os.path.join(SAVES_DIR, safe_filename(name))
|
||||||
|
existing_size = os.path.getsize(existing) if os.path.exists(existing) else 0
|
||||||
|
if get_dir_size() - existing_size + len(data) > QUOTA_BYTES:
|
||||||
|
abort(507, "Storage quota exceeded")
|
||||||
with open(os.path.join(SAVES_DIR, filename), "wb") as f:
|
with open(os.path.join(SAVES_DIR, filename), "wb") as f:
|
||||||
f.write(data)
|
f.write(data)
|
||||||
ph = request.headers.get("X-Password-Hash", "")
|
ph = request.headers.get("X-Password-Hash", "")
|
||||||
|
|||||||
Reference in New Issue
Block a user