Skip to content

Commit

Permalink
upload_song: add error when file exceeds limit of 300MB (#653)
Browse files Browse the repository at this point in the history
* upload_song: add error when file exceeds limit of 300MB (closes #647)

* fix import
  • Loading branch information
sigma67 authored Oct 5, 2024
1 parent 09ca398 commit 7d8a756
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/mixins/test_browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from unittest import mock

import pytest
from test_helpers import is_ci

from tests.test_helpers import is_ci


class TestBrowsing:
Expand Down
8 changes: 8 additions & 0 deletions tests/mixins/test_uploads.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -51,6 +53,12 @@ def test_upload_song(self, config, yt_auth):
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"]))
Expand Down
4 changes: 4 additions & 0 deletions ytmusicapi/mixins/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ def upload_song(self, filepath: str) -> Union[ResponseStatus, requests.Response]
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"
Expand Down

0 comments on commit 7d8a756

Please sign in to comment.