upload_song: add error when file exceeds limit of 300MB (#653)

* upload_song: add error when file exceeds limit of 300MB (closes #647)

* fix import
This commit is contained in:
sigma67 2024-10-05 11:40:51 +02:00 • committed by GitHub
commit 7d8a756638
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 1 deletions

View file

@ -4,7 +4,8 @@ from pathlib import Path
from unittest import mock
import pytest
from test_helpers import is_ci
from tests.test_helpers import is_ci
class TestBrowsing:

View file

@ -1,10 +1,12 @@
import tempfile
import time
from unittest import mock
import pytest
from tests.conftest import get_resource
from ytmusicapi.enums import ResponseStatus
from ytmusicapi.exceptions import YTMusicUserError
from ytmusicapi.ytmusic import YTMusic
@ -51,6 +53,12 @@ class TestUploads:
response = yt_auth.upload_song(get_resource(config["uploads"]["file"]))
assert response.status_code == 409
with (
mock.patch("os.path.getsize", return_value=4 * 10**9),
pytest.raises(YTMusicUserError, match="larger than the limit of 300MB"),
):
yt_auth.upload_song(get_resource(config["uploads"]["file"]))
def test_upload_song_and_verify(self, config, yt_auth: YTMusic):
"""Upload a song and verify it can be retrieved after it finishes processing."""
upload_response = yt_auth.upload_song(get_resource(config["uploads"]["file"]))

View file

@ -225,6 +225,10 @@ class UploadsMixin(MixinProtocol):
headers = self.headers.copy()
upload_url = f"https://upload.youtube.com/upload/usermusic/http?authuser={headers['x-goog-authuser']}"
filesize = os.path.getsize(filepath)
if filesize >= 314572800: # 300MB in bytes
msg = f"File {filepath} has size {filesize} bytes, which is larger than the limit of 300MB"
raise YTMusicUserError(msg)
body = ("filename=" + ntpath.basename(filepath)).encode("utf-8")
headers.pop("content-encoding", None)
headers["content-type"] = "application/x-www-form-urlencoded;charset=utf-8"