Skip to content

Commit

Permalink
Fixes #6747: OSError "The filename, directory name, or volume label s…
Browse files Browse the repository at this point in the history
…yntax is incorrect" in dragEnterEvent
  • Loading branch information
kozlovsky committed Jan 31, 2022
1 parent 285f749 commit c0aa282
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/tribler-common/tribler_common/rest_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path
from typing import Any, Union

from yarl import URL
Expand Down Expand Up @@ -43,3 +44,11 @@ def scheme_from_uri(uri: str) -> str:
'http://en.wikipedia.org' -> 'http'
"""
return URL(uri).scheme


def uri_is_valid_file(file_uri: str) -> bool:
file_path = uri_to_path(file_uri)
try:
return Path(file_path).is_file()
except OSError:
return False
10 changes: 9 additions & 1 deletion src/tribler-common/tribler_common/tests/test_rest_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from tribler_common.rest_utils import path_to_uri, scheme_from_uri, uri_to_path
from tribler_common.rest_utils import path_to_uri, scheme_from_uri, uri_is_valid_file, uri_to_path

NIX_PATHS = [
('/path/to/file', 'file:///path/to/file'),
Expand Down Expand Up @@ -51,3 +51,11 @@ def test_uri_to_path_win(path, uri):
@pytest.mark.parametrize('path, scheme', SCHEMES)
def test_scheme_from_uri(path, scheme):
assert scheme_from_uri(path) == scheme


def test_uri_is_valid_file(tmpdir):
file_path = tmpdir / '1.txt'
file_path.write('test')
file_uri = path_to_uri(file_path)
assert uri_is_valid_file(file_uri)
assert not uri_is_valid_file(file_uri + '/*')
4 changes: 2 additions & 2 deletions src/tribler-gui/tribler_gui/tribler_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

from tribler_common.network_utils import default_network_utils
from tribler_common.process_checker import ProcessChecker
from tribler_common.rest_utils import FILE_SCHEME, MAGNET_SCHEME, scheme_from_uri, uri_to_path
from tribler_common.rest_utils import FILE_SCHEME, MAGNET_SCHEME, scheme_from_uri, uri_is_valid_file, uri_to_path
from tribler_common.utilities import parse_query
from tribler_common.version_manager import VersionHistory

Expand Down Expand Up @@ -1142,7 +1142,7 @@ def get_urls_from_dragndrop_list(cls, e):

def dragEnterEvent(self, e):
file_urls = self.get_urls_from_dragndrop_list(e)
if any(Path(uri_to_path(fu)).is_file() for fu in file_urls):
if any(uri_is_valid_file(fu) for fu in file_urls):
e.accept()
else:
e.ignore()
Expand Down

0 comments on commit c0aa282

Please sign in to comment.