From: Chris Fulljames Date: Sun, 26 Jan 2025 16:09:30 +0000 (-0500) Subject: Refactor mp3 verification X-Git-Url: https://littlesong.place/gitweb/gitweb.cgi?a=commitdiff_plain;h=ec6a222425cf89a0c4cf1c11211e4f81d4714bec;p=littlesongplace.git Refactor mp3 verification --- diff --git a/main.py b/main.py index 2451d3d..8dab553 100644 --- a/main.py +++ b/main.py @@ -310,15 +310,7 @@ def update_song(): error = False if file: with tempfile.NamedTemporaryFile(delete=False) as tmp_file: - file.save(tmp_file) - tmp_file.close() - - result = subprocess.run(["mpck", tmp_file.name], stdout=subprocess.PIPE) - res_stdout = result.stdout.decode() - app.logger.info(f"mpck result: \n {res_stdout}") - lines = res_stdout.split("\n") - lines = [l.strip().lower() for l in lines] - passed = any(l.startswith("result") and l.endswith("ok") for l in lines) + passed = convert_song(tmp_file, file) if passed: # Move file to permanent location @@ -357,15 +349,7 @@ def create_song(): collaborators = [c.strip() for c in request.form["collabs"].split(",")] with tempfile.NamedTemporaryFile(delete=False) as tmp_file: - file.save(tmp_file) - tmp_file.close() - - result = subprocess.run(["mpck", tmp_file.name], stdout=subprocess.PIPE) - res_stdout = result.stdout.decode() - app.logger.info(f"mpck result: \n {res_stdout}") - lines = res_stdout.split("\n") - lines = [l.strip().lower() for l in lines] - passed = any(l.startswith("result") and l.endswith("ok") for l in lines) + passed = convert_song(tmp_file, file) if not passed: flash_and_log("Invalid mp3 file", "error") @@ -395,6 +379,21 @@ def create_song(): flash_and_log(f"Successfully uploaded '{title}'", "success") return False +def convert_song(tmp_file, request_file): + request_file.save(tmp_file) + tmp_file.close() + + result = subprocess.run(["mpck", tmp_file.name], stdout=subprocess.PIPE) + res_stdout = result.stdout.decode() + app.logger.info(f"mpck result: \n {res_stdout}") + lines = res_stdout.split("\n") + lines = [l.strip().lower() for l in lines] + if any(l.startswith("result") and l.endswith("ok") for l in lines): + # Uploaded valid mp3 file + return True + + return False + @app.get("/delete-song/") def delete_song(songid): diff --git a/test/test_offline.py b/test/test_offline.py index de07c4f..8b990f4 100644 --- a/test/test_offline.py +++ b/test/test_offline.py @@ -174,8 +174,8 @@ def test_update_bio(client): # Upload Song ################################################################################ -def _test_upload_song(client, msg, error=False, songid=None, user="user", **kwargs): - song_file = open("sample-3s.mp3", "rb") +def _test_upload_song(client, msg, error=False, songid=None, user="user", filename="sample-3s.mp3", **kwargs): + song_file = open(filename, "rb") data = { "song": song_file, @@ -266,7 +266,11 @@ def _create_user_and_song(client): def test_update_song_success(client): _create_user_and_song(client) - _test_upload_song(client, b"Successfully updated 'song title'", songid=1) + _test_upload_song(client, b"Successfully updated 'song title'", filename="sample-6s.mp3", songid=1) + response = client.get("/song/1/1") + assert response.status_code == 200 + with open("sample-6s.mp3", "rb") as expected_file: + assert response.data == expected_file.read() def test_update_song_bad_title(client): _create_user_and_song(client) @@ -519,3 +523,4 @@ def test_site_news(client): response = client.get("/site-news") assert response.status_code == 200 assert b"Site News" in response.data +