131 lines
4.3 KiB
Python
131 lines
4.3 KiB
Python
import os
|
|
import uuid
|
|
from datetime import datetime
|
|
from flask import Flask, request, render_template, send_file, jsonify, url_for, abort
|
|
from config import UPLOAD_FOLDER, SECRET_KEY, MAX_CONTENT_LENGTH, EXPIRY_OPTIONS, DAILY_TRAFFIC_LIMIT
|
|
|
|
MAX_FILE_SIZE_MB = MAX_CONTENT_LENGTH // (1024 * 1024)
|
|
DAILY_GB = DAILY_TRAFFIC_LIMIT // (1024 * 1024 * 1024)
|
|
from database import init_db, add_file, get_file, delete_file, cleanup_expired, add_upload_traffic, add_download_traffic, get_client_ip, is_traffic_exceeded, get_daily_traffic, get_monthly_stats
|
|
|
|
app = Flask(__name__)
|
|
app.config['SECRET_KEY'] = SECRET_KEY
|
|
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
init_db()
|
|
|
|
@app.route('/')
|
|
def index():
|
|
stats = get_monthly_stats()
|
|
return render_template('index.html',
|
|
expiry_options=EXPIRY_OPTIONS,
|
|
max_file_size_mb=MAX_FILE_SIZE_MB,
|
|
daily_gb=DAILY_GB,
|
|
stats=stats)
|
|
|
|
@app.route('/upload', methods=['POST'])
|
|
def upload():
|
|
if 'file' not in request.files:
|
|
return jsonify({'error': 'No file provided'}), 400
|
|
|
|
file = request.files['file']
|
|
if file.filename == '':
|
|
return jsonify({'error': 'No file selected'}), 400
|
|
|
|
expiry_key = request.form.get('expiry', '24h')
|
|
expiry_seconds = EXPIRY_OPTIONS.get(expiry_key, EXPIRY_OPTIONS['24h'])
|
|
expiry_hours = expiry_seconds // 3600
|
|
|
|
ip = get_client_ip(request)
|
|
content_length = request.content_length or 0
|
|
|
|
if is_traffic_exceeded(ip, content_length, 'upload'):
|
|
return jsonify({'error': 'Daily traffic limit exceeded (20GB)'}), 429
|
|
|
|
file_id = str(uuid.uuid4())
|
|
filename = file.filename
|
|
filepath = os.path.join(UPLOAD_FOLDER, file_id)
|
|
file.save(filepath)
|
|
|
|
filesize = os.path.getsize(filepath)
|
|
add_file(file_id, filename, filepath, filesize, expiry_hours)
|
|
add_upload_traffic(ip, filesize)
|
|
|
|
share_url = url_for('download_file', file_id=file_id, _external=True)
|
|
return jsonify({'id': file_id, 'filename': filename, 'share_url': share_url})
|
|
|
|
@app.route('/api/upload', methods=['POST'])
|
|
def api_upload():
|
|
if 'file' not in request.files:
|
|
return jsonify({'error': 'No file provided'}), 400
|
|
|
|
file = request.files['file']
|
|
if file.filename == '':
|
|
return jsonify({'error': 'No file selected'}), 400
|
|
|
|
expiry_key = request.form.get('expiry', '24h')
|
|
expiry_seconds = EXPIRY_OPTIONS.get(expiry_key, EXPIRY_OPTIONS['24h'])
|
|
expiry_hours = expiry_seconds // 3600
|
|
|
|
ip = get_client_ip(request)
|
|
content_length = request.content_length or 0
|
|
|
|
if is_traffic_exceeded(ip, content_length, 'upload'):
|
|
return jsonify({'error': 'Daily traffic limit exceeded (20GB)'}), 429
|
|
|
|
file_id = str(uuid.uuid4())
|
|
filename = file.filename
|
|
filepath = os.path.join(UPLOAD_FOLDER, file_id)
|
|
file.save(filepath)
|
|
|
|
filesize = os.path.getsize(filepath)
|
|
add_file(file_id, filename, filepath, filesize, expiry_hours)
|
|
add_upload_traffic(ip, filesize)
|
|
|
|
share_url = url_for('download_file', file_id=file_id, _external=True)
|
|
return jsonify({'id': file_id, 'filename': filename, 'filesize': filesize, 'expiry_hours': expiry_hours, 'share_url': share_url})
|
|
|
|
@app.route('/file/<file_id>')
|
|
def download_file(file_id):
|
|
cleanup_expired()
|
|
row = get_file(file_id)
|
|
if not row:
|
|
abort(404)
|
|
return render_template('download.html', file=row)
|
|
|
|
@app.route('/api/file/<file_id>')
|
|
def api_get_file(file_id):
|
|
cleanup_expired()
|
|
row = get_file(file_id)
|
|
if not row:
|
|
return jsonify({'error': 'File not found or expired'}), 404
|
|
|
|
ip = get_client_ip(request)
|
|
upload, download = get_daily_traffic(ip)
|
|
return jsonify({
|
|
'id': row['id'],
|
|
'filename': row['filename'],
|
|
'filesize': row['filesize'],
|
|
'created_at': row['created_at'],
|
|
'expires_at': row['expires_at'],
|
|
'daily_upload': upload,
|
|
'daily_download': download,
|
|
'traffic_limit': DAILY_TRAFFIC_LIMIT
|
|
})
|
|
|
|
@app.route('/download/<file_id>')
|
|
def serve_file(file_id):
|
|
row = get_file(file_id)
|
|
if not row:
|
|
abort(404)
|
|
|
|
ip = get_client_ip(request)
|
|
add_download_traffic(ip, row['filesize'])
|
|
|
|
return send_file(row['filepath'], download_name=row['filename'], as_attachment=True)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000, debug=True)
|