]> littlesong.place Git - littlesongplace.git/commitdiff
Refactor mp3 verification
authorChris Fulljames <christianfulljames@gmail.com>
Sun, 26 Jan 2025 16:09:30 +0000 (11:09 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Sun, 26 Jan 2025 16:09:30 +0000 (11:09 -0500)
main.py
test/test_offline.py

diff --git a/main.py b/main.py
index 2451d3dbc14f0732f3936c49fd1a0a8dbfeaa4dd..8dab553dd69b108aee83aa51b20c553b55c74a55 100644 (file)
--- 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/<int:songid>")
 def delete_song(songid):
 
index de07c4f7d3d0f80612abdf2eef52434e471567bd..8b990f43b5baa16ab26949e425511126be20fefe 100644 (file)
@@ -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 &#39;song title&#39;", songid=1)
+    _test_upload_song(client, b"Successfully updated &#39;song title&#39;", 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
+