fixes file handling.
This commit is contained in:
64
app.py
64
app.py
@@ -15,6 +15,11 @@ from datetime import datetime
|
||||
from flask import send_from_directory
|
||||
import configparser
|
||||
|
||||
import zipfile
|
||||
import os
|
||||
from datetime import timedelta
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Load configuration
|
||||
@@ -53,6 +58,26 @@ logger.addHandler(queue_handler)
|
||||
scraping_active = False
|
||||
scraping_thread = None
|
||||
|
||||
|
||||
def create_zip(file_paths, zip_name):
|
||||
zip_path = os.path.join(app.root_path, 'temp', zip_name)
|
||||
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
||||
for file_path in file_paths:
|
||||
arcname = os.path.basename(file_path)
|
||||
zipf.write(file_path, arcname)
|
||||
return zip_path
|
||||
|
||||
def delete_old_zips():
|
||||
temp_dir = os.path.join(app.root_path, 'temp')
|
||||
now = datetime.now()
|
||||
for filename in os.listdir(temp_dir):
|
||||
if filename.endswith('.zip'):
|
||||
file_path = os.path.join(temp_dir, filename)
|
||||
file_time = datetime.fromtimestamp(os.path.getmtime(file_path))
|
||||
if now - file_time > timedelta(hours=1):
|
||||
os.remove(file_path)
|
||||
logger.info(f"Deleted old zip file: {filename}")
|
||||
|
||||
def fetch_faction_data(faction_id):
|
||||
url = f"https://api.torn.com/faction/{faction_id}?selections=&key={API_KEY}"
|
||||
response = requests.get(url)
|
||||
@@ -282,15 +307,48 @@ def download_results():
|
||||
|
||||
return render_template('download_results.html', files=files)
|
||||
|
||||
@app.route('/download_files', methods=['POST'])
|
||||
def download_files():
|
||||
delete_old_zips() # Clean up old zip files
|
||||
|
||||
file_paths = request.json.get('file_paths', [])
|
||||
|
||||
if not file_paths:
|
||||
return jsonify({"error": "No files specified"}), 400
|
||||
|
||||
# Validate and correct file paths
|
||||
valid_file_paths = []
|
||||
for file_path in file_paths:
|
||||
if file_path.startswith('/data/'):
|
||||
corrected_path = file_path.lstrip('/')
|
||||
full_path = os.path.join(app.root_path, corrected_path)
|
||||
if os.path.isfile(full_path):
|
||||
valid_file_paths.append(full_path)
|
||||
elif file_path.startswith('/log/'):
|
||||
corrected_path = file_path.lstrip('/')
|
||||
full_path = os.path.join(app.root_path, corrected_path)
|
||||
if os.path.isfile(full_path):
|
||||
valid_file_paths.append(full_path)
|
||||
|
||||
if not valid_file_paths:
|
||||
return jsonify({"error": "No valid files specified"}), 400
|
||||
|
||||
# Create a unique zip file name
|
||||
zip_name = f"files_{datetime.now().strftime('%Y%m%d%H%M%S')}.zip"
|
||||
zip_path = create_zip(valid_file_paths, zip_name)
|
||||
|
||||
return send_from_directory(directory='temp', path=zip_name, as_attachment=True)
|
||||
|
||||
@app.route('/delete_files', methods=['POST'])
|
||||
def delete_files():
|
||||
file_paths = request.form.getlist('file_paths')
|
||||
file_paths = request.json.get('file_paths', [])
|
||||
|
||||
if not file_paths:
|
||||
return jsonify({"error": "No files specified"}), 400
|
||||
|
||||
errors = []
|
||||
for file_path in file_paths:
|
||||
file_path = os.path.join(app.root_path, file_path.lstrip('/'))
|
||||
if not os.path.isfile(file_path):
|
||||
errors.append({"file": file_path, "error": "File not found"})
|
||||
continue
|
||||
@@ -323,9 +381,9 @@ def get_size(path):
|
||||
def download_data_file(filename):
|
||||
return send_from_directory('data', filename)
|
||||
|
||||
@app.route('/logs/<path:filename>')
|
||||
@app.route('/log/<path:filename>')
|
||||
def download_log_file(filename):
|
||||
return send_from_directory('logs', filename)
|
||||
return send_from_directory('log', filename)
|
||||
|
||||
@app.route('/config/lines_per_page')
|
||||
def get_lines_per_page():
|
||||
|
||||
Reference in New Issue
Block a user