Commit 02b910bc authored by Fence's avatar Fence 🌈

some api fixes

parent 955d493f
......@@ -13,7 +13,7 @@ class ApiController(FlaskController):
@route("")
def index(self):
v = {"version": version.number, "name": version.name}
v = {"version": version.number, "name": version.name, "code": 200}
return jsonify(v)
@route("/ping")
......
......@@ -9,6 +9,7 @@ from minor.decorators import json_required, auth_required
from minor.model import Song
from minor.util import get_ext, transform_mongo_dict
@route("/api/music")
class MusicController(FlaskController):
def __init__(self, upload_dir):
......@@ -59,26 +60,27 @@ class MusicController(FlaskController):
@auth_required
@route("/songs/<id>")
def song(self, id, payload=None):
song = Song.objects.get(id=id)
try:
song = Song.objects.get(id=id)
except Song.DoesNotExist:
return jsonify({"code": 404, "message": "not found"}), 404
if song is not None:
song = transform_mongo_dict(json.loads(song.to_json()))
return jsonify({"code": 200, "message": "ok", "data": song}), 200
else:
return jsonify({"code": 400, "message": "bad_request"}), 400
song = transform_mongo_dict(json.loads(song.to_json()))
return jsonify({"code": 200, "message": "ok", "data": song}), 200
@auth_required
@route("/songs/<id>/hasfile")
def song_has_file(self, id, payload=None):
song = Song.objects.get(id=id)
if song is not None:
filename = self._upload_dir + "/" + str(song.id) + ".mp3"
if os.path.isfile(filename):
return jsonify({"code": 200, "message": "ok", "data": True}), 200
else:
return jsonify({"code": 200, "message": "ok", "data": False}), 200
try:
song = Song.objects.get(id=id)
except Song.DoesNotExist:
return jsonify({"code": 404, "message": "not found"}), 404
filename = self._upload_dir + "/" + str(song.id) + ".mp3"
if os.path.isfile(filename):
return jsonify({"code": 200, "message": "ok", "data": True}), 200
else:
return jsonify({"code": 400, "message": "bad_request"}), 400
return jsonify({"code": 200, "message": "ok", "data": False}), 200
@auth_required
@json_required
......@@ -114,19 +116,23 @@ class MusicController(FlaskController):
def song_file(self, id, payload=None):
if 'file' in request.files:
file = request.files["file"]
db_song = Song.objects.get(id=id)
if db_song is not None:
if file.filename is not '':
ext = get_ext(file.filename)
if ext is not None:
name = secure_filename(str(db_song.id) + "." + ext)
path = os.path.join(self._upload_dir, name)
file.save(path)
# update db entry with the length of the song
db_song.playtime = MP3(path).info.length
db_song.save()
return jsonify({"code": 200, "message": "ok"}), 200
try:
db_song = Song.objects.get(id=id)
except Song.DoesNotExist:
return jsonify({"code": 404, "message": "not found"}), 404
if file.filename is not '':
ext = get_ext(file.filename)
if ext is not None:
name = secure_filename(str(db_song.id) + "." + ext)
path = os.path.join(self._upload_dir, name)
file.save(path)
# update db entry with the length of the song
db_song.playtime = MP3(path).info.length
db_song.save()
return jsonify({"code": 200, "message": "ok"}), 200
return jsonify({"code": 400, "message": "bad_request"}), 400
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment