Merge remote-tracking branch 'embed_metadata/embed_metadata_on_send' into Develop

This commit is contained in:
Ozzie Isaacs 2024-02-18 14:48:04 +01:00
commit 8e5bee5352
2 changed files with 27 additions and 2 deletions

View File

@ -159,7 +159,7 @@ EXTENSIONS_UPLOAD = {'txt', 'pdf', 'epub', 'kepub', 'mobi', 'azw', 'azw3', 'cbr'
_extension = "" _extension = ""
if sys.platform == "win32": if sys.platform == "win32":
_extension = ".exe" _extension = ".exe"
SUPPORTED_CALIBRE_BINARIES = {binary:binary + _extension for binary in ["ebook-convert", "calibredb"]} SUPPORTED_CALIBRE_BINARIES = {binary:binary + _extension for binary in ["ebook-convert", "calibredb", "ebook-meta"]}
def has_flag(value, bit_flag): def has_flag(value, bit_flag):

View File

@ -21,6 +21,7 @@ import smtplib
import ssl import ssl
import threading import threading
import socket import socket
from shutil import copy
import mimetypes import mimetypes
from io import StringIO from io import StringIO
@ -33,6 +34,8 @@ from email.utils import formatdate
from cps.services.worker import CalibreTask from cps.services.worker import CalibreTask
from cps.services import gmail from cps.services import gmail
from cps import logger, config from cps import logger, config
from cps.subproc_wrapper import process_open
from cps.constants import SUPPORTED_CALIBRE_BINARIES
from cps import gdriveutils from cps import gdriveutils
import uuid import uuid
@ -249,15 +252,23 @@ class TaskEmail(CalibreTask):
df.GetContentFile(datafile) df.GetContentFile(datafile)
else: else:
return None return None
if config.config_binariesdir:
datafile = cls._embed_metadata(calibre_path, book_path, filename, datafile)
os.remove(os.path.join(calibre_path, book_path, filename))
file_ = open(datafile, 'rb') file_ = open(datafile, 'rb')
data = file_.read() data = file_.read()
file_.close() file_.close()
os.remove(datafile) os.remove(datafile)
else: else:
datafile = os.path.join(calibre_path, book_path, filename)
try: try:
file_ = open(os.path.join(calibre_path, book_path, filename), 'rb') if config.config_binariesdir:
datafile = cls._embed_metadata(calibre_path, book_path, filename, datafile)
file_ = open(datafile, 'rb')
data = file_.read() data = file_.read()
file_.close() file_.close()
if config.config_binariesdir:
os.remove(datafile)
except IOError as e: except IOError as e:
log.error_or_exception(e, stacklevel=3) log.error_or_exception(e, stacklevel=3)
log.error('The requested file could not be read. Maybe wrong permissions?') log.error('The requested file could not be read. Maybe wrong permissions?')
@ -274,3 +285,17 @@ class TaskEmail(CalibreTask):
def __str__(self): def __str__(self):
return "E-mail {}, {}".format(self.name, self.subject) return "E-mail {}, {}".format(self.name, self.subject)
def _embed_metadata(self, calibre_path, book_path, filename, datafile):
datafile_tmp = os.path.join(calibre_path, book_path, "tmp_" + filename)
path_opf = os.path.join(calibre_path, book_path, "metadata.opf")
copy(datafile, datafile_tmp)
calibredb_binarypath = os.path.join(config.config_binariesdir, SUPPORTED_CALIBRE_BINARIES["ebook-meta"])
opf_command = [calibredb_binarypath, datafile_tmp, "--from-opf", path_opf]
p = process_open(opf_command)
_, err = p.communicate()
if err:
# ToDo: Improve error handling
log.error('Metadata embedder encountered an error: %s', err)
return datafile_tmp