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:
parent
09ca398719
commit
7d8a756638
3 changed files with 14 additions and 1 deletions
|
|
@ -4,7 +4,8 @@ from pathlib import Path
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from test_helpers import is_ci
|
|
||||||
|
from tests.test_helpers import is_ci
|
||||||
|
|
||||||
|
|
||||||
class TestBrowsing:
|
class TestBrowsing:
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tests.conftest import get_resource
|
from tests.conftest import get_resource
|
||||||
from ytmusicapi.enums import ResponseStatus
|
from ytmusicapi.enums import ResponseStatus
|
||||||
|
from ytmusicapi.exceptions import YTMusicUserError
|
||||||
from ytmusicapi.ytmusic import YTMusic
|
from ytmusicapi.ytmusic import YTMusic
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -51,6 +53,12 @@ class TestUploads:
|
||||||
response = yt_auth.upload_song(get_resource(config["uploads"]["file"]))
|
response = yt_auth.upload_song(get_resource(config["uploads"]["file"]))
|
||||||
assert response.status_code == 409
|
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):
|
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 a song and verify it can be retrieved after it finishes processing."""
|
||||||
upload_response = yt_auth.upload_song(get_resource(config["uploads"]["file"]))
|
upload_response = yt_auth.upload_song(get_resource(config["uploads"]["file"]))
|
||||||
|
|
|
||||||
|
|
@ -225,6 +225,10 @@ class UploadsMixin(MixinProtocol):
|
||||||
headers = self.headers.copy()
|
headers = self.headers.copy()
|
||||||
upload_url = f"https://upload.youtube.com/upload/usermusic/http?authuser={headers['x-goog-authuser']}"
|
upload_url = f"https://upload.youtube.com/upload/usermusic/http?authuser={headers['x-goog-authuser']}"
|
||||||
filesize = os.path.getsize(filepath)
|
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")
|
body = ("filename=" + ntpath.basename(filepath)).encode("utf-8")
|
||||||
headers.pop("content-encoding", None)
|
headers.pop("content-encoding", None)
|
||||||
headers["content-type"] = "application/x-www-form-urlencoded;charset=utf-8"
|
headers["content-type"] = "application/x-www-form-urlencoded;charset=utf-8"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue