mirror of
https://github.com/mattintech/simplefileupload-server.git
synced 2026-07-11 11:51:54 +00:00
require client-key for uploads
This commit is contained in:
18
src/app.py
18
src/app.py
@@ -1,20 +1,36 @@
|
||||
import os
|
||||
import uuid
|
||||
from functools import wraps
|
||||
from flask import Flask, request, jsonify, send_from_directory
|
||||
from werkzeug.utils import secure_filename
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
import utils.mLogger as log
|
||||
from controllers.file_controller import upload_file, download_file, delete_file
|
||||
from views import views
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = 'tmp/uploads'
|
||||
app.config['CLIENT_KEY'] = os.getenv('CLIENT_KEY', 'default-secret-key')
|
||||
|
||||
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
||||
os.makedirs(app.config['UPLOAD_FOLDER'])
|
||||
|
||||
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
|
||||
|
||||
# Route for file uploads
|
||||
app.route('/upload', methods=['POST'])(upload_file)
|
||||
app.route('/upload', methods=['POST'])(require_client_key(upload_file))
|
||||
|
||||
# Route for downloading the file
|
||||
app.route('/download/<code>', methods=['GET'])(download_file)
|
||||
|
||||
Reference in New Issue
Block a user