|
|
@ -23,6 +23,7 @@ from cps import config, global_WorkerThread, get_locale, db, mimetypes
|
|
|
|
from flask import current_app as app
|
|
|
|
from flask import current_app as app
|
|
|
|
from tempfile import gettempdir
|
|
|
|
from tempfile import gettempdir
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import unicodedata
|
|
|
|
import unicodedata
|
|
|
@ -72,6 +73,12 @@ try:
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
pass # We're not using Python 3
|
|
|
|
pass # We're not using Python 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
use_PIL = True
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
|
|
|
use_PIL = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_download(book_id, user_id):
|
|
|
|
def update_download(book_id, user_id):
|
|
|
|
check = ub.session.query(ub.Downloads).filter(ub.Downloads.user_id == user_id).filter(ub.Downloads.book_id ==
|
|
|
|
check = ub.session.query(ub.Downloads).filter(ub.Downloads.user_id == user_id).filter(ub.Downloads.book_id ==
|
|
|
@ -459,29 +466,73 @@ def get_book_cover(cover_path):
|
|
|
|
return send_from_directory(os.path.join(config.config_calibre_dir, cover_path), "cover.jpg")
|
|
|
|
return send_from_directory(os.path.join(config.config_calibre_dir, cover_path), "cover.jpg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# saves book cover to gdrive or locally
|
|
|
|
# saves book cover from url
|
|
|
|
def save_cover(url, book_path):
|
|
|
|
def save_cover_from_url(url, book_path):
|
|
|
|
img = requests.get(url)
|
|
|
|
img = requests.get(url)
|
|
|
|
if img.headers.get('content-type') != 'image/jpeg':
|
|
|
|
return save_cover(img, book_path)
|
|
|
|
app.logger.error("Cover is no jpg file, can't save")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
|
|
|
|
tmpDir = gettempdir()
|
|
|
|
def save_cover_from_filestorage(filepath, saved_filename, img):
|
|
|
|
f = open(os.path.join(tmpDir, "uploaded_cover.jpg"), "wb")
|
|
|
|
if hasattr(img, '_content'):
|
|
|
|
f.write(img.content)
|
|
|
|
f = open(os.path.join(filepath, saved_filename), "wb")
|
|
|
|
|
|
|
|
f.write(img._content)
|
|
|
|
f.close()
|
|
|
|
f.close()
|
|
|
|
gd.uploadFileToEbooksFolder(os.path.join(book_path, 'cover.jpg'), os.path.join(tmpDir, f.name))
|
|
|
|
else:
|
|
|
|
app.logger.info("Cover is saved on Google Drive")
|
|
|
|
# check if file path exists, otherwise create it, copy file to calibre path and delete temp file
|
|
|
|
return True
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
|
|
|
|
try:
|
|
|
|
f = open(os.path.join(config.config_calibre_dir, book_path, "cover.jpg"), "wb")
|
|
|
|
os.makedirs(filepath)
|
|
|
|
f.write(img.content)
|
|
|
|
except OSError:
|
|
|
|
f.close()
|
|
|
|
app.logger.error(u"Failed to create path for cover")
|
|
|
|
app.logger.info("Cover is saved")
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
img.save(os.path.join(filepath, saved_filename))
|
|
|
|
|
|
|
|
except OSError:
|
|
|
|
|
|
|
|
app.logger.error(u"Failed to store cover-file")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
except IOError:
|
|
|
|
|
|
|
|
app.logger.error(u"Cover-file is not a valid image file")
|
|
|
|
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# saves book cover to gdrive or locally
|
|
|
|
|
|
|
|
def save_cover(img, book_path):
|
|
|
|
|
|
|
|
content_type = img.headers.get('content-type')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if use_PIL:
|
|
|
|
|
|
|
|
if content_type not in ('image/jpeg', 'image/png', 'image/webp'):
|
|
|
|
|
|
|
|
app.logger.error("Only jpg/jpeg/png/webp files are supported as coverfile")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
# convert to jpg because calibre only supports jpg
|
|
|
|
|
|
|
|
if content_type in ('image/png', 'image/webp'):
|
|
|
|
|
|
|
|
if hasattr(img,'stream'):
|
|
|
|
|
|
|
|
imgc = Image.open(img.stream)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if content_type not in ('image/jpeg'):
|
|
|
|
|
|
|
|
app.logger.error("Only jpg/jpeg files are supported as coverfile")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ub.config.config_use_google_drive:
|
|
|
|
|
|
|
|
tmpDir = gettempdir()
|
|
|
|
|
|
|
|
if save_cover_from_filestorage(tmpDir, "uploaded_cover.jpg", img) is True:
|
|
|
|
|
|
|
|
gd.uploadFileToEbooksFolder(os.path.join(book_path, 'cover.jpg'),
|
|
|
|
|
|
|
|
os.path.join(tmpDir, "uploaded_cover.jpg"))
|
|
|
|
|
|
|
|
app.logger.info("Cover is saved on Google Drive")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return save_cover_from_filestorage(os.path.join(config.config_calibre_dir, book_path), "cover.jpg", img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do_download_file(book, book_format, data, headers):
|
|
|
|
def do_download_file(book, book_format, data, headers):
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
if config.config_use_google_drive:
|
|
|
|
startTime = time.time()
|
|
|
|
startTime = time.time()
|
|
|
@ -504,7 +555,6 @@ def do_download_file(book, book_format, data, headers):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_unrar(unrarLocation):
|
|
|
|
def check_unrar(unrarLocation):
|
|
|
|
error = False
|
|
|
|
error = False
|
|
|
|
if os.path.exists(unrarLocation):
|
|
|
|
if os.path.exists(unrarLocation):
|
|
|
@ -652,27 +702,22 @@ def fill_indexpage(page, database, db_filter, order, *join):
|
|
|
|
|
|
|
|
|
|
|
|
# read search results from calibre-database and return it (function is used for feed and simple search
|
|
|
|
# read search results from calibre-database and return it (function is used for feed and simple search
|
|
|
|
def get_search_results(term):
|
|
|
|
def get_search_results(term):
|
|
|
|
q = list()
|
|
|
|
def get_search_results(term):
|
|
|
|
authorterms = re.split("[, ]+", term)
|
|
|
|
db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
|
|
|
|
for authorterm in authorterms:
|
|
|
|
q = list()
|
|
|
|
q.append(db.Books.authors.any(db.or_(db.Authors.name.ilike("%" + authorterm + "%"),
|
|
|
|
authorterms = re.split("[, ]+", term)
|
|
|
|
db.Authors.name.ilike("%" + unidecode.unidecode(authorterm) + "%"))))
|
|
|
|
for authorterm in authorterms:
|
|
|
|
db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
|
|
|
|
q.append(db.Books.authors.any(db.func.lower(db.Authors.name).ilike("%" + authorterm + "%")))
|
|
|
|
db.Books.authors.any(db.or_(db.Authors.name.ilike("%" + term + "%"),
|
|
|
|
|
|
|
|
db.Authors.name.ilike("%" + unidecode.unidecode(term) + "%")))
|
|
|
|
db.Books.authors.any(db.func.lower(db.Authors.name).ilike("%" + term + "%"))
|
|
|
|
|
|
|
|
|
|
|
|
return db.session.query(db.Books).filter(common_filters()).filter(
|
|
|
|
return db.session.query(db.Books).filter(common_filters()).filter(
|
|
|
|
db.or_(db.Books.tags.any(db.Tags.name.ilike("%" + term + "%")),
|
|
|
|
db.or_(db.Books.tags.any(db.func.lower(db.Tags.name).ilike("%" + term + "%")),
|
|
|
|
db.Books.series.any(db.Series.name.ilike("%" + term + "%")),
|
|
|
|
db.Books.series.any(db.func.lower(db.Series.name).ilike("%" + term + "%")),
|
|
|
|
db.Books.authors.any(and_(*q)),
|
|
|
|
db.Books.authors.any(and_(*q)),
|
|
|
|
db.Books.publishers.any(db.Publishers.name.ilike("%" + term + "%")),
|
|
|
|
db.Books.publishers.any(db.func.lower(db.Publishers.name).ilike("%" + term + "%")),
|
|
|
|
db.Books.title.ilike("%" + term + "%"),
|
|
|
|
db.func.lower(db.Books.title).ilike("%" + term + "%")
|
|
|
|
db.Books.tags.any(db.Tags.name.ilike("%" + unidecode.unidecode(term) + "%")),
|
|
|
|
)).all()
|
|
|
|
db.Books.series.any(db.Series.name.ilike("%" + unidecode.unidecode(term) + "%")),
|
|
|
|
|
|
|
|
db.Books.publishers.any(db.Publishers.name.ilike("%" + unidecode.unidecode(term) + "%")),
|
|
|
|
|
|
|
|
db.Books.title.ilike("%" + unidecode.unidecode(term) + "%")
|
|
|
|
|
|
|
|
)).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_unique_other_books(library_books, author_books):
|
|
|
|
def get_unique_other_books(library_books, author_books):
|
|
|
|
# Get all identifiers (ISBN, Goodreads, etc) and filter author's books by that list so we show fewer duplicates
|
|
|
|
# Get all identifiers (ISBN, Goodreads, etc) and filter author's books by that list so we show fewer duplicates
|
|
|
|