Skip to content

Commit 664286a

Browse files
committed
Updated to use netloc comparison
Signed-off-by: Doug Halley <[email protected]>
1 parent 206e7b8 commit 664286a

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/py-opentimelineio/opentimelineio/url_utils.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""Utilities for conversion between urls and file paths"""
55

66
import os
7+
import sys
78
from urllib import (
89
parse as urlparse,
910
request
@@ -52,18 +53,18 @@ def filepath_from_url(urlstr):
5253
.. _ongoing discussions: https://discuss.python.org/t/file-uris-in-python/15600
5354
"""
5455

55-
# De-encode the URL
56-
decoded_url_str = urlparse.unquote(urlstr)
57-
5856
# Parse provided URL
59-
parsed_result = urlparse.urlparse(decoded_url_str)
57+
parsed_result = urlparse.urlparse(urlstr)
58+
59+
# De-encode the parsed path
60+
decoded_parsed_path = urlparse.unquote(parsed_result.path)
6061

6162
# Convert the parsed URL to a path
62-
filepath = Path(request.url2pathname(parsed_result.path))
63+
filepath = Path(request.url2pathname(decoded_parsed_path))
6364

6465
# If the network location is a window drive, reassemble the path
6566
if PureWindowsPath(parsed_result.netloc).drive:
66-
filepath = Path(parsed_result.netloc + parsed_result.path)
67+
filepath = Path(parsed_result.netloc + decoded_parsed_path)
6768

6869
# Check if the specified index has a specified `drive`, if so then do nothing
6970
elif filepath.drive:
@@ -74,9 +75,18 @@ def filepath_from_url(urlstr):
7475
# Remove leading "/" if/when `request.url2pathname` yields "/S:/path/file.ext"
7576
filepath = filepath.relative_to(filepath.root)
7677

77-
# Last resort, strip the "file:" prefix
78-
else:
79-
filepath = Path(decoded_url_str.strip('file:'))
78+
# Should catch UNC paths,
79+
# as parsing "file:///some/path/to/file.ext" doesn't provide a netloc
80+
elif parsed_result.netloc and parsed_result.netloc != 'localhost':
81+
# Paths of type: "file://host/share/path/to/file.ext" provide "host" as netloc
82+
filepath = Path('//', parsed_result.netloc + decoded_parsed_path)
8083

8184
# Convert "\" to "/" if needed
82-
return filepath.as_posix()
85+
path = filepath.as_posix()
86+
87+
# Since the previous code handles Windows drive letter, we can assume that any path
88+
# starting with a "/" is a UNC path if executed is run on Windows.
89+
if path.startswith('/') and sys.platform == 'win32':
90+
path = '/' + path
91+
92+
return path

tests/test_url_conversions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
MEDIA_EXAMPLE_PATH_ABS
3333
)
3434

35-
ENCODED_WINDOWS_URL = "file://localhost/S%3a/path/file.ext"
35+
ENCODED_WINDOWS_URL = "file://host/S%3a/path/file.ext"
3636
WINDOWS_DRIVE_URL = "file://S:/path/file.ext"
3737
CORRECTED_WINDOWS_DRIVE_PATH = "S:/path/file.ext"
3838

0 commit comments

Comments
 (0)