35 lines
1.1 KiB
Python
Executable File
35 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
from flask import Flask, request, jsonify
|
|
import subprocess
|
|
import base64
|
|
|
|
app = Flask(__name__)
|
|
|
|
UNENCODED_AUTH_TOKEN='PitD5xB+Wq6uH7W7UfPtoJo4F6UfWZ9yLrSKZ0bKg9EGoUrK2W77TEI5Y5x1j4uzluqleOo8TGZq2w=='
|
|
AUTH_TOKEN=base64.b64encode(f"me:{UNENCODED_AUTH_TOKEN}".encode('utf-8')).decode('utf-8')
|
|
|
|
REMOTE_DIRECTORY="margarita:./basket/*"
|
|
LOCAL_DEST="/serve/data/basket"
|
|
PORT=35350
|
|
|
|
@app.route('/requestsync', methods=['POST'])
|
|
def requestsync():
|
|
# Check for auth token
|
|
auth_header = request.headers.get('Authorization')
|
|
if auth_header != f"Basic {AUTH_TOKEN}":
|
|
print(f"Wrong auth token. Was expecting: {AUTH_TOKEN}\nGot: {auth_header}")
|
|
return jsonify({"error": "Unauthorized"}), 403
|
|
|
|
try:
|
|
# Run rsync
|
|
print("Sync requested... running now.")
|
|
#subprocess.run(['rsync', '-ravzL', REMOTE_DIRECTORY, LOCAL_DEST], check=True)
|
|
return jsonify({'status': 'OK'})
|
|
except subprocess.CalledProcessError as e:
|
|
return jsonify({"error": 'rsync failed', 'details': str(e)}), 500
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host='0.0.0.0', port=PORT)
|