Added support for custom set of safe characters (configured via addon settings) when escaping paths using direct paths for http(s), dav(s) and (s)ftp urls.

This commit is contained in:
geropan 2020-06-07 18:27:20 +01:00
parent 038477fa77
commit 2ce1a6e639
6 changed files with 44 additions and 6 deletions

View file

@ -880,6 +880,11 @@ msgctxt "#39036"
msgid "Escape special characters in path (e.g. space to %20)" msgid "Escape special characters in path (e.g. space to %20)"
msgstr "" msgstr ""
# PKC Settings - Customize Paths
msgctxt "#39090"
msgid "Safe characters for http(s), dav(s) and (s)ftp urls"
msgstr ""
# PKC Settings - Customize Paths # PKC Settings - Customize Paths
msgctxt "#39037" msgctxt "#39037"
msgid "Original Plex MOVIE path to replace:" msgid "Original Plex MOVIE path to replace:"

View file

@ -965,7 +965,12 @@ msgstr ""
# PKC Settings - Customize Paths # PKC Settings - Customize Paths
msgctxt "#39036" msgctxt "#39036"
msgid "Escape special characters in path (e.g. space to %20)" msgid "Escape special characters in path (e.g. space to %20)"
msgstr "Escapar caracteres especiales en la ruta (i.e. espacio a %20)" msgstr "Escapar caracteres especiales en la ruta (p. ej. espacio a %20)"
# PKC Settings - Customize Paths
msgctxt "#39090"
msgid "Safe characters for http(s), dav(s) and (s)ftp urls"
msgstr "Caracteres seguros para urls http(s), dav(s) y (s)ftp"
# PKC Settings - Customize Paths # PKC Settings - Customize Paths
msgctxt "#39037" msgctxt "#39037"

View file

@ -42,6 +42,7 @@ class Sync(object):
self.remapSMBphotoNew = None self.remapSMBphotoNew = None
# Escape path? # Escape path?
self.escape_path = None self.escape_path = None
self.escape_path_safe_chars = None
# Shall we replace custom user ratings with the number of versions available? # Shall we replace custom user ratings with the number of versions available?
self.indicate_media_versions = None self.indicate_media_versions = None
# Will sync movie trailer differently: either play trailer directly or show # Will sync movie trailer differently: either play trailer directly or show
@ -109,6 +110,7 @@ class Sync(object):
self.remapSMBphotoOrg = remove_trailing_slash(utils.settings('remapSMBphotoOrg')) self.remapSMBphotoOrg = remove_trailing_slash(utils.settings('remapSMBphotoOrg'))
self.remapSMBphotoNew = remove_trailing_slash(utils.settings('remapSMBphotoNew')) self.remapSMBphotoNew = remove_trailing_slash(utils.settings('remapSMBphotoNew'))
self.escape_path = utils.settings('escapePath') == 'true' self.escape_path = utils.settings('escapePath') == 'true'
self.escape_path_safe_chars = utils.settings('escapePathSafeChars').encode('utf-8')
self.indicate_media_versions = utils.settings('indicate_media_versions') == "true" self.indicate_media_versions = utils.settings('indicate_media_versions') == "true"
self.sync_specific_plex_playlists = utils.settings('syncSpecificPlexPlaylists') == 'true' self.sync_specific_plex_playlists = utils.settings('syncSpecificPlexPlaylists') == 'true'
self.sync_specific_kodi_playlists = utils.settings('syncSpecificKodiPlaylists') == 'true' self.sync_specific_kodi_playlists = utils.settings('syncSpecificKodiPlaylists') == 'true'

View file

@ -319,7 +319,7 @@ class Media(object):
if path.startswith('\\\\'): if path.startswith('\\\\'):
path = 'smb:' + path.replace('\\', '/') path = 'smb:' + path.replace('\\', '/')
if app.SYNC.escape_path: if app.SYNC.escape_path:
path = utils.escape_path(path) path = utils.escape_path(path, app.SYNC.escape_path_safe_chars)
if (app.SYNC.path_verified and not force_check) or omit_check: if (app.SYNC.path_verified and not force_check) or omit_check:
return path return path

View file

@ -56,7 +56,7 @@ REGEX_MUSICPATH = re.compile(r'''^\^(.+)\$$''')
REGEX_PLEX_ID_FROM_URL = re.compile(r'''metadata%2F(\d+)''') REGEX_PLEX_ID_FROM_URL = re.compile(r'''metadata%2F(\d+)''')
SAFE_URL_CHARACTERS = "%/:=&?~#+!$,;'@()*[]".encode('utf-8') SAFE_URL_CHARACTERS = "%/:=&?~#+!$,;'@()*[]".encode('utf-8')
HTTP_DAV_FTP = re.compile(r'(http(s)?|dav(s)?|(s)?ftp)://((.+):(.+)@)?([\w\.]+)(:([\d]+))?/')
def garbageCollect(): def garbageCollect():
gc.collect(2) gc.collect(2)
@ -385,7 +385,7 @@ def urlparse(url, scheme='', allow_fragments=True):
return _urlparse.urlparse(url, scheme, allow_fragments) return _urlparse.urlparse(url, scheme, allow_fragments)
def escape_path(path): def escape_path(path, safe_url_char=SAFE_URL_CHARACTERS):
""" """
Uses urllib.quote to escape to escape path [unicode]. See here for the Uses urllib.quote to escape to escape path [unicode]. See here for the
reasoning whether a character is safe or not and whether or not it should reasoning whether a character is safe or not and whether or not it should
@ -396,8 +396,33 @@ def escape_path(path):
you in trouble (e.g. '@') you in trouble (e.g. '@')
Returns the escaped path as unicode Returns the escaped path as unicode
""" """
return urllib.quote(path.encode('utf-8'), is_http_dav_ftp = HTTP_DAV_FTP.match(path)
safe=SAFE_URL_CHARACTERS).decode('utf-8') if is_http_dav_ftp:
# If path seems to be a http(s), dav(s) or (s)ftp url, the escape path will be constructed using RegExp and
# using safe_url_char as safe characters not to be escaped
protocol = is_http_dav_ftp.group(1)
user = is_http_dav_ftp.group(6)
psswd = is_http_dav_ftp.group(7)
if user and psswd:
user = urllib.quote(user.encode('utf-8'), safe=safe_url_char).decode('utf-8')
psswd = urllib.quote(psswd.encode('utf-8'), safe=safe_url_char).decode('utf-8')
host = is_http_dav_ftp.group(8)
port = is_http_dav_ftp.group(10)
url_path = path.replace(is_http_dav_ftp.group(), '', 1)
if url_path:
url_path = urllib.quote(path.replace(is_http_dav_ftp.group(), '', 1).encode('utf-8'),
safe=safe_url_char).decode('utf-8')
return protocol + \
u'://' + \
(user + u':' + psswd + u'@' if (user and psswd) else u'') + \
host + \
(u':' + port if port else u'') + \
u'/' + \
(url_path if url_path else u'')
else:
# If paths does not seem to be a http(s), dav(s) or (s)ftp url (e.g. plugin://), escape path as before
return urllib.quote(path.encode('utf-8'),
safe=SAFE_URL_CHARACTERS).decode('utf-8')
def quote(s, safe='/'): def quote(s, safe='/'):

View file

@ -101,6 +101,7 @@
<setting id="remapSMBphotoOrg" type="text" label="39045" default="" visible="eq(-7,true)" subsetting="true" /> <!-- Original Plex MUSIC path to replace: --> <setting id="remapSMBphotoOrg" type="text" label="39045" default="" visible="eq(-7,true)" subsetting="true" /> <!-- Original Plex MUSIC path to replace: -->
<setting id="remapSMBphotoNew" type="text" label="39046" default="smb://" visible="eq(-8,true)" subsetting="true" /> <!-- Replace Plex MUSIC with: --> <setting id="remapSMBphotoNew" type="text" label="39046" default="smb://" visible="eq(-8,true)" subsetting="true" /> <!-- Replace Plex MUSIC with: -->
<setting id="escapePath" type="bool" label="39036" default="false" /> <!-- Escape special characters in path (e.g. space to %20) --> <setting id="escapePath" type="bool" label="39036" default="false" /> <!-- Escape special characters in path (e.g. space to %20) -->
<setting id="escapePathSafeChars" type="text" label="39090" default="%/:=&amp;?~#+!$,;'@()*[]" visible="eq(-1,true)" subsetting="true"/> <!-- Safe characters for http(s), dav(s) and (s)ftp urls -->
</category> </category>
<category label="30516"><!-- Playback --> <category label="30516"><!-- Playback -->