Here's a complete Flask application in a single Python file that functions similarly to bashupload.com, but with additional features like statistics. This application stores uploaded files in RAM and includes all HTML and CSS within the Python file using template strings.
python Copy code
from flask import Flask, request, redirect, url_for, render_template_string, send_file, abort
from werkzeug.utils import secure_filename
import uuid
import io
from datetime import datetime
app = Flask(__name__)
# In-memory storage for files and statistics
files = {}
stats = {
'total_uploads': 0,
'total_downloads': 0,
'current_files': 0,
'total_data_stored': 0 # in bytes
}
# HTML Templates
UPLOAD_PAGE = """
<!doctype html>
<html lang="en">
<head>
<title>Upload File</title>
<style>
{{ style }}
</style>
</head>
<body>
<h1>Upload File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file required>
<input type=submit value=Upload>
</form>
<p><a href="{{ url_for('show_stats') }}">View Statistics</a></p>
</body>
</html>
"""
STATS_PAGE = """
<!doctype html>
<html lang="en">
<head>
<title>Statistics</title>
<style>
{{ style }}
</style>
</head>
<body>
<h1>Statistics</h1>
<ul>
<li>Total uploads: {{ stats.total_uploads }}</li>
<li>Total downloads: {{ stats.total_downloads }}</li>
<li>Current files stored: {{ stats.current_files }}</li>
<li>Total data stored: {{ stats.total_data_stored }} bytes</li>
</ul>
<p><a href="{{ url_for('upload_file') }}">Upload another file</a></p>
</body>
</html>
"""
# CSS Styles
STYLE = """
body {
font-family: Arial, sans-serif;
margin: 30px;
}
h1 {
color: #333;
}
form {
margin-top: 20px;
}
input[type="file"] {
display: block;
margin-bottom: 10px;
}
a {
color: #0066cc;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
"""
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# Handle file upload
uploaded_file = request.files.get('file')
if uploaded_file:
filename = secure_filename(uploaded_file.filename)
file_id = str(uuid.uuid4())
file_data = uploaded_file.read()
files[file_id] = {
'filename': filename,
'data': file_data,
'uploaded_at': datetime.utcnow()
}
# Update stats
stats['total_uploads'] += 1
stats['current_files'] += 1
stats['total_data_stored'] += len(file_data)
# Provide download link
download_url = url_for('download_file', file_id=file_id, _external=True)
return render_template_string("""
<!doctype html>
<html lang="en">
<head>
<title>File Uploaded</title>
<style>{{ style }}</style>
</head>
<body>
<h1>File Uploaded Successfully!</h1>
<p>Your file is available at:</p>
<p><a href="{{ download_url }}">{{ download_url }}</a></p>
<p><a href="{{ url_for('upload_file') }}">Upload another file</a></p>
<p><a href="{{ url_for('show_stats') }}">View Statistics</a></p>
</body>
</html>
""", download_url=download_url, style=STYLE)
# GET request - show upload form
return render_template_string(UPLOAD_PAGE, style=STYLE)
@app.route('/files/<file_id>')
def download_file(file_id):
file_entry = files.get(file_id)
if file_entry:
# Update stats
stats['total_downloads'] += 1
return send_file(
io.BytesIO(file_entry['data']),
attachment_filename=file_entry['filename'],
as_attachment=True
)
else:
abort(404)
@app.route('/stats')
def show_stats():
return render_template_string(STATS_PAGE, stats=stats, style=STYLE)
if __name__ == '__main__':
app.run(debug=True)