diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 7e4c662b..7bff089b 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -22,7 +22,9 @@ import shutil import chardet import ssl import sqlite3 +import mimetypes +from werkzeug.datastructures import Headers from flask import Response, stream_with_context from sqlalchemy import create_engine from sqlalchemy import Column, UniqueConstraint @@ -600,7 +602,10 @@ def get_cover_via_gdrive(cover_path): except (OperationalError, IntegrityError) as ex: log.error_or_exception('Database error: {}'.format(ex)) session.rollback() - return df.metadata.get('webContentLink') + headers = Headers() + headers["Content-Type"] = 'image/jpeg' + resp, content = df.auth.Get_Http_Object().request(df.metadata.get('downloadUrl'), headers=headers) + return content else: return None diff --git a/cps/helper.py b/cps/helper.py index 84e46a24..f4c226a6 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -30,7 +30,7 @@ import requests import unidecode from uuid import uuid4 -from flask import send_from_directory, make_response, redirect, abort, url_for +from flask import send_from_directory, make_response, abort, url_for, Response from flask_babel import gettext as _ from flask_babel import lazy_gettext as N_ from flask_babel import get_locale @@ -787,9 +787,9 @@ def get_book_cover_internal(book, resolution=None): try: if not gd.is_gdrive_ready(): return get_cover_on_failure() - path = gd.get_cover_via_gdrive(book.path) - if path: - return redirect(path) + cover_file = gd.get_cover_via_gdrive(book.path) + if cover_file: + return Response(cover_file, mimetype='image/jpeg') else: log.error('{}/cover.jpg not found on Google Drive'.format(book.path)) return get_cover_on_failure() diff --git a/cps/tasks/thumbnail.py b/cps/tasks/thumbnail.py index 54b68f40..2921ffc9 100644 --- a/cps/tasks/thumbnail.py +++ b/cps/tasks/thumbnail.py @@ -19,6 +19,7 @@ import os from shutil import copyfile, copyfileobj from urllib.request import urlopen +from io import BytesIO from .. import constants from cps import config, db, fs, gdriveutils, logger, ub @@ -182,13 +183,11 @@ class TaskGenerateCoverThumbnails(CalibreTask): if not gdriveutils.is_gdrive_ready(): raise Exception('Google Drive is configured but not ready') - web_content_link = gdriveutils.get_cover_via_gdrive(book.path) - if not web_content_link: + content = gdriveutils.get_cover_via_gdrive(book.path) + if not content: raise Exception('Google Drive cover url not found') - - stream = None try: - stream = urlopen(web_content_link) + stream = BytesIO(content) with Image(file=stream) as img: filename = self.cache.get_cache_file_path(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS)