From f9526349715709e808e50d1212477847f275ea62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 5 Jan 2014 13:46:08 +0100 Subject: [PATCH] Refactored detection of corrupted files --- kcc/comic2ebook.py | 23 ++++++++++++++++++++--- kcc/comic2panel.py | 15 --------------- kcc/image.py | 21 ++------------------- 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index ffab9d4..a3e11c0 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -712,9 +712,6 @@ def sanitizeTreeBeforeConversion(filetree): for root, dirs, files in os.walk(filetree, False): for name in files: os.chmod(os.path.join(root, name), stat.S_IWRITE | stat.S_IREAD) - # Detect corrupted files - Phase 1 - if os.path.getsize(os.path.join(root, name)) == 0: - os.remove(os.path.join(root, name)) for name in dirs: os.chmod(os.path.join(root, name), stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC) @@ -870,6 +867,25 @@ def preSplitDirectory(path): return [path] +def detectCorruption(tmpPath, orgPath): + for root, dirs, files in os.walk(tmpPath, False): + for name in files: + if getImageFileName(name) is not None: + path = os.path.join(root, name) + pathOrg = os.path.join(orgPath, name) + if os.path.getsize(path) == 0: + rmtree(os.path.join(tmpPath, '..', '..'), True) + raise RuntimeError('Image file %s is corrupted.' % pathOrg) + try: + img = Image.open(path) + img.verify() + img = Image.open(path) + img.load() + except: + rmtree(os.path.join(tmpPath, '..', '..'), True) + raise RuntimeError('Image file %s is corrupted.' % pathOrg) + + def Copyright(): print ('comic2ebook v%(__version__)s. ' 'Written 2013 by Ciro Mattia Gonano and Pawel Jastrzebski.' % globals()) @@ -951,6 +967,7 @@ def main(argv=None, qtGUI=None): parser.print_help() return path = getWorkFolder(args[0]) + detectCorruption(path + "/OEBPS/Images/", args[0]) checkComicInfo(path + "/OEBPS/Images/", args[0]) if options.webtoon: if options.customheight > 0: diff --git a/kcc/comic2panel.py b/kcc/comic2panel.py index f803ebe..4fd86ed 100644 --- a/kcc/comic2panel.py +++ b/kcc/comic2panel.py @@ -170,21 +170,6 @@ def splitImage(work): print ".", fileExpanded = os.path.splitext(name) filePath = os.path.join(path, name) - # Detect corrupted files - try: - Image.open(filePath) - except IOError: - raise RuntimeError('Cannot read image file %s' % filePath) - try: - image = Image.open(filePath) - image.verify() - except: - raise RuntimeError('Image file %s is corrupted' % filePath) - try: - image = Image.open(filePath) - image.load() - except: - raise RuntimeError('Image file %s is corrupted' % filePath) image = Image.open(filePath) image = image.convert('RGB') widthImg, heightImg = image.size diff --git a/kcc/image.py b/kcc/image.py index ab19bcc..b1bde19 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -122,25 +122,8 @@ class ComicPage: self.profile_label, self.size, self.palette, self.gamma, self.panelviewsize = device except KeyError: raise RuntimeError('Unexpected output device %s' % device) - # Detect corrupted files - Phase 2 - try: - self.origFileName = source - self.filename = os.path.basename(self.origFileName) - self.image = Image.open(source) - except IOError: - raise RuntimeError('Cannot read image file %s' % source) - # Detect corrupted files - Phase 3 - try: - self.image = Image.open(source) - self.image.verify() - except: - raise RuntimeError('Image file %s is corrupted' % source) - # Detect corrupted files - Phase 4 - try: - self.image = Image.open(source) - self.image.load() - except: - raise RuntimeError('Image file %s is corrupted' % source) + self.origFileName = source + self.filename = os.path.basename(self.origFileName) self.image = Image.open(source) self.image = self.image.convert('RGB') self.rotated = None