catch invalid file encodings in media check & sync

This commit is contained in:
Damien Elmes 2013-11-13 17:19:25 +09:00
parent 75f87201a2
commit f6b9dadf13
2 changed files with 14 additions and 2 deletions

View file

@ -213,6 +213,7 @@ class MediaManager(object):
allRefs.update(noteRefs) allRefs.update(noteRefs)
# loop through media folder # loop through media folder
unused = [] unused = []
invalid = []
if local is None: if local is None:
files = os.listdir(mdir) files = os.listdir(mdir)
else: else:
@ -225,6 +226,9 @@ class MediaManager(object):
if file.startswith("_"): if file.startswith("_"):
# leading _ says to ignore file # leading _ says to ignore file
continue continue
if not isinstance(file, unicode):
invalid.append(unicode(file, sys.getfilesystemencoding(), "replace"))
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
@ -242,7 +246,7 @@ class MediaManager(object):
else: else:
allRefs.discard(nfcFile) allRefs.discard(nfcFile)
nohave = [x for x in allRefs if not x.startswith("_")] nohave = [x for x in allRefs if not x.startswith("_")]
return (nohave, unused) return (nohave, unused, invalid)
def _normalizeNoteRefs(self, nid): def _normalizeNoteRefs(self, nid):
note = self.col.getNote(nid) note = self.col.getNote(nid)
@ -336,6 +340,9 @@ class MediaManager(object):
return re.sub(self._illegalCharReg, "", str) return re.sub(self._illegalCharReg, "", str)
def hasIllegal(self, 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) return not not re.search(self._illegalCharReg, str)
# Media syncing - bundling zip files to send to server # Media syncing - bundling zip files to send to server

View file

@ -913,11 +913,16 @@ will be lost. Continue?"""))
def onCheckMediaDB(self): def onCheckMediaDB(self):
self.progress.start(immediate=True) self.progress.start(immediate=True)
(nohave, unused) = self.col.media.check() (nohave, unused, invalid) = self.col.media.check()
self.progress.finish() self.progress.finish()
# generate report # generate report
report = "" report = ""
if invalid:
report += _("Invalid encoding; please rename:")
report += "\n" + "\n".join(invalid)
if unused: if unused:
if report:
report += "\n\n\n"
report += _( report += _(
"In media folder but not used by any cards:") "In media folder but not used by any cards:")
report += "\n" + "\n".join(unused) report += "\n" + "\n".join(unused)