Refactored get_temp_dir

bugfixes export_metadata
This commit is contained in:
Ozzie Isaacs 2023-11-11 14:48:59 +01:00
parent b7aaa0f24d
commit cf6810db87
7 changed files with 64 additions and 40 deletions

26
cps/file_helper.py Normal file
View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
# Copyright (C) 2023 OzzieIsaacs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from tempfile import gettempdir
import os
def get_temp_dir():
tmp_dir = os.path.join(gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)
return tmp_dir

View File

@ -23,7 +23,6 @@
import os
import hashlib
import json
import tempfile
from uuid import uuid4
from time import time
from shutil import move, copyfile
@ -34,6 +33,7 @@ from flask_login import login_required
from . import logger, gdriveutils, config, ub, calibre_db, csrf
from .admin import admin_required
from .file_helper import get_temp_dir
gdrive = Blueprint('gdrive', __name__, url_prefix='/gdrive')
log = logger.create()
@ -139,9 +139,7 @@ try:
dbpath = os.path.join(config.config_calibre_dir, "metadata.db").encode()
if not response['deleted'] and response['file']['title'] == 'metadata.db' \
and response['file']['md5Checksum'] != hashlib.md5(dbpath): # nosec
tmp_dir = os.path.join(tempfile.gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)
tmp_dir = get_temp_dir()
log.info('Database file updated')
copyfile(dbpath, os.path.join(tmp_dir, "metadata.db_" + str(current_milli_time())))

View File

@ -34,7 +34,6 @@ except ImportError:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.exc import OperationalError, InvalidRequestError, IntegrityError
from sqlalchemy.orm.exc import StaleDataError
from sqlalchemy.sql.expression import text
try:
from httplib2 import __version__ as httplib2_version

View File

@ -25,9 +25,9 @@ import re
import shutil
import socket
from datetime import datetime, timedelta
from tempfile import gettempdir
import requests
import unidecode
from uuid import uuid4
from flask import send_from_directory, make_response, redirect, abort, url_for
from flask_babel import gettext as _
@ -60,6 +60,7 @@ from .services.worker import WorkerThread
from .tasks.mail import TaskEmail
from .tasks.thumbnail import TaskClearCoverThumbnailCache, TaskGenerateCoverThumbnails
from .tasks.metadata_backup import TaskBackupMetadata
from .file_helper import get_temp_dir
log = logger.create()
@ -921,10 +922,7 @@ def save_cover(img, book_path):
return False, _("Only jpg/jpeg files are supported as coverfile")
if config.config_use_google_drive:
tmp_dir = os.path.join(gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)
tmp_dir = get_temp_dir()
ret, message = save_cover_from_filestorage(tmp_dir, "uploaded_cover.jpg", img)
if ret is True:
gd.uploadFileToEbooksFolder(os.path.join(book_path, 'cover.jpg').replace("\\", "/"),
@ -944,6 +942,12 @@ def do_download_file(book, book_format, client, data, headers):
df = gd.getFileFromEbooksFolder(book.path, book_name + "." + book_format)
# log.debug('%s', time.time() - startTime)
if df:
# ToDo check:!!!!!!!!
if config.config_binariesdir:
output = os.path.join(config.config_calibre_dir, book.path, data.name)
gd.ownloadFile(book.path, book_name + "." + book_format, output)
filename, download_name = do_calibre_export(book, book_format)
else:
return gd.do_gdrive_download(df, headers)
else:
abort(404)
@ -957,9 +961,11 @@ def do_download_file(book, book_format, client, data, headers):
headers["Content-Disposition"] = headers["Content-Disposition"].replace(".kepub", ".kepub.epub")
if config.config_binariesdir:
filename, book_name = do_calibre_export(book, book_format)
filename, download_name = do_calibre_export(book, book_format)
else:
download_name = book_name
response = make_response(send_from_directory(filename, book_name + "." + book_format))
response = make_response(send_from_directory(filename, download_name + "." + book_format))
# ToDo Check headers parameter
for element in headers:
response.headers[element[0]] = element[1]
@ -967,24 +973,21 @@ def do_download_file(book, book_format, client, data, headers):
return response
def do_calibre_export(book, book_format):
try:
quotes = [3, 5, 7, 9]
tmp_dir = os.path.join(gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)
tmp_dir = get_temp_dir()
calibredb_binarypath = get_calibre_binarypath("calibredb")
opf_command = [calibredb_binarypath, 'export', '--dont-write-opf', str(book.id),
'--with-library', config.config_calibre_dir, '--to-dir', tmp_dir,
'--formats', book_format, "--template", "{} - {{authors}}".format(book.title)]
file_name = book.title
if len(book.authors) > 0:
file_name = file_name + ' - ' + book.authors[0].name
temp_file_name = str(uuid4())
opf_command = [calibredb_binarypath, 'export', '--dont-write-opf', '--with-library', config.config_calibre_dir,
'--to-dir', tmp_dir, '--formats', book_format, "--template", "{}".format(temp_file_name),
str(book.id)]
p = process_open(opf_command, quotes)
_, err = p.communicate()
if err:
log.error('Metadata embedder encountered an error: %s', err)
return tmp_dir, file_name
return tmp_dir, temp_file_name
except OSError as ex:
# ToDo real error handling
log.error_or_exception(ex)

View File

@ -21,7 +21,6 @@ import re
from glob import glob
from shutil import copyfile, copyfileobj
from markupsafe import escape
from tempfile import gettempdir
from time import time
from sqlalchemy.exc import SQLAlchemyError
@ -34,6 +33,7 @@ from cps.subproc_wrapper import process_open
from flask_babel import gettext as _
from cps.kobo_sync_status import remove_synced_book
from cps.ub import init_db_thread
from cps.file_helper import get_temp_dir
from cps.tasks.mail import TaskEmail
from cps import gdriveutils
@ -243,9 +243,10 @@ class TaskConvert(CalibreTask):
# separate handling for windows and linux
quotes = [3, 5]
tmp_dir = os.path.join(gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)
tmp_dir = get_temp_dir()
#tmp_dir = os.path.join(gettempdir(), 'calibre_web')
#if not os.path.isdir(tmp_dir):
# os.mkdir(tmp_dir)
calibredb_binarypath = os.path.join(config.config_binariesdir, SUPPORTED_CALIBRE_BINARIES["calibredb"])
opf_command = [calibredb_binarypath, 'show_metadata', '--as-opf', str(book_id), '--with-library', config.config_calibre_dir]
p = process_open(opf_command, quotes)

View File

@ -25,13 +25,13 @@ import threading
import time
import zipfile
from io import BytesIO
from tempfile import gettempdir
import requests
from flask_babel import format_datetime
from flask_babel import gettext as _
from . import constants, logger # config, web_server
from .file_helper import gettempdir
log = logger.create()
@ -85,7 +85,7 @@ class Updater(threading.Thread):
z = zipfile.ZipFile(BytesIO(r.content))
self.status = 3
log.debug('Extracting zipfile')
tmp_dir = gettempdir()
tmp_dir = get_temp_dir()
z.extractall(tmp_dir)
folder_name = os.path.join(tmp_dir, z.namelist()[0])[:-1]
if not os.path.isdir(folder_name):

View File

@ -18,12 +18,12 @@
import os
import hashlib
from tempfile import gettempdir
from flask_babel import gettext as _
from . import logger, comic, isoLanguages
from .constants import BookMeta
from .helper import split_authors
from .file_helper import get_temp_dir
log = logger.create()
@ -249,10 +249,7 @@ def get_magick_version():
def upload(uploadfile, rar_excecutable):
tmp_dir = os.path.join(gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)
tmp_dir = get_temp_dir()
filename = uploadfile.filename
filename_root, file_extension = os.path.splitext(filename)