From 44df873f331e529e1c6ba11794e953739af4d8e1 Mon Sep 17 00:00:00 2001 From: Pavel Yakunin Date: Sun, 5 Jun 2016 19:42:18 +0300 Subject: [PATCH] logging, tmp cleanup --- cps/book_formats.py | 22 +++++++++++++++++----- cps/web.py | 14 ++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/cps/book_formats.py b/cps/book_formats.py index 4f0d16f4..fa1808fe 100644 --- a/cps/book_formats.py +++ b/cps/book_formats.py @@ -1,12 +1,22 @@ __author__ = 'lemmsh' +import logging +logger = logging.getLogger("book_formats") + import uploader import os try: from wand.image import Image use_generic_pdf_cover = False except ImportError, e: + logger.warning('cannot import Image, generating pdf covers for pdf uploads will not work') use_generic_pdf_cover = True +try: + from PyPDF2 import PdfFileReader + use_pdf_meta = True +except ImportError, e: + logger.warning('cannot import PyPDF2, extracting pdf metadata will not work') + use_pdf_meta = False def process(tmp_file_path, original_file_name, original_file_extension): if (".PDF" == original_file_extension.upper()): @@ -15,11 +25,13 @@ def process(tmp_file_path, original_file_name, original_file_extension): def pdf_meta(tmp_file_path, original_file_name, original_file_extension): - from PyPDF2 import PdfFileReader - pdf = PdfFileReader(open(tmp_file_path, 'rb')) - doc_info = pdf.getDocumentInfo() - print("!!!!!!!!!!!!!!") - print(doc_info.producer) + + if (use_pdf_meta): + pdf = PdfFileReader(open(tmp_file_path, 'rb')) + doc_info = pdf.getDocumentInfo() + else: + doc_info = None + if (doc_info is not None): author = doc_info.author title = doc_info.title diff --git a/cps/web.py b/cps/web.py index 6b71305b..e158b9ea 100755 --- a/cps/web.py +++ b/cps/web.py @@ -23,13 +23,7 @@ import base64 from sqlalchemy.sql import * import json import datetime -import book_formats from uuid import uuid4 -try: - from wand.image import Image - use_generic_pdf_cover = False -except ImportError, e: - use_generic_pdf_cover = True from shutil import copyfile app = (Flask(__name__)) @@ -41,6 +35,9 @@ file_handler.setLevel(logging.INFO) file_handler.setFormatter(formatter) app.logger.addHandler(file_handler) app.logger.info('Starting Calibre Web...') +logging.getLogger("book_formats").addHandler(file_handler) +logging.getLogger("book_formats").setLevel(logging.INFO) + Principal(app) @@ -1077,6 +1074,7 @@ def edit_book(book_id): return render_template('edit_book.html', book=book, authors=author_names, cc=cc) import uploader +from shutil import move @app.route("/upload", methods = ["GET", "POST"]) @login_required @@ -1107,7 +1105,7 @@ def upload(): flash("Failed to create path %s (Permission denied)." % filepath, category="error") return redirect(url_for('index')) try: - copyfile(meta.file_path, saved_filename) #remove as well + move(meta.file_path, saved_filename) #remove as well except OSError: flash("Failed to store file %s (Permission denied)." % saved_filename, category="error") return redirect(url_for('index')) @@ -1119,7 +1117,7 @@ def upload(): copyfile(os.path.join(basedir, "static/generic_cover.jpg"), os.path.join(filepath, "cover.jpg")) else: has_cover = 1 - copyfile(meta.cover, os.path.join(filepath, "cover.jpg")) + move(meta.cover, os.path.join(filepath, "cover.jpg")) is_author = db.session.query(db.Authors).filter(db.Authors.name == author).first() if is_author: