From 2f4841be7293fa8d8807d460a812b9f946301227 Mon Sep 17 00:00:00 2001 From: t0is Date: Thu, 17 Jul 2025 08:56:13 +0200 Subject: [PATCH] allow zip upload for AC sw --- app.py | 101 +++++++++++++++++++++++++++++++++-------------------- index.html | 64 ++++++++++++++++++++++++++++----- 2 files changed, 120 insertions(+), 45 deletions(-) diff --git a/app.py b/app.py index 22ba5a0..87e30e2 100644 --- a/app.py +++ b/app.py @@ -1,66 +1,93 @@ import os +import re +import tempfile +import subprocess import shutil from flask import Flask, request, render_template, jsonify -import subprocess from dotenv import load_dotenv -# Load environment variables from .env (if available) +# Load environment variables load_dotenv() app = Flask(__name__, static_folder='.', template_folder='.') -# Define target directories (relative to the script’s folder) +# Directories for sys02.lib TARGET_DIRS = [ - os.path.join("software", "."), - os.path.join("software", "VAT_LATEST", "Data"), - os.path.join("software", "VAT_LATEST_DUREMOTE", "Data"), - os.path.join("software", "GENERAL8.0.5.6", "Data") + os.path.join('software', '.'), + os.path.join('software', 'VAT_LATEST', 'Data'), + os.path.join('software', 'VAT_LATEST_DUREMOTE', 'Data'), + os.path.join('software', 'GENERAL8.0.5.6', 'Data') ] +# Archive rules +ARCHIVE_PATTERN = re.compile(r'^AIR 3\.verison July2019 .+\.(zip|rar)$', re.IGNORECASE) +EXTRACT_SUBDIR = 'AIR 3.verison July2019 pouzivat tento' + @app.route('/') def index(): - # Render the HTML file; ensure index.html is in the same folder as app.py return render_template('index.html') -@app.route('/test') -def test(): - # Render the HTML file; ensure index.html is in the same folder as app.py - return render_template('test.html') - @app.route('/upload', methods=['POST']) def upload(): if 'file' not in request.files: return jsonify({'error': 'No file provided'}), 400 file = request.files['file'] - commit_msg = request.form.get('commit_msg', '') - - # Check file name - if file.filename != "sys02.lib": - return jsonify({'error': 'Uploaded file must be named sys02.lib'}), 400 + filename = file.filename.strip() + commit_msg = request.form.get('commit_msg', '').strip() + if not commit_msg: + return jsonify({'error': 'Commit message is required.'}), 400 try: - # Read file content into memory so we can write to multiple locations - file_data = file.read() - for target in TARGET_DIRS: - # Ensure the target directory exists - os.makedirs(target, exist_ok=True) - filepath = os.path.join(target, file.filename) - with open(filepath, 'wb') as f: - f.write(file_data) + # sys02.lib branch + if filename.lower() == 'sys02.lib': + data = file.read() + for target in TARGET_DIRS: + os.makedirs(target, exist_ok=True) + with open(os.path.join(target, 'sys02.lib'), 'wb') as f: + f.write(data) - # Run git commands: stage changes, commit, and push. - # (Make sure that the working directory is already a git repository.) - os.chdir("software") - subprocess.check_call(["git", "add", "."]) - subprocess.check_call(["git", "commit", "-m", commit_msg]) - subprocess.check_call(["git", "push"]) + # ZIP archive branch + elif filename.lower().endswith('.zip'): + if not ARCHIVE_PATTERN.match(filename): + return jsonify({ + 'error': 'Archive name must match "AIR 3.verison July2019 XXXX.zip"' + }), 400 - return jsonify({'message': 'File uploaded and git commit/push successful.'}) + with tempfile.TemporaryDirectory() as tmp: + temp_path = os.path.join(tmp, filename) + file.save(temp_path) + + extract_dir = os.path.join('software', EXTRACT_SUBDIR) + os.makedirs(extract_dir, exist_ok=True) + + shutil.unpack_archive(temp_path, extract_dir) + + else: + return jsonify({ + 'error': 'Unsupported file type. Provide sys02.lib or a valid .zip archive.' + }), 400 + + # Git operations + os.chdir('software') + subprocess.check_call(['git', 'add', '.']) + subprocess.check_call(['git', 'commit', '-m', commit_msg]) + subprocess.check_call(['git', 'push']) + + return jsonify({'message': 'Upload processed and git commit/push successful.'}) + + except shutil.ReadError: + return jsonify({'error': 'Failed to unpack ZIP archive'}), 400 except subprocess.CalledProcessError as e: - return jsonify({'error': 'Git command failed', 'details': str(e)}), 500 + return jsonify({ + 'error': 'Git command failed', + 'details': str(e) + }), 500 except Exception as e: - return jsonify({'error': 'An error occurred', 'details': str(e)}), 500 + return jsonify({ + 'error': 'An unexpected error occurred', + 'details': str(e) + }), 500 -if __name__ == "__main__": - app.run(host="0.0.0.0", port=5001, debug=True) \ No newline at end of file +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5002, debug=True) \ No newline at end of file diff --git a/index.html b/index.html index 5a0d700..a8c9907 100644 --- a/index.html +++ b/index.html @@ -11,14 +11,33 @@
-

Upload sys02.lib

+

Upload sys02.lib or Archive

+ + + +
-
- - + +
+ +
+ + + +
- +
@@ -27,8 +46,37 @@