mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
calibredb add_format: Add support for adding extra data files
This commit is contained in:
parent
b45ca52f0f
commit
2f6cd98741
@ -1936,8 +1936,10 @@ class DB:
|
||||
with src:
|
||||
yield relpath, src, mtime
|
||||
|
||||
def add_extra_file(self, relpath, stream, book_path):
|
||||
def add_extra_file(self, relpath, stream, book_path, replace=True):
|
||||
dest = os.path.abspath(os.path.join(self.library_path, book_path, relpath))
|
||||
if not replace and os.path.exists(dest):
|
||||
return False
|
||||
if isinstance(stream, str):
|
||||
try:
|
||||
shutil.copy2(stream, dest)
|
||||
@ -1952,6 +1954,7 @@ class DB:
|
||||
d = open(dest, 'wb')
|
||||
with d:
|
||||
shutil.copyfileobj(stream, d)
|
||||
return True
|
||||
|
||||
def write_backup(self, path, raw):
|
||||
path = os.path.abspath(os.path.join(self.library_path, path, METADATA_FILE_NAME))
|
||||
|
@ -3050,11 +3050,13 @@ class Cache:
|
||||
self.backend.reindex_annotations()
|
||||
|
||||
@write_api
|
||||
def add_extra_files(self, book_id, map_of_relpath_to_stream_or_path):
|
||||
def add_extra_files(self, book_id, map_of_relpath_to_stream_or_path, replace=True):
|
||||
' Add extra data files '
|
||||
path = self._field_for('path', book_id).replace('/', os.sep)
|
||||
added = {}
|
||||
for relpath, stream_or_path in map_of_relpath_to_stream_or_path.items():
|
||||
self.backend.add_extra_file(relpath, stream_or_path, path)
|
||||
added[relpath] = self.backend.add_extra_file(relpath, stream_or_path, path, replace)
|
||||
return added
|
||||
|
||||
@read_api
|
||||
def list_extra_files_matching(self, book_id, pattern=''):
|
||||
|
@ -15,8 +15,14 @@ def implementation(db, notify_changes, book_id, data, fmt, replace):
|
||||
is_remote = notify_changes is not None
|
||||
if is_remote:
|
||||
data = BytesIO(data[1])
|
||||
added = db.add_format(book_id, fmt, data, replace=replace)
|
||||
if is_remote and added:
|
||||
relpath = ''
|
||||
if fmt.startswith('.EXTRA_DATA_FILE:'):
|
||||
relpath = fmt[len('.EXTRA_DATA_FILE:'):]
|
||||
if relpath:
|
||||
added = db.add_extra_files(book_id, {relpath: data}, replace=replace)[relpath]
|
||||
else:
|
||||
added = db.add_format(book_id, fmt, data, replace=replace)
|
||||
if is_remote and added and not relpath:
|
||||
notify_changes(formats_added({book_id: (fmt,)}))
|
||||
return added
|
||||
|
||||
@ -40,6 +46,13 @@ it is replaced, unless the do not replace option is specified.\
|
||||
action='store_false',
|
||||
help=_('Do not replace the format if it already exists')
|
||||
)
|
||||
parser.add_option(
|
||||
'--as-extra-data-file',
|
||||
default=False,
|
||||
action='store_true',
|
||||
help=_('Add the file as an extra data file to the book, not an ebook format')
|
||||
)
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
@ -48,9 +61,14 @@ def main(opts, args, dbctx):
|
||||
raise SystemExit(_('You must specify an id and an e-book file'))
|
||||
|
||||
id, path, fmt = int(args[0]), args[1], os.path.splitext(args[1])[-1]
|
||||
if opts.as_extra_data_file:
|
||||
fmt = '.EXTRA_DATA_FILE:' + 'data/' + os.path.basename(args[1])
|
||||
else:
|
||||
fmt = fmt[1:].upper()
|
||||
if not fmt:
|
||||
raise SystemExit(_('e-book file must have an extension'))
|
||||
fmt = fmt[1:].upper()
|
||||
if not dbctx.run('add_format', id, dbctx.path(path), fmt, opts.replace):
|
||||
if opts.as_extra_data_file:
|
||||
raise SystemExit(f'An extra data file with the filename {os.path.basename(args[1])} already exists')
|
||||
raise SystemExit(_('A %(fmt)s file already exists for book: %(id)d, not replacing')%dict(fmt=fmt, id=id))
|
||||
return 0
|
||||
|
Loading…
x
Reference in New Issue
Block a user