def copy_format_to(self, book_id, fmt, path): fdata = self.dbctx.run('export', 'fmt', book_id, fmt, path) if self.dbctx.is_remote: if fdata is None: raise NoSuchFormat(fmt) with lopen(path, 'wb') as f: f.write(fdata)
def book_fmt(ctx, rd, library_id, db, book_id, fmt): mdata = db.format_metadata(book_id, fmt) if not mdata: raise NoSuchFormat() mtime = mdata['mtime'] update_metadata = fmt in update_metadata_in_fmts extra_etag_data = '' if update_metadata: mi = db.get_metadata(book_id) mtime = max(mtime, mi.last_modified) # Get any plugboards for the Content server plugboards = db.pref('plugboards') if plugboards: cpb = find_plugboard(plugboard_content_server_value, fmt, plugboards) if cpb: # Transform the metadata via the plugboard newmi = mi.deepcopy_metadata() newmi.template_to_attribute(mi, cpb) mi = newmi extra_etag_data = repr(cpb) else: mi = db.get_proxy_metadata(book_id) def copy_func(dest): db.copy_format_to(book_id, fmt, dest) if update_metadata: set_metadata(dest, mi, fmt) dest.seek(0) au = authors_to_string(mi.authors or [_('Unknown')]) title = mi.title or _('Unknown') ext = fmt if ext == 'kepub' and 'Kobo Touch' in rd.inheaders.get('User-Agent', ''): ext = 'kepub.epub' fname = '%s - %s_%s.%s' % (title[:30], au[:30], book_id, ext) fname = ascii_filename(fname).replace('"', '_') rd.outheaders['Content-Disposition'] = 'attachment; filename="%s"' % fname return create_file_copy(ctx, rd, 'fmt', library_id, book_id, fmt, mtime, copy_func, extra_etag_data=extra_etag_data)
def book_fmt(ctx, rd, library_id, db, book_id, fmt): mdata = db.format_metadata(book_id, fmt) if not mdata: raise NoSuchFormat() mtime = mdata['mtime'] update_metadata = fmt in update_metadata_in_fmts extra_etag_data = '' if update_metadata: mi = db.get_metadata(book_id) mtime = max(mtime, mi.last_modified) # Get any plugboards for the Content server plugboards = db.pref('plugboards') if plugboards: cpb = find_plugboard(plugboard_content_server_value, fmt, plugboards) if cpb: # Transform the metadata via the plugboard newmi = mi.deepcopy_metadata() newmi.template_to_attribute(mi, cpb) mi = newmi extra_etag_data = repr(cpb) else: mi = db.get_proxy_metadata(book_id) def copy_func(dest): db.copy_format_to(book_id, fmt, dest) if update_metadata: if not mi.cover_data or not mi.cover_data[-1]: cdata = db.cover(book_id) if cdata: mi.cover_data = ('jpeg', cdata) set_metadata(dest, mi, fmt) dest.seek(0) rd.outheaders[ 'Content-Disposition'] = '''attachment; filename="%s"; filename*=utf-8''%s''' % ( book_filename(rd, book_id, mi, fmt), book_filename(rd, book_id, mi, fmt, as_encoded_unicode=True)) return create_file_copy(ctx, rd, 'fmt', library_id, book_id, fmt, mtime, copy_func, extra_etag_data=extra_etag_data)
def copy_format_to(self, book_id, fmt, dest, use_hardlink=False): ''' Copy the format ``fmt`` to the file like object ``dest``. If the specified format does not exist, raises :class:`NoSuchFormat` error. dest can also be a path, in which case the format is copied to it, iff the path is different from the current path (taking case sensitivity into account). ''' try: name = self.fields['formats'].format_fname(book_id, fmt) path = self._field_for('path', book_id).replace('/', os.sep) except (KeyError, AttributeError): raise NoSuchFormat('Record %d has no %s file' % (book_id, fmt)) return self.backend.copy_format_to(book_id, fmt, name, path, dest, use_hardlink=use_hardlink)
def format_path(self, index, fmt, index_is_id=False): book_id = index if index_is_id else self.id(index) ans = self.new_api.format_abspath(book_id, fmt) if ans is None: raise NoSuchFormat('Record %d has no format: %s' % (book_id, fmt)) return ans