Skip to content

Commit bfbdd5b

Browse files
committed
fix: debug and improve backup
fix: fix an authentication issue fix: fix path combination issue
1 parent f0a3628 commit bfbdd5b

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/pelit/plib/route_tool.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def authenticate(cfg: dict[str, Any]) -> bool:
2222
token = token[7:]
2323

2424
# 验证密钥,可用 PELIT_AUTH 环境变量或配置文件
25-
if 'hashed' in cfg['auth'] and not 'from_env' in cfg['auth']:
25+
if 'PELIT_AUTH' in os.environ and 'from_env' in cfg['auth'] and cfg['auth']['from_env']:
26+
token_set: str = os.environ['PELIT_AUTH']
27+
return token_set.upper() == token.upper()
28+
elif 'hashed' in cfg['auth']:
2629
token_set: str = cfg['auth']['hashed']
2730
return token_set.upper() == \
2831
hashlib.sha256(token.encode('utf-8')).hexdigest().upper()
29-
elif 'PELIT_AUTH' in os.environ and 'from_env' in cfg['auth']:
30-
token_set: str = os.environ['PELIT_AUTH']
31-
return token_set.upper() == token.upper()
3232
else:
3333
return False
3434

@@ -109,8 +109,9 @@ def backup_to_file(path: Path, file: Path) -> None:
109109
path: 需要备份的目录
110110
file: 备份保存的文件
111111
"""
112+
print(path, file)
112113

113114
try:
114-
shutil.make_archive(str(path), 'gztar', str(file))
115+
shutil.make_archive(str(file), 'gztar', str(path))
115116
except Exception:
116117
pass

src/pelit/route.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _delete(directory: str, file: str) -> tuple[Response, int]:
174174

175175
@main_route.route('/list', methods=['GET'])
176176
@main_route.route('/list/<directory>', methods=['GET'])
177-
def _list(directory: str = "/") -> tuple[Response, int]:
177+
def _list(directory: str = "") -> tuple[Response, int]:
178178
"""
179179
列出指定目录下所有目录
180180
@@ -282,7 +282,7 @@ def _retrieve(directory: str, file: str) -> tuple[Response, int]:
282282

283283
@main_route.route('/backup', methods=['GET'])
284284
@main_route.route('/backup/<directory>', methods=['GET'])
285-
def _backup(directory: str = '/') -> tuple[Response, int]:
285+
def _backup(directory: str = '') -> tuple[Response, int]:
286286
"""
287287
创建一个备份任务,具体方式在配置文件中指定
288288
@@ -291,18 +291,29 @@ def _backup(directory: str = '/') -> tuple[Response, int]:
291291
"""
292292
info_head = f"{request.remote_addr} {request.method} {request.path}"
293293

294-
bak_name = generate_file_name(cfg['storage']['path'], '.tar.gz')
295-
bak_path = Path(cfg['storage']['path']) / (bak_name + '.tar.gz')
296-
bak_url = join_url(cfg['network']['base_url'], bak_name + '.tar.gz')
294+
if not authenticate(cfg):
295+
lg.warn(f"{info_head} 401: 认证失败")
296+
return jsonify({
297+
"success": False,
298+
"message": "认证失败"
299+
}), 401
300+
301+
bak_name = generate_file_name(Path(cfg['storage']['path']), '.tar.gz')
302+
bak_path = Path(cfg['storage']['path']) / bak_name
303+
bak_url = join_url(
304+
cfg['network']['base_url'] if 'network' in cfg['network'] else '',
305+
bak_name + '.tar.gz')
297306

298307
path = Path(cfg['storage']['path']) / directory
299308

300309
# 创建新进程压缩备份
301310
# FIXME: 由于是多进程的,这个任务失败了也不会有表示
311+
# FIXME: 备份会包括所有之前所有的 .tar.gz
302312
p = Process(target=backup_to_file, args=(path, bak_path))
303313
p.start()
304314

305315
lg.info(f"{info_head} 200 备份任务创建成功")
316+
lg.info(f"位置:{bak_path}.tar.gz")
306317
return jsonify({
307318
"success": True,
308319
"url": bak_url,

0 commit comments

Comments
 (0)