def _get_attachment(cls, book_path, filename): """Get file as MIMEBase message""" calibre_path = config.config_calibre_dir if config.config_use_google_drive: df = gdriveutils.getFileFromEbooksFolder(book_path, filename) if df: datafile = os.path.join(calibre_path, book_path, filename) if not os.path.exists(os.path.join(calibre_path, book_path)): os.makedirs(os.path.join(calibre_path, book_path)) df.GetContentFile(datafile) else: return None file_ = open(datafile, 'rb') data = file_.read() file_.close() os.remove(datafile) else: try: file_ = open(os.path.join(calibre_path, book_path, filename), 'rb') data = file_.read() file_.close() except IOError as e: log.error_or_exception(e, stacklevel=3) log.error( u'The requested file could not be read. Maybe wrong permissions?' ) return None return data
def _get_attachment(cls, bookpath, filename): """Get file as MIMEBase message""" calibre_path = config.config_calibre_dir if config.config_use_google_drive: df = gdriveutils.getFileFromEbooksFolder(bookpath, filename) if df: datafile = os.path.join(calibre_path, bookpath, filename) if not os.path.exists(os.path.join(calibre_path, bookpath)): os.makedirs(os.path.join(calibre_path, bookpath)) df.GetContentFile(datafile) else: return None file_ = open(datafile, 'rb') data = file_.read() file_.close() os.remove(datafile) else: try: file_ = open(os.path.join(calibre_path, bookpath, filename), 'rb') data = file_.read() file_.close() except IOError as e: log.debug_or_exception(e) log.error(u'The requested file could not be read. Maybe wrong permissions?') return None # Set mimetype content_type, encoding = mimetypes.guess_type(filename) if content_type is None or encoding is not None: content_type = 'application/octet-stream' main_type, sub_type = content_type.split('/', 1) attachment = MIMEBase(main_type, sub_type) attachment.set_payload(data) encoders.encode_base64(attachment) attachment.add_header('Content-Disposition', 'attachment', filename=filename) return attachment
def run(self, worker_thread): self.worker_thread = worker_thread if config.config_use_google_drive: worker_db = db.CalibreDB(expire_on_commit=False) cur_book = worker_db.get_book(self.bookid) self.title = cur_book.title data = worker_db.get_book_format(self.bookid, self.settings['old_book_format']) df = gdriveutils.getFileFromEbooksFolder( cur_book.path, data.name + "." + self.settings['old_book_format'].lower()) if df: datafile = os.path.join( config.config_calibre_dir, cur_book.path, data.name + u"." + self.settings['old_book_format'].lower()) if not os.path.exists( os.path.join(config.config_calibre_dir, cur_book.path)): os.makedirs( os.path.join(config.config_calibre_dir, cur_book.path)) df.GetContentFile(datafile) worker_db.session.close() else: error_message = _( u"%(format)s not found on Google Drive: %(fn)s", format=self.settings['old_book_format'], fn=data.name + "." + self.settings['old_book_format'].lower()) worker_db.session.close() return error_message filename = self._convert_ebook_format() if config.config_use_google_drive: os.remove(self.file_path + u'.' + self.settings['old_book_format'].lower()) if filename: if config.config_use_google_drive: # Upload files to gdrive gdriveutils.updateGdriveCalibreFromLocal() self._handleSuccess() if self.kindle_mail: # if we're sending to kindle after converting, create a one-off task and run it immediately # todo: figure out how to incorporate this into the progress try: EmailText = _(u"%(book)s send to Kindle", book=escape(self.title)) worker_thread.add( self.user, TaskEmail(self.settings['subject'], self.results["path"], filename, self.settings, self.kindle_mail, EmailText, self.settings['body'], internal=True)) except Exception as ex: return self._handleError(str(ex))
def _get_attachment(cls, bookpath, filename, bookname=''): """Get file as MIMEBase message""" calibrepath = config.config_calibre_dir if config.config_use_google_drive: df = gdriveutils.getFileFromEbooksFolder(bookpath, filename) if df: datafile = os.path.join(calibrepath, bookpath, filename) if not os.path.exists(os.path.join(calibrepath, bookpath)): os.makedirs(os.path.join(calibrepath, bookpath)) df.GetContentFile(datafile) else: return None file_ = open(datafile, 'rb') data = file_.read() file_.close() os.remove(datafile) else: try: file_ = open(os.path.join(calibrepath, bookpath, filename), 'rb') data = file_.read() file_.close() except IOError as e: log.exception(e) log.error( u'The requested file could not be read. Maybe wrong permissions?' ) return None attachment = MIMEBase('application', 'octet-stream') attachment.set_payload(data) encoders.encode_base64(attachment) attachment.add_header('Content-Disposition', 'attachment', filename=bookname) return attachment