diff --git a/src/tribler-common/tribler_common/rest_utils.py b/src/tribler-common/tribler_common/rest_utils.py index e0d89a53778..0bc4eb2bc19 100644 --- a/src/tribler-common/tribler_common/rest_utils.py +++ b/src/tribler-common/tribler_common/rest_utils.py @@ -1,3 +1,4 @@ +import re from typing import Any, Union from yarl import URL @@ -6,6 +7,8 @@ HTTP_SCHEME = 'http' FILE_SCHEME = 'file' +re_fix_win_path = re.compile(r'^(?:/)(\w:)', re.UNICODE) + def to_file_uri(file_path: Union[str, Any]) -> str: if not isinstance(file_path, str): @@ -14,4 +17,6 @@ def to_file_uri(file_path: Union[str, Any]) -> str: def from_file_uri(file_uri: str) -> str: - return URL(file_uri).path + path = URL(file_uri).path + corrected_path = re_fix_win_path.sub(r'\1', path) + return corrected_path diff --git a/src/tribler-common/tribler_common/tests/test_rest_utils.py b/src/tribler-common/tribler_common/tests/test_rest_utils.py index dec4f74555a..15b4592979b 100644 --- a/src/tribler-common/tribler_common/tests/test_rest_utils.py +++ b/src/tribler-common/tribler_common/tests/test_rest_utils.py @@ -1,9 +1,35 @@ -from tribler_common.rest_utils import from_file_uri, to_file_uri +import pytest +from tribler_common.rest_utils import from_file_uri, re_fix_win_path, to_file_uri -def test_to_file_uri(): - assert to_file_uri('/path') == 'file:///path' +PATHS = [ + # nix + ('/path/to/file', 'file:///path/to/file'), + ('/path/to/%20%21file', 'file:///path/to/%2520%2521file'), # See: https://github.com/Tribler/tribler/issues/6700 + # win + ('E:\\path\\to\\file', 'file:///E:%5Cpath%5Cto%5Cfile'), +] +PATHS_TO_FIX = [ + # correct + ('c:\\', 'c:\\'), + ('/path', '/path'), + # corrupted: + ('/c:\\', 'c:\\'), + ('/C:\\', 'C:\\'), +] -def test_from_file_uri(): - assert from_file_uri('file:///path') == '/path' + +@pytest.mark.parametrize('path,uri', PATHS) +def test_to_file_uri(path, uri): + assert to_file_uri(path) == uri + + +@pytest.mark.parametrize('path,uri', PATHS) +def test_from_file_uri(path, uri): + assert from_file_uri(uri) == path + + +@pytest.mark.parametrize('original,expected', PATHS_TO_FIX) +def test_re_fix_win_path(original, expected): + assert re_fix_win_path.sub(r'\1', original) == expected diff --git a/src/tribler-core/tribler_core/requirements.txt b/src/tribler-core/tribler_core/requirements.txt index d9b67a1134d..f043d25a03e 100644 --- a/src/tribler-core/tribler_core/requirements.txt +++ b/src/tribler-core/tribler_core/requirements.txt @@ -20,4 +20,4 @@ pyyaml==6.0 sentry-sdk==1.5.0 service-identity==21.1.0 yappi==1.3.3 -yarl==1.7.0 \ No newline at end of file +yarl==1.7.2 # keep this dependency higher than 1.6.3. See: https://github.com/aio-libs/yarl/issues/517