From cf6810db87ffd7d8f4ce48c707561e72789e3dd6 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 11 Nov 2023 14:48:59 +0100 Subject: [PATCH] Refactored get_temp_dir bugfixes export_metadata --- cps/file_helper.py | 26 +++++++++++++++++++++++ cps/gdrive.py | 6 ++---- cps/gdriveutils.py | 1 - cps/helper.py | 49 +++++++++++++++++++++++--------------------- cps/tasks/convert.py | 9 ++++---- cps/updater.py | 6 +++--- cps/uploader.py | 7 ++----- 7 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 cps/file_helper.py diff --git a/cps/file_helper.py b/cps/file_helper.py new file mode 100644 index 00000000..717cbb7d --- /dev/null +++ b/cps/file_helper.py @@ -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 . + +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 diff --git a/cps/gdrive.py b/cps/gdrive.py index 832350e1..4d110f83 100644 --- a/cps/gdrive.py +++ b/cps/gdrive.py @@ -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()))) diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 08ead47d..b1d30596 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -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 diff --git a/cps/helper.py b/cps/helper.py index cabc0363..013a6fc2 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -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,7 +942,13 @@ 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: - return gd.do_gdrive_download(df, headers) + # 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) else: @@ -957,34 +961,33 @@ 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, download_name + "." + book_format)) + # ToDo Check headers parameter + for element in headers: + response.headers[element[0]] = element[1] + log.info('Downloading file: {}'.format(os.path.join(filename, book_name + "." + book_format))) + return response - response = make_response(send_from_directory(filename, book_name + "." + book_format)) - # ToDo Check headers parameter - for element in headers: - response.headers[element[0]] = element[1] - log.info('Downloading file: {}'.format(os.path.join(filename, book_name + "." + book_format))) - 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) diff --git a/cps/tasks/convert.py b/cps/tasks/convert.py index 2bef9a20..61a2bda0 100755 --- a/cps/tasks/convert.py +++ b/cps/tasks/convert.py @@ -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) diff --git a/cps/updater.py b/cps/updater.py index 6d6e408f..4369e18f 100644 --- a/cps/updater.py +++ b/cps/updater.py @@ -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): diff --git a/cps/uploader.py b/cps/uploader.py index 23dfc4a6..8f20762f 100644 --- a/cps/uploader.py +++ b/cps/uploader.py @@ -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)