From 6c493df2f56c6f4ea77b771d74b3553f468af3cb Mon Sep 17 00:00:00 2001 From: Yijun Zhao Date: Tue, 26 Mar 2019 14:31:00 +0800 Subject: [PATCH 1/9] support issue: #843 --- cps/helper.py | 15 +++++++++++++-- requirements.txt | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cps/helper.py b/cps/helper.py index ea9e035c..6826c9c6 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -24,6 +24,7 @@ import ub from flask import current_app as app from tempfile import gettempdir import sys +import io import os import re import unicodedata @@ -34,6 +35,7 @@ from flask_babel import gettext as _ from flask_login import current_user from babel.dates import format_datetime from datetime import datetime +from PIL import Image import shutil import requests try: @@ -445,10 +447,19 @@ def get_book_cover(cover_path): # saves book cover to gdrive or locally def save_cover(url, book_path): img = requests.get(url) - if img.headers.get('content-type') != 'image/jpeg': - web.app.logger.error("Cover is no jpg file, can't save") + content_type = img.headers.get('content-type') + if content_type not in ('image/jpeg', 'image/png', 'image/webp'): + web.app.logger.error("Cover is only support jpg/png/webp file, can't save") return False + # convert to jpg because calibre just support jpg + if content_type in ('image/png', 'image/webp'): + imgc = Image.open(io.BytesIO(img.content)) + im = imgc.convert('RGB') + tmp_bytesio = io.BytesIO() + im.save(tmp_bytesio, format='JPEG') + img.content = tmp_bytesio.getvalue() + if ub.config.config_use_google_drive: tmpDir = gettempdir() f = open(os.path.join(tmpDir, "uploaded_cover.jpg"), "wb") diff --git a/requirements.txt b/requirements.txt index 3fb23ea3..84ffdd7c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ SQLAlchemy>=1.1.0 tornado>=4.1 Wand>=0.4.4 unidecode>=0.04.19 +Pillow>=5.4.0 \ No newline at end of file From 0b8624b542f413c4213df8065e53bcefb68390d0 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Wed, 17 Apr 2019 19:55:32 +0200 Subject: [PATCH 2/9] Deleted load metadata from douban, as API seems to be shut down (#858) --- cps/static/js/get_meta.js | 22 +++++++++------------- cps/templates/book_edit.html | 4 ++-- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/cps/static/js/get_meta.js b/cps/static/js/get_meta.js index 4598228a..7c5dbe4f 100644 --- a/cps/static/js/get_meta.js +++ b/cps/static/js/get_meta.js @@ -25,15 +25,12 @@ var ggResults = []; $(function () { var msg = i18nMsg; - var douban = "https://api.douban.com"; - var dbSearch = "/v2/book/search"; - // var dbGetInfo = "/v2/book/"; - // var db_get_info_by_isbn = "/v2/book/isbn/ "; - var dbDone = false; + /*var douban = "https://api.douban.com"; + var dbSearch = "/v2/book/search";*/ + var dbDone = true; var google = "https://www.googleapis.com/"; var ggSearch = "/books/v1/volumes"; - // var gg_get_info = "/books/v1/volumes/"; var ggDone = false; var showFlag = 0; @@ -96,7 +93,7 @@ $(function () { }); ggDone = false; } - if (dbDone && dbResults.length > 0) { + /*if (dbDone && dbResults.length > 0) { dbResults.forEach(function(result) { var book = { id: result.id, @@ -130,7 +127,7 @@ $(function () { $("#book-list").append($book); }); dbDone = false; - } + }*/ } function ggSearchBook (title) { @@ -150,7 +147,7 @@ $(function () { }); } - function dbSearchBook (title) { + /*function dbSearchBook (title) { $.ajax({ url: douban + dbSearch + "?q=" + title + "&fields=all&count=10", type: "GET", @@ -160,7 +157,7 @@ $(function () { dbResults = data.books; }, error: function error() { - $("#meta-info").html("

" + msg.search_error + "!

"); + $("#meta-info").html("

" + msg.search_error + "!

"+ $("#meta-info")[0].innerHTML) }, complete: function complete() { dbDone = true; @@ -168,14 +165,13 @@ $(function () { $("#show-douban").trigger("change"); } }); - } + }*/ function doSearch (keyword) { showFlag = 0; $("#meta-info").text(msg.loading); - // var keyword = $("#keyword").val(); if (keyword) { - dbSearchBook(keyword); + // dbSearchBook(keyword); ggSearchBook(keyword); } } diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 9d2b2c76..90eb20c0 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -223,8 +223,8 @@