Fix exporting of data not storing timestamps for format and extra files

Also make the code DRYer
This commit is contained in:
Kovid Goyal 2024-04-21 09:24:37 +05:30
parent d6b602a085
commit bcc8ea4d5e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 9 additions and 25 deletions

View File

@ -46,7 +46,7 @@ from calibre.ebooks.metadata.book.base import Metadata
from calibre.ebooks.metadata.opf2 import metadata_to_opf
from calibre.ptempfile import PersistentTemporaryFile, SpooledTemporaryFile, base_dir
from calibre.utils.config import prefs, tweaks
from calibre.utils.date import UNDEFINED_DATE, utcnow
from calibre.utils.date import UNDEFINED_DATE, timestampfromdt, utcnow
from calibre.utils.date import now as nowf
from calibre.utils.filenames import make_long_path_useable
from calibre.utils.icu import lower as icu_lower
@ -3158,7 +3158,10 @@ class Cache:
mdata = self.format_metadata(book_id, fmt)
key = f'{key_prefix}:{book_id}:{fmt}'
fm[fmt] = key
with exporter.start_file(key, mtime=mdata.get('mtime')) as dest:
mtime = mdata.get('mtime')
if mtime is not None:
mtime = timestampfromdt(mtime)
with exporter.start_file(key, mtime=mtime) as dest:
self._copy_format_to(book_id, fmt, dest, report_file_size=dest.ensure_space)
cover_key = '{}:{}:{}'.format(key_prefix, book_id, '.cover')
with exporter.start_file(cover_key) as dest:

View File

@ -25,18 +25,6 @@ from polyglot.builtins import error_message, iteritems
# Export {{{
def send_file(from_obj, to_obj, chunksize=1<<20):
m = hashlib.sha1()
while True:
raw = from_obj.read(chunksize)
if not raw:
break
m.update(raw)
to_obj.write(raw)
return str(m.hexdigest())
class FileDest:
def __init__(self, key, exporter, mtime=None):
@ -44,7 +32,7 @@ class FileDest:
self.hasher = hashlib.sha1()
self.start_pos = exporter.f.tell()
self._discard = False
self.mtime = None
self.mtime = mtime
def discard(self):
self._discard = True
@ -59,7 +47,7 @@ class FileDest:
self.exporter.f.write(data)
def flush(self):
pass
self.exporter.f.flush()
def close(self):
if not self._discard:
@ -127,15 +115,8 @@ class Exporter:
self.commit_part(is_last=True)
def add_file(self, fileobj, key):
fileobj.seek(0, os.SEEK_END)
size = fileobj.tell()
fileobj.seek(0)
self.ensure_space(size)
pos = self.f.tell()
digest = send_file(fileobj, self.f)
size = self.f.tell() - pos
mtime = os.fstat(fileobj.fileno()).st_mtime
self.file_metadata[key] = (len(self.parts), pos, size, digest, mtime)
with self.start_file(key, os.fstat(fileobj.fileno()).st_mtime) as dest:
shutil.copyfileobj(fileobj, dest)
def start_file(self, key, mtime=None):
return FileDest(key, self, mtime=mtime)