refactor(download): 移除下载后自动删除逻辑,保留手动删除接口
1. download.py:
- 完全移除 _mark_task_downloaded() 函数(原功能:下载后设置 delete_after_at
触发 janitor 到期自动清理文件)
- 改为仅记录 downloaded_at 时间戳,不再调度自动删除
- 手动 DELETE /task/<id> 接口不受影响(tasks.py:151-211)
2. gasflux.ini:
- successful_task_cleanup_age: 60 → 315360000(10年)
彻底禁止 janitor 的下载后自动清理行为
This commit is contained in:
@ -3,6 +3,7 @@ Download Blueprint
|
||||
Handles file download endpoints.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from flask import Blueprint, send_file, current_app
|
||||
|
||||
@ -10,40 +11,6 @@ from ..shared import _format_response, log_performance, logger
|
||||
from ..auth import require_api_key
|
||||
|
||||
|
||||
def _mark_task_downloaded(task_id):
|
||||
"""Mark task as downloaded and schedule deletion in database."""
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
# Use independent database connection (not from flask.g which may be closed)
|
||||
from ..db import get_db_path as get_config_db_path
|
||||
db_path = get_config_db_path(current_app)
|
||||
|
||||
# Get cleanup age for successful tasks from config (in seconds)
|
||||
successful_task_cleanup_age = current_app.config.get('SUCCESSFUL_TASK_CLEANUP_AGE', 3600)
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(str(db_path), check_same_thread=False)
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
conn.execute("PRAGMA busy_timeout=3000")
|
||||
|
||||
# Update downloaded timestamp and set deletion time based on config
|
||||
conn.execute("""
|
||||
UPDATE tasks
|
||||
SET downloaded_at = datetime('now', '+8 hours'),
|
||||
delete_after_at = datetime('now', '+8 hours', '+' || ? || ' seconds')
|
||||
WHERE task_id = ?
|
||||
""", (successful_task_cleanup_age, task_id))
|
||||
|
||||
conn.commit()
|
||||
logger.info(f"Task {task_id} marked as downloaded, scheduled for deletion in {successful_task_cleanup_age} seconds")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to mark task {task_id} as downloaded: {str(e)}", exc_info=True)
|
||||
finally:
|
||||
if 'conn' in locals():
|
||||
conn.close()
|
||||
|
||||
# Create blueprint
|
||||
download_bp = Blueprint('download', __name__, url_prefix='/download')
|
||||
|
||||
@ -124,12 +91,19 @@ def download_file(filename):
|
||||
file_size = file_path.stat().st_size
|
||||
logger.info(f"Serving file: {filename} ({file_size} bytes)")
|
||||
|
||||
# Mark download immediately before sending file
|
||||
# 记录下载时间(仅时间戳,不设置自动删除)
|
||||
if task_id:
|
||||
try:
|
||||
_mark_task_downloaded(task_id)
|
||||
from ..db import get_db
|
||||
db = get_db()
|
||||
db.execute(
|
||||
"UPDATE tasks SET downloaded_at = datetime('now', '+8 hours') WHERE task_id = ?",
|
||||
(task_id,)
|
||||
)
|
||||
db.commit()
|
||||
logger.info(f"Task {task_id} downloaded at {datetime.now()}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to mark download for task {task_id}: {str(e)}")
|
||||
logger.error(f"Failed to record download for task {task_id}: {str(e)}")
|
||||
|
||||
response = send_file(file_path)
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user