Enabled covers with gdrive

This commit is contained in:
Ozzie Isaacs 2024-07-02 20:53:08 +02:00
parent 60dd3df861
commit ba0718f3d0
3 changed files with 14 additions and 10 deletions

View File

@ -22,7 +22,9 @@ import shutil
import chardet import chardet
import ssl import ssl
import sqlite3 import sqlite3
import mimetypes
from werkzeug.datastructures import Headers
from flask import Response, stream_with_context from flask import Response, stream_with_context
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy import Column, UniqueConstraint from sqlalchemy import Column, UniqueConstraint
@ -600,7 +602,10 @@ def get_cover_via_gdrive(cover_path):
except (OperationalError, IntegrityError) as ex: except (OperationalError, IntegrityError) as ex:
log.error_or_exception('Database error: {}'.format(ex)) log.error_or_exception('Database error: {}'.format(ex))
session.rollback() 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: else:
return None return None

View File

@ -30,7 +30,7 @@ import requests
import unidecode import unidecode
from uuid import uuid4 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 gettext as _
from flask_babel import lazy_gettext as N_ from flask_babel import lazy_gettext as N_
from flask_babel import get_locale from flask_babel import get_locale
@ -787,9 +787,9 @@ def get_book_cover_internal(book, resolution=None):
try: try:
if not gd.is_gdrive_ready(): if not gd.is_gdrive_ready():
return get_cover_on_failure() return get_cover_on_failure()
path = gd.get_cover_via_gdrive(book.path) cover_file = gd.get_cover_via_gdrive(book.path)
if path: if cover_file:
return redirect(path) return Response(cover_file, mimetype='image/jpeg')
else: else:
log.error('{}/cover.jpg not found on Google Drive'.format(book.path)) log.error('{}/cover.jpg not found on Google Drive'.format(book.path))
return get_cover_on_failure() return get_cover_on_failure()

View File

@ -19,6 +19,7 @@
import os import os
from shutil import copyfile, copyfileobj from shutil import copyfile, copyfileobj
from urllib.request import urlopen from urllib.request import urlopen
from io import BytesIO
from .. import constants from .. import constants
from cps import config, db, fs, gdriveutils, logger, ub from cps import config, db, fs, gdriveutils, logger, ub
@ -182,13 +183,11 @@ class TaskGenerateCoverThumbnails(CalibreTask):
if not gdriveutils.is_gdrive_ready(): if not gdriveutils.is_gdrive_ready():
raise Exception('Google Drive is configured but not ready') raise Exception('Google Drive is configured but not ready')
web_content_link = gdriveutils.get_cover_via_gdrive(book.path) content = gdriveutils.get_cover_via_gdrive(book.path)
if not web_content_link: if not content:
raise Exception('Google Drive cover url not found') raise Exception('Google Drive cover url not found')
stream = None
try: try:
stream = urlopen(web_content_link) stream = BytesIO(content)
with Image(file=stream) as img: with Image(file=stream) as img:
filename = self.cache.get_cache_file_path(thumbnail.filename, filename = self.cache.get_cache_file_path(thumbnail.filename,
constants.CACHE_TYPE_THUMBNAILS) constants.CACHE_TYPE_THUMBNAILS)