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!
"""
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()
try:
text = text.decode(ENCODING)
@ -218,7 +218,7 @@ def _write_playlist_to_file(playlist, xml):
text += '\n'
text = text.encode(ENCODING, 'ignore')
try:
with open(playlist.kodi_path, 'wb') as f:
with open(utils.encode_path(playlist.kodi_path), 'wb') as f:
f.write(text)
except (OSError, IOError) as err:
LOG.error('Could not write Kodi playlist file: %s', playlist)
@ -361,8 +361,10 @@ def _full_sync():
if state.ENABLE_MUSIC:
master_paths.append(v.PLAYLIST_PATH_MUSIC)
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:
file = utils.decode_path(file)
try:
extension = file.rsplit('.', 1)[1]
except IndexError:

View file

@ -1410,14 +1410,8 @@ class API(object):
return
else:
LOG.debug('Writing temp subtitle to %s', path)
try:
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)
with open(utils.encode_path(path), 'wb') as filer:
filer.write(response.content)
return path
def kodi_premiere_date(self):

View file

@ -24,6 +24,7 @@ import hashlib
import re
import unicodedata
from .watchdog.utils import unicode_paths
from . import variables as v
from . import state
@ -117,7 +118,7 @@ def exists_dir(path):
else:
dummyfile = os.path.join(try_decode(path), 'dummyfile.txt')
try:
with open(dummyfile, 'w') as filer:
with open(encode_path(dummyfile), 'w') as filer:
filer.write('text')
except IOError:
# folder does not exist yet
@ -247,6 +248,26 @@ def kodi_time_to_millis(time):
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'):
"""
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'
}
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(
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n'
'<smartplaylist type="%s">\n\t'
@ -1022,6 +1043,7 @@ def delete_playlists():
if file.startswith('Plex'):
os.remove(os.path.join(root, file))
def delete_nodes():
"""
Clean up video nodes
@ -1043,7 +1065,7 @@ def generate_file_md5(path):
"""
m = hashlib.md5()
m.update(path.encode('utf-8'))
with open(path, 'rb') as f:
with open(encode_path(path), 'rb') as f:
while True:
piece = f.read(32768)
if not piece: