Files
temp-file-trans/app.py

99 lines
3.2 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
from database import init_db, add_file, get_file, delete_file, cleanup_expired
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():
return render_template('index.html', expiry_options=EXPIRY_OPTIONS)
@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
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)
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
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)
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
return jsonify({
'id': row['id'],
'filename': row['filename'],
'filesize': row['filesize'],
'created_at': row['created_at'],
'expires_at': row['expires_at']
})
@app.route('/download/<file_id>')
def serve_file(file_id):
row = get_file(file_id)
if not row:
abort(404)
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)