diff options
| -rw-r--r-- | Dockerfile | 1 | ||||
| -rw-r--r-- | app.py | 27 | ||||
| -rw-r--r-- | templates/file_list.html | 48 |
3 files changed, 74 insertions, 2 deletions
@@ -12,6 +12,7 @@ RUN apt-get update && \ WORKDIR /app +COPY templates/file_list.html ./templates/ COPY requirements.txt . COPY app.py . COPY gunicorn_config.py . @@ -1,14 +1,16 @@ import os import re import uuid -from flask import Flask, request, jsonify, send_from_directory +from flask import Flask, request, jsonify, send_from_directory, render_template import datetime from flasgger import Swagger, swag_from -app = Flask(__name__) +app = Flask(__name__, template_folder='templates') swagger = Swagger(app) app.config['UPLOAD_DIRECTORY'] = os.environ.get('UPLOAD_DIRECTORY', '/uploads') app.config['MAX_CONTENT_LENGTH'] = int(os.environ.get('MAX_CONTENT_LENGTH', '5')) * 1024 * 1024 # in MB +app.config['ENABLE_WEBSERVER'] = os.getenv('ENABLE_WEBSERVER', 'True').lower() == 'true' + VALID_FILENAME_REGEX = r'^[a-zA-Z0-9\-_\.]+$' @@ -17,6 +19,27 @@ AUTH_TOKEN = os.environ.get('AUTH_TOKEN', 'myuploadtoken') def is_valid_filename(filename): return bool(re.match(VALID_FILENAME_REGEX, filename)) +if app.config['ENABLE_WEBSERVER']: + @app.route('/', methods=['GET']) + def file_list(): + """ + Endpoint for displaying a list of files in the upload directory. + """ + files = [] + for filename in os.listdir(app.config['UPLOAD_DIRECTORY']): + file_path = os.path.join(app.config['UPLOAD_DIRECTORY'], filename) + if os.path.isfile(file_path): + stats = os.stat(file_path) + size = stats.st_size + last_modified = datetime.datetime.fromtimestamp(stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S') + files.append({ + 'name': filename, + 'size': size, + 'last_modified': last_modified + }) + + return render_template('file_list.html', files=files) + @app.route('/health', methods=['GET']) def health_check(): """ diff --git a/templates/file_list.html b/templates/file_list.html new file mode 100644 index 0000000..bfb8f2c --- /dev/null +++ b/templates/file_list.html @@ -0,0 +1,48 @@ +<!doctype html> +<html> +<head> + <title>File List</title> + <style> + table { + border-collapse: collapse; + width: 100%; + } + + th, td { + text-align: left; + padding: 8px; + border-bottom: 1px solid #ddd; + } + + tr:hover { + background-color: #f5f5f5; + } + + th { + background-color: #4CAF50; + color: white; + } + </style> +</head> +<body> + <h1>File List</h1> + <table> + <thead> + <tr> + <th>Name</th> + <th>Size (KB)</th> + <th>Last Modified</th> + </tr> + </thead> + <tbody> + {% for file in files %} + <tr> + <td><a href="{{ url_for('download_file', filename=file.name) }}">{{ file.name }}</a> + <td>{{ '%.2f' % (file.size / 1024) }} KB</td> + <td>{{ file.last_modified }}</td> + </tr> + {% endfor %} + </tbody> + </table> +</body> +</html> |