Implemented the REST of the endpoints
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
186c51301d
commit
9010db211e
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from flask import jsonify, request, abort, current_app
|
from flask import jsonify, request, abort, current_app, Response
|
||||||
from flask_classful import FlaskView
|
from flask_classful import FlaskView
|
||||||
from minio.error import BucketAlreadyExists, BucketAlreadyOwnedByYou, ResponseError
|
from minio.error import BucketAlreadyExists, BucketAlreadyOwnedByYou, ResponseError, NoSuchKey
|
||||||
from marshmallow import ValidationError
|
from marshmallow import ValidationError
|
||||||
from utils import storage
|
from utils import storage
|
||||||
from schemas import DescriptionSchema
|
from schemas import DescriptionSchema
|
||||||
@ -10,6 +10,15 @@ from schemas import DescriptionSchema
|
|||||||
class ObjectView(FlaskView):
|
class ObjectView(FlaskView):
|
||||||
description_schema = DescriptionSchema(many=False)
|
description_schema = DescriptionSchema(many=False)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _check_existance(tag: str) -> bool: # ez aztán igen
|
||||||
|
try:
|
||||||
|
storage.connection.stat_object(current_app.config['MINIO_BUCKET_NAME'], tag)
|
||||||
|
except NoSuchKey:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
# get important data from the request
|
# get important data from the request
|
||||||
try:
|
try:
|
||||||
@ -17,6 +26,11 @@ class ObjectView(FlaskView):
|
|||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
abort(400, str(e))
|
abort(400, str(e))
|
||||||
|
|
||||||
|
# check for conflict
|
||||||
|
if self._check_existance(description['tag']):
|
||||||
|
abort(409)
|
||||||
|
|
||||||
|
# get and validate file
|
||||||
file = request.files['soundFile']
|
file = request.files['soundFile']
|
||||||
|
|
||||||
if file.content_type != 'audio/wave':
|
if file.content_type != 'audio/wave':
|
||||||
@ -37,8 +51,31 @@ class ObjectView(FlaskView):
|
|||||||
|
|
||||||
# poot file into bucket
|
# poot file into bucket
|
||||||
try:
|
try:
|
||||||
storage.connection.put_object(current_app.config['MINIO_BUCKET_NAME'], description['tag'], file, file.content_length, content_type=file.content_type)
|
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
|
except ResponseError: # TODO: Check if object already exists... somehow
|
||||||
raise
|
raise
|
||||||
|
|
||||||
return jsonify({"status": "200"}), 200 # TODO: 200 should be OK but that would be inconsistent with the errors
|
return jsonify({"status": "200"}), 200 # TODO: 200 should be OK but that would be inconsistent with the errors
|
||||||
|
|
||||||
|
def get(self, tag: str):
|
||||||
|
|
||||||
|
# TODO: Validate tag
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = storage.connection.get_object(current_app.config['MINIO_BUCKET_NAME'], tag)
|
||||||
|
except NoSuchKey:
|
||||||
|
abort(404)
|
||||||
|
|
||||||
|
return Response(data.stream(), mimetype=data.headers['Content-type'])
|
||||||
|
|
||||||
|
def delete(self, tag: str):
|
||||||
|
|
||||||
|
# TODO: Validate tag
|
||||||
|
|
||||||
|
if not self._check_existance(tag):
|
||||||
|
abort(404)
|
||||||
|
|
||||||
|
storage.connection.remove_object(current_app.config['MINIO_BUCKET_NAME'], tag)
|
||||||
|
|
||||||
|
return jsonify({"status": "200"}), 200
|
||||||
|
Loading…
Reference in New Issue
Block a user