handle invalid filename encodings

on Linux users can end up creating filenames that can't be encoded to utf8,
which breaks Anki when it tries to store the filename in sqlite
This commit is contained in:
Damien Elmes 2017-08-31 18:55:11 +10:00
parent f9f8b117d9
commit 5fd9228273

View File

@ -263,6 +263,14 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
if file.startswith("_"): if file.startswith("_"):
# leading _ says to ignore file # leading _ says to ignore file
continue continue
if self.hasIllegal(file):
name = file.encode(sys.getfilesystemencoding(), errors="replace")
name = str(name, sys.getfilesystemencoding())
warnings.append(
_("Invalid file name, please rename: %s") % name)
continue
nfcFile = unicodedata.normalize("NFC", file) nfcFile = unicodedata.normalize("NFC", file)
# we enforce NFC fs encoding on non-macs; on macs we'll have gotten # we enforce NFC fs encoding on non-macs; on macs we'll have gotten
# NFD so we use the above variable for comparing references # NFD so we use the above variable for comparing references
@ -320,7 +328,13 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
return re.sub(self._illegalCharReg, "", str) return re.sub(self._illegalCharReg, "", str)
def hasIllegal(self, str): def hasIllegal(self, str):
return not not re.search(self._illegalCharReg, str) if re.search(self._illegalCharReg, str):
return True
try:
str.encode(sys.getfilesystemencoding())
except UnicodeEncodeError:
return True
return False
# Tracking changes # Tracking changes
########################################################################## ##########################################################################