From 5fd9228273fda7830c3c15dd92b01553842ca133 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 31 Aug 2017 18:55:11 +1000 Subject: [PATCH] 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 --- anki/media.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/anki/media.py b/anki/media.py index 3703100f4..31e836bf8 100644 --- a/anki/media.py +++ b/anki/media.py @@ -263,6 +263,14 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); if file.startswith("_"): # leading _ says to ignore file 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) # we enforce NFC fs encoding on non-macs; on macs we'll have gotten # 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) 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 ##########################################################################