2024-10-07 23:16:42 -04:00
|
|
|
import os
|
|
|
|
|
import uuid
|
2025-04-15 07:44:20 -04:00
|
|
|
from functools import wraps
|
2024-10-07 23:16:42 -04:00
|
|
|
from flask import Flask, request, jsonify, send_from_directory
|
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
|
import requests
|
2025-04-15 07:44:20 -04:00
|
|
|
from dotenv import load_dotenv
|
2024-10-07 23:16:42 -04:00
|
|
|
import utils.mLogger as log
|
2025-04-14 22:57:32 -04:00
|
|
|
from controllers.file_controller import upload_file, download_file, delete_file
|
2025-04-14 20:47:26 -04:00
|
|
|
from views import views
|
2024-10-07 23:16:42 -04:00
|
|
|
|
2025-04-15 07:44:20 -04:00
|
|
|
# Load environment variables
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
2024-10-07 23:16:42 -04:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
app.config['UPLOAD_FOLDER'] = 'tmp/uploads'
|
2025-04-15 07:44:20 -04:00
|
|
|
app.config['CLIENT_KEY'] = os.getenv('CLIENT_KEY', 'default-secret-key')
|
2024-10-07 23:16:42 -04:00
|
|
|
|
|
|
|
|
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
|
|
|
|
os.makedirs(app.config['UPLOAD_FOLDER'])
|
|
|
|
|
|
2025-04-15 07:44:20 -04:00
|
|
|
def require_client_key(f):
|
|
|
|
|
@wraps(f)
|
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
|
client_key = request.headers.get('X-Client-Key')
|
|
|
|
|
if not client_key or client_key != app.config['CLIENT_KEY']:
|
|
|
|
|
log.e("Invalid or missing client key")
|
|
|
|
|
return jsonify({'error': 'Invalid or missing client key'}), 401
|
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
return decorated_function
|
|
|
|
|
|
2024-10-07 23:16:42 -04:00
|
|
|
# Route for file uploads
|
2025-04-15 07:44:20 -04:00
|
|
|
app.route('/upload', methods=['POST'])(require_client_key(upload_file))
|
2024-10-07 23:16:42 -04:00
|
|
|
|
2025-04-14 20:47:26 -04:00
|
|
|
# Route for downloading the file
|
|
|
|
|
app.route('/download/<code>', methods=['GET'])(download_file)
|
2024-10-07 23:16:42 -04:00
|
|
|
|
2025-04-14 22:57:32 -04:00
|
|
|
# Route for deleting the file
|
|
|
|
|
app.route('/delete/<code>', methods=['DELETE'])(delete_file)
|
|
|
|
|
|
2025-04-14 20:47:26 -04:00
|
|
|
app.register_blueprint(views)
|
2024-10-07 23:16:42 -04:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
log.i("Running flask in debug...")
|
|
|
|
|
app.run(debug=True, host="0.0.0.0", port=7777)
|