From a311197af54d14f44d6a686c674d2c1e5be60f23 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 18 Feb 2011 13:57:18 -0700 Subject: [PATCH] Fix regression that broke extracting covers from CBR files on windows. Fixes #9038 (Covers no longer being detected in .cbr files) --- src/calibre/libunrar.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/calibre/libunrar.py b/src/calibre/libunrar.py index 4bfd5c537a..d0e48034b5 100644 --- a/src/calibre/libunrar.py +++ b/src/calibre/libunrar.py @@ -13,7 +13,7 @@ from tempfile import NamedTemporaryFile from StringIO import StringIO from calibre import iswindows, load_library, CurrentDir -from calibre.ptempfile import TemporaryDirectory +from calibre.ptempfile import TemporaryDirectory, PersistentTemporaryFile _librar_name = 'libunrar' cdll = ctypes.cdll @@ -272,13 +272,18 @@ def extract_member(path, match=re.compile(r'\.(jpg|jpeg|gif|png)\s*$', re.I), def extract_first_alphabetically(path): if hasattr(path, 'read'): data = path.read() - f = NamedTemporaryFile(suffix='.rar') - f.write(data) - f.flush() + with PersistentTemporaryFile('.rar') as f: + f.write(data) path = f.name names_ = [x for x in names(path) if os.path.splitext(x)[1][1:].lower() in ('png', 'jpg', 'jpeg', 'gif')] names_.sort() - return extract_member(path, name=names_[0], match=None) + ans = extract_member(path, name=names_[0], match=None) + try: + os.remove(path) + except: + pass + return ans +