mirror of
https://github.com/ankitects/anki.git
synced 2025-09-23 16:26:40 -04:00
catch invalid file encodings in media check & sync
This commit is contained in:
parent
75f87201a2
commit
f6b9dadf13
2 changed files with 14 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue