72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
import os
|
|
from flask import Flask, request, render_template, send_from_directory, redirect, url_for, flash, jsonify
|
|
import subprocess
|
|
from werkzeug.utils import secure_filename
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = 'your_secret_key_here' # Required for flash messaging
|
|
|
|
INPUT_FOLDER = '/app/input'
|
|
OUTPUT_FOLDER = '/app/output'
|
|
ALLOWED_EXTENSIONS = {'gpx'}
|
|
|
|
app.config['INPUT_FOLDER'] = INPUT_FOLDER
|
|
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
|
|
|
|
def allowed_file(filename):
|
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def upload_file():
|
|
if request.method == 'POST':
|
|
if 'file' not in request.files:
|
|
flash('No file part', 'error')
|
|
return redirect(url_for('upload_file'))
|
|
file = request.files['file']
|
|
if file.filename == '':
|
|
flash('No selected file', 'error')
|
|
return redirect(url_for('upload_file'))
|
|
if file and allowed_file(file.filename):
|
|
filename = secure_filename(file.filename)
|
|
input_path = os.path.join(app.config['INPUT_FOLDER'], filename)
|
|
file.save(input_path)
|
|
|
|
output_filename = f"{os.path.splitext(filename)[0]}.fit"
|
|
output_path = os.path.join(app.config['OUTPUT_FOLDER'], output_filename)
|
|
|
|
subprocess.run(['gpsbabel', '-i', 'gpx', '-f', input_path, '-o', 'garmin_fit', '-F', output_path])
|
|
|
|
flash('File uploaded and converted successfully', 'success')
|
|
return redirect(url_for('upload_file'))
|
|
else:
|
|
flash('Invalid file type. Please upload a .gpx file.', 'error')
|
|
return redirect(url_for('upload_file'))
|
|
|
|
input_files = os.listdir(app.config['INPUT_FOLDER'])
|
|
output_files = os.listdir(app.config['OUTPUT_FOLDER'])
|
|
return render_template('index.html', input_files=input_files, output_files=output_files)
|
|
|
|
@app.route('/download/<filename>')
|
|
def download_file(filename):
|
|
return send_from_directory(app.config['OUTPUT_FOLDER'], filename, as_attachment=True)
|
|
|
|
@app.route('/delete', methods=['POST'])
|
|
def delete_files():
|
|
files_to_delete = request.json.get('files', [])
|
|
folder = request.json.get('folder', '')
|
|
|
|
if folder not in ['input', 'output']:
|
|
return jsonify({'error': 'Invalid folder specified'}), 400
|
|
|
|
target_folder = app.config['INPUT_FOLDER'] if folder == 'input' else app.config['OUTPUT_FOLDER']
|
|
|
|
for filename in files_to_delete:
|
|
file_path = os.path.join(target_folder, filename)
|
|
if os.path.exists(file_path):
|
|
os.remove(file_path)
|
|
|
|
return jsonify({'message': 'Files deleted successfully'}), 200
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, host='0.0.0.0')
|