From c8b9d624cdf2c4e8cc821f5704b2a9a6d1a0a8fa Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Jun 2013 12:02:12 +0530 Subject: [PATCH] Fix caching of images in docx to use filenames instead of possibly non-unique relationship ids. --- src/calibre/ebooks/docx/images.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/docx/images.py b/src/calibre/ebooks/docx/images.py index ea3685316f..85e957a589 100644 --- a/src/calibre/ebooks/docx/images.py +++ b/src/calibre/ebooks/docx/images.py @@ -101,10 +101,11 @@ class Images(object): self.rid_map = relationships_by_id def generate_filename(self, rid, base=None, rid_map=None): - if rid in self.used: - return self.used[rid] rid_map = self.rid_map if rid_map is None else rid_map - raw = self.docx.read(rid_map[rid]) + fname = rid_map[rid] + if fname in self.used: + return self.used[fname] + raw = self.docx.read(fname) base = base or ascii_filename(rid_map[rid].rpartition('/')[-1]).replace(' ', '_') or 'image' ext = what(None, raw) or base.rpartition('.')[-1] or 'jpeg' base = base.rpartition('.')[0] @@ -118,7 +119,7 @@ class Images(object): n, e = base.rpartition('.')[0::2] name = '%s-%d.%s' % (n, c, e) c += 1 - self.used[rid] = name + self.used[fname] = name with open(os.path.join(self.dest_dir, name), 'wb') as f: f.write(raw) self.all_images.add('images/' + name)