Implemented file uploading
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pünkösd Marcell 2020-03-25 04:02:26 +01:00
parent 71d942477a
commit 186c51301d
2 changed files with 37 additions and 2 deletions

View File

@ -22,6 +22,7 @@ app.secret_key = os.environ.get('SECRET_KEY', os.urandom(12))
app.config['MINIO_ENDPOINT'] = os.environ['MINIO_ENDPOINT'] app.config['MINIO_ENDPOINT'] = os.environ['MINIO_ENDPOINT']
app.config['MINIO_ACCESS_KEY'] = os.environ['MINIO_ACCESS_KEY'] app.config['MINIO_ACCESS_KEY'] = os.environ['MINIO_ACCESS_KEY']
app.config['MINIO_SECRET_KEY'] = os.environ['MINIO_SECRET_KEY'] app.config['MINIO_SECRET_KEY'] = os.environ['MINIO_SECRET_KEY']
app.config['MINIO_BUCKET_NAME'] = os.environ['MINIO_BUCKET_NAME']
# register error handlers # register error handlers
register_all_error_handlers(app) register_all_error_handlers(app)

View File

@ -1,10 +1,44 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from flask import jsonify from flask import jsonify, request, abort, current_app
from flask_classful import FlaskView from flask_classful import FlaskView
from minio.error import BucketAlreadyExists, BucketAlreadyOwnedByYou, ResponseError
from marshmallow import ValidationError
from utils import storage from utils import storage
from schemas import DescriptionSchema
class ObjectView(FlaskView): class ObjectView(FlaskView):
description_schema = DescriptionSchema(many=False)
def post(self): def post(self):
return jsonify({}), 200 # get important data from the request
try:
description = self.description_schema.loads(request.form.get('description'))
except ValidationError as e:
abort(400, str(e))
file = request.files['soundFile']
if file.content_type != 'audio/wave':
abort(400, f"{file.content_type} is not audio/wave")
if file.content_length <= 0:
abort(411, f"Content length for soundFile is not a positive integer or missing.")
# create bucket if necessary
try:
storage.connection.make_bucket(current_app.config['MINIO_BUCKET_NAME'])
except BucketAlreadyOwnedByYou as err:
pass
except BucketAlreadyExists as err:
pass
# Everything else should be raised
# poot file into bucket
try:
storage.connection.put_object(current_app.config['MINIO_BUCKET_NAME'], description['tag'], file, file.content_length, content_type=file.content_type)
except ResponseError: # TODO: Check if object already exists... somehow
raise
return jsonify({"status": "200"}), 200 # TODO: 200 should be OK but that would be inconsistent with the errors