This repository has been archived on 2024-11-06. You can view files and clone it, but cannot push or open issues or pull requests.
python-api-server/app.py

34 lines
1 KiB
Python
Raw Normal View History

2023-04-12 16:08:55 +02:00
from flask import Flask, request, jsonify
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 # 5MB limit
@app.route('/upload', methods=['POST'])
def upload_file():
file = request.files['file']
if file:
filename = file.filename
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return 'File uploaded successfully'
else:
return 'No file specified', 400
@app.route('/files', methods=['GET'])
def list_files():
files = []
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
if os.path.isfile(path):
files.append(filename)
return jsonify(files)
if __name__ == '__main__':
app.run()
# curl -X POST -F 'file=@/path/to/file' http://localhost:5000/upload
# curl http://localhost:5000/files