34 lines
1 KiB
Python
34 lines
1 KiB
Python
|
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
|