mirror of
https://github.com/mattintech/simplefileupload-server.git
synced 2026-07-11 15:31:52 +00:00
improving look and feel
This commit is contained in:
43
src/app.py
43
src/app.py
@@ -4,6 +4,8 @@ from flask import Flask, request, jsonify, send_from_directory
|
||||
from werkzeug.utils import secure_filename
|
||||
import requests
|
||||
import utils.mLogger as log
|
||||
from controllers.file_controller import upload_file, download_file
|
||||
from views import views
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = 'tmp/uploads'
|
||||
@@ -11,46 +13,13 @@ app.config['UPLOAD_FOLDER'] = 'tmp/uploads'
|
||||
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
||||
os.makedirs(app.config['UPLOAD_FOLDER'])
|
||||
|
||||
# TinyURL API for generating short URLs
|
||||
def generate_tiny_url(long_url):
|
||||
response = requests.get(f'http://tinyurl.com/api-create.php?url={long_url}')
|
||||
return response.text if response.status_code == 200 else None
|
||||
|
||||
# Route for file uploads
|
||||
@app.route('/upload', methods=['POST'])
|
||||
def upload_file():
|
||||
if 'file' not in request.files:
|
||||
log.e("No File Part")
|
||||
return jsonify({'error': 'No file part'}), 400
|
||||
app.route('/upload', methods=['POST'])(upload_file)
|
||||
|
||||
file = request.files['file']
|
||||
if file.filename == '':
|
||||
log.e("No selected file")
|
||||
return jsonify({'error': 'No selected file'}), 400
|
||||
# Route for downloading the file
|
||||
app.route('/download/<code>', methods=['GET'])(download_file)
|
||||
|
||||
filename = secure_filename(file.filename)
|
||||
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
||||
log.i(f"File upload: {file_path}")
|
||||
file.save(file_path)
|
||||
|
||||
# Generate a download link
|
||||
download_url = request.host_url + 'download/' + filename
|
||||
tiny_url = generate_tiny_url(download_url)
|
||||
|
||||
return jsonify({'tiny_url': tiny_url}), 200
|
||||
|
||||
# Route for downloading the file (one-time)
|
||||
@app.route('/download/<filename>', methods=['GET'])
|
||||
def download_file(filename):
|
||||
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
||||
|
||||
if os.path.exists(file_path):
|
||||
# Serve the file and then delete it
|
||||
response = send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
||||
os.remove(file_path)
|
||||
return response
|
||||
else:
|
||||
return jsonify({'error': 'File not found'}), 404
|
||||
app.register_blueprint(views)
|
||||
|
||||
if __name__ == '__main__':
|
||||
log.i("Running flask in debug...")
|
||||
|
||||
Reference in New Issue
Block a user