Encode file paths correctly for all platforms

This commit is contained in:
Croneter 2018-06-22 13:39:38 +02:00
parent 9b76795ea4
commit c03abddc27
3 changed files with 32 additions and 14 deletions

View file

@ -180,7 +180,7 @@ def m3u_to_plex_ids(playlist):
Adapter to process *.m3u playlist files. Encoding is not uniform! Adapter to process *.m3u playlist files. Encoding is not uniform!
""" """
plex_ids = list() plex_ids = list()
with open(playlist.kodi_path, 'rb') as f: with open(utils.encode_path(playlist.kodi_path), 'rb') as f:
text = f.read() text = f.read()
try: try:
text = text.decode(ENCODING) text = text.decode(ENCODING)
@ -218,7 +218,7 @@ def _write_playlist_to_file(playlist, xml):
text += '\n' text += '\n'
text = text.encode(ENCODING, 'ignore') text = text.encode(ENCODING, 'ignore')
try: try:
with open(playlist.kodi_path, 'wb') as f: with open(utils.encode_path(playlist.kodi_path), 'wb') as f:
f.write(text) f.write(text)
except (OSError, IOError) as err: except (OSError, IOError) as err:
LOG.error('Could not write Kodi playlist file: %s', playlist) LOG.error('Could not write Kodi playlist file: %s', playlist)
@ -361,8 +361,10 @@ def _full_sync():
if state.ENABLE_MUSIC: if state.ENABLE_MUSIC:
master_paths.append(v.PLAYLIST_PATH_MUSIC) master_paths.append(v.PLAYLIST_PATH_MUSIC)
for master_path in master_paths: for master_path in master_paths:
for root, _, files in os.walk(master_path): for root, _, files in os.walk(utils.encode_path(master_path)):
root = utils.decode_path(root)
for file in files: for file in files:
file = utils.decode_path(file)
try: try:
extension = file.rsplit('.', 1)[1] extension = file.rsplit('.', 1)[1]
except IndexError: except IndexError:

View file

@ -1410,13 +1410,7 @@ class API(object):
return return
else: else:
LOG.debug('Writing temp subtitle to %s', path) LOG.debug('Writing temp subtitle to %s', path)
try: with open(utils.encode_path(path), 'wb') as filer:
with open(path, 'wb') as filer:
filer.write(response.content)
except UnicodeEncodeError:
LOG.debug('Need to slugify the filename %s', path)
path = utils.slugify(path)
with open(path, 'wb') as filer:
filer.write(response.content) filer.write(response.content)
return path return path

View file

@ -24,6 +24,7 @@ import hashlib
import re import re
import unicodedata import unicodedata
from .watchdog.utils import unicode_paths
from . import variables as v from . import variables as v
from . import state from . import state
@ -117,7 +118,7 @@ def exists_dir(path):
else: else:
dummyfile = os.path.join(try_decode(path), 'dummyfile.txt') dummyfile = os.path.join(try_decode(path), 'dummyfile.txt')
try: try:
with open(dummyfile, 'w') as filer: with open(encode_path(dummyfile), 'w') as filer:
filer.write('text') filer.write('text')
except IOError: except IOError:
# folder does not exist yet # folder does not exist yet
@ -247,6 +248,26 @@ def kodi_time_to_millis(time):
return ret return ret
def encode_path(path):
"""
Filenames and paths are not necessarily utf-8 encoded. Use this function
instead of try_encode/trydecode if working with filenames and paths!
(os.walk only feeds on encoded paths. sys.getfilesystemencoding returns None
for Raspberry Pi)
"""
return unicode_paths.encode(path)
def decode_path(path):
"""
Filenames and paths are not necessarily utf-8 encoded. Use this function
instead of try_encode/trydecode if working with filenames and paths!
(os.walk only feeds on encoded paths. sys.getfilesystemencoding returns None
for Raspberry Pi)
"""
return unicode_paths.decode(path)
def try_encode(input_str, encoding='utf-8'): def try_encode(input_str, encoding='utf-8'):
""" """
Will try to encode input_str (in unicode) to encoding. This possibly Will try to encode input_str (in unicode) to encoding. This possibly
@ -998,7 +1019,7 @@ def playlist_xsp(mediatype, tagname, viewid, viewtype="", delete=False):
'show': 'tvshows' 'show': 'tvshows'
} }
LOG.info("Writing playlist file to: %s", xsppath) LOG.info("Writing playlist file to: %s", xsppath)
with open(xsppath, 'wb') as filer: with open(encode_path(xsppath), 'wb') as filer:
filer.write(try_encode( filer.write(try_encode(
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n' '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n'
'<smartplaylist type="%s">\n\t' '<smartplaylist type="%s">\n\t'
@ -1022,6 +1043,7 @@ def delete_playlists():
if file.startswith('Plex'): if file.startswith('Plex'):
os.remove(os.path.join(root, file)) os.remove(os.path.join(root, file))
def delete_nodes(): def delete_nodes():
""" """
Clean up video nodes Clean up video nodes
@ -1043,7 +1065,7 @@ def generate_file_md5(path):
""" """
m = hashlib.md5() m = hashlib.md5()
m.update(path.encode('utf-8')) m.update(path.encode('utf-8'))
with open(path, 'rb') as f: with open(encode_path(path), 'rb') as f:
while True: while True:
piece = f.read(32768) piece = f.read(32768)
if not piece: if not piece: