diff --git a/anki/media.py b/anki/media.py index d44065117..822982050 100644 --- a/anki/media.py +++ b/anki/media.py @@ -213,6 +213,7 @@ class MediaManager(object): allRefs.update(noteRefs) # loop through media folder unused = [] + invalid = [] if local is None: files = os.listdir(mdir) else: @@ -225,6 +226,9 @@ class MediaManager(object): if file.startswith("_"): # leading _ says to ignore file continue + if not isinstance(file, unicode): + invalid.append(unicode(file, sys.getfilesystemencoding(), "replace")) + 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 @@ -242,7 +246,7 @@ class MediaManager(object): else: allRefs.discard(nfcFile) nohave = [x for x in allRefs if not x.startswith("_")] - return (nohave, unused) + return (nohave, unused, invalid) def _normalizeNoteRefs(self, nid): note = self.col.getNote(nid) @@ -336,6 +340,9 @@ class MediaManager(object): return re.sub(self._illegalCharReg, "", str) def hasIllegal(self, str): + # a file that couldn't be decoded to unicode is considered invalid + if not isinstance(str, unicode): + return False return not not re.search(self._illegalCharReg, str) # Media syncing - bundling zip files to send to server diff --git a/aqt/main.py b/aqt/main.py index 2b0f840f8..395346e72 100644 --- a/aqt/main.py +++ b/aqt/main.py @@ -913,11 +913,16 @@ will be lost. Continue?""")) def onCheckMediaDB(self): self.progress.start(immediate=True) - (nohave, unused) = self.col.media.check() + (nohave, unused, invalid) = self.col.media.check() self.progress.finish() # generate report report = "" + if invalid: + report += _("Invalid encoding; please rename:") + report += "\n" + "\n".join(invalid) if unused: + if report: + report += "\n\n\n" report += _( "In media folder but not used by any cards:") report += "\n" + "\n".join(unused)