2019-07-13 20:45:48 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 OzzieIsaacs, cervinko, jkrehm, bodybybuddha, ok11,
|
|
|
|
# andy29485, idalin, Kyosfonica, wuqi, Kennyl, lemmsh,
|
|
|
|
# falgh1, grunjol, csitko, ytils, xybydy, trasba, vrabe,
|
|
|
|
# ruben-herold, marblepebble, JackED42, SiphonSquirrel,
|
|
|
|
# apetresc, nanu-c, mutschler
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import division, print_function, unicode_literals
|
|
|
|
import os
|
2019-12-14 22:22:27 +01:00
|
|
|
import sys
|
2019-07-13 20:45:48 +02:00
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import tempfile
|
|
|
|
from uuid import uuid4
|
|
|
|
from time import time
|
|
|
|
from shutil import move, copyfile
|
|
|
|
|
|
|
|
from flask import Blueprint, flash, request, redirect, url_for, abort
|
|
|
|
from flask_babel import gettext as _
|
|
|
|
from flask_login import login_required
|
|
|
|
|
2020-05-21 22:26:06 +02:00
|
|
|
from . import logger, gdriveutils, config, ub, calibre_db
|
2020-12-12 11:23:17 +01:00
|
|
|
from .admin import admin_required
|
2019-07-13 20:45:48 +02:00
|
|
|
|
2020-12-10 14:41:45 +01:00
|
|
|
gdrive = Blueprint('gdrive', __name__, url_prefix='/gdrive')
|
2019-07-13 20:45:48 +02:00
|
|
|
log = logger.create()
|
|
|
|
|
2020-07-21 18:01:38 +02:00
|
|
|
try:
|
|
|
|
from googleapiclient.errors import HttpError
|
|
|
|
except ImportError as err:
|
2020-09-20 11:44:03 +02:00
|
|
|
log.debug("Cannot import googleapiclient, using GDrive will not work: %s", err)
|
2020-07-21 18:01:38 +02:00
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
current_milli_time = lambda: int(round(time() * 1000))
|
|
|
|
|
2021-03-14 15:06:09 +01:00
|
|
|
gdrive_watch_callback_token = 'target=calibreweb-watch_files' #nosec
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
|
2020-12-10 14:41:45 +01:00
|
|
|
@gdrive.route("/authenticate")
|
2019-07-13 20:45:48 +02:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def authenticate_google_drive():
|
|
|
|
try:
|
|
|
|
authUrl = gdriveutils.Gauth.Instance().auth.GetAuthUrl()
|
|
|
|
except gdriveutils.InvalidConfigError:
|
|
|
|
flash(_(u'Google Drive setup not completed, try to deactivate and activate Google Drive again'),
|
|
|
|
category="error")
|
|
|
|
return redirect(url_for('web.index'))
|
|
|
|
return redirect(authUrl)
|
|
|
|
|
|
|
|
|
2020-12-10 14:41:45 +01:00
|
|
|
@gdrive.route("/callback")
|
2019-07-13 20:45:48 +02:00
|
|
|
def google_drive_callback():
|
|
|
|
auth_code = request.args.get('code')
|
|
|
|
if not auth_code:
|
|
|
|
abort(403)
|
|
|
|
try:
|
|
|
|
credentials = gdriveutils.Gauth.Instance().auth.flow.step2_exchange(auth_code)
|
|
|
|
with open(gdriveutils.CREDENTIALS, 'w') as f:
|
|
|
|
f.write(credentials.to_json())
|
2020-07-21 18:01:38 +02:00
|
|
|
except (ValueError, AttributeError) as error:
|
2019-07-13 20:45:48 +02:00
|
|
|
log.error(error)
|
2021-05-26 13:35:35 +02:00
|
|
|
return redirect(url_for('admin.db_configuration'))
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
|
2020-12-10 14:41:45 +01:00
|
|
|
@gdrive.route("/watch/subscribe")
|
2019-07-13 20:45:48 +02:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def watch_gdrive():
|
|
|
|
if not config.config_google_drive_watch_changes_response:
|
|
|
|
with open(gdriveutils.CLIENT_SECRETS, 'r') as settings:
|
|
|
|
filedata = json.load(settings)
|
2020-12-10 14:41:45 +01:00
|
|
|
address = filedata['web']['redirect_uris'][0].rstrip('/').replace('/gdrive/callback', '/gdrive/watch/callback')
|
2019-07-13 20:45:48 +02:00
|
|
|
notification_id = str(uuid4())
|
|
|
|
try:
|
|
|
|
result = gdriveutils.watchChange(gdriveutils.Gdrive.Instance().drive, notification_id,
|
|
|
|
'web_hook', address, gdrive_watch_callback_token, current_milli_time() + 604800*1000)
|
2020-08-15 15:07:03 +02:00
|
|
|
config.config_google_drive_watch_changes_response = result
|
2019-07-13 20:45:48 +02:00
|
|
|
config.save()
|
|
|
|
except HttpError as e:
|
|
|
|
reason=json.loads(e.content)['error']['errors'][0]
|
|
|
|
if reason['reason'] == u'push.webhookUrlUnauthorized':
|
2020-12-10 14:41:45 +01:00
|
|
|
flash(_(u'Callback domain is not verified, '
|
|
|
|
u'please follow steps to verify domain in google developer console'), category="error")
|
2019-07-13 20:45:48 +02:00
|
|
|
else:
|
|
|
|
flash(reason['message'], category="error")
|
|
|
|
|
2021-05-26 13:35:35 +02:00
|
|
|
return redirect(url_for('admin.db_configuration'))
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
|
2020-12-10 14:41:45 +01:00
|
|
|
@gdrive.route("/watch/revoke")
|
2019-07-13 20:45:48 +02:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def revoke_watch_gdrive():
|
|
|
|
last_watch_response = config.config_google_drive_watch_changes_response
|
|
|
|
if last_watch_response:
|
|
|
|
try:
|
|
|
|
gdriveutils.stopChannel(gdriveutils.Gdrive.Instance().drive, last_watch_response['id'],
|
|
|
|
last_watch_response['resourceId'])
|
|
|
|
except HttpError:
|
|
|
|
pass
|
2020-07-21 18:01:38 +02:00
|
|
|
config.config_google_drive_watch_changes_response = {}
|
2019-07-13 20:45:48 +02:00
|
|
|
config.save()
|
2021-05-26 13:35:35 +02:00
|
|
|
return redirect(url_for('admin.db_configuration'))
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
|
2020-12-10 14:41:45 +01:00
|
|
|
@gdrive.route("/watch/callback", methods=['GET', 'POST'])
|
2019-07-13 20:45:48 +02:00
|
|
|
def on_received_watch_confirmation():
|
2020-12-08 13:34:15 +01:00
|
|
|
if not config.config_google_drive_watch_changes_response:
|
|
|
|
return ''
|
2020-10-27 19:39:11 +01:00
|
|
|
if request.headers.get('X-Goog-Channel-Token') != gdrive_watch_callback_token \
|
|
|
|
or request.headers.get('X-Goog-Resource-State') != 'change' \
|
|
|
|
or not request.data:
|
2020-12-11 13:34:29 +01:00
|
|
|
return ''
|
2020-10-27 19:39:11 +01:00
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
log.debug('%r', request.headers)
|
2020-10-27 19:39:11 +01:00
|
|
|
log.debug('%r', request.data)
|
|
|
|
log.info('Change received from gdrive')
|
|
|
|
|
|
|
|
try:
|
|
|
|
j = json.loads(request.data)
|
|
|
|
log.info('Getting change details')
|
|
|
|
response = gdriveutils.getChangeById(gdriveutils.Gdrive.Instance().drive, j['id'])
|
|
|
|
log.debug('%r', response)
|
|
|
|
if response:
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
|
|
|
|
else:
|
|
|
|
dbpath = os.path.join(config.config_calibre_dir, "metadata.db").encode()
|
|
|
|
if not response['deleted'] and response['file']['title'] == 'metadata.db' \
|
2021-03-15 13:48:05 +01:00
|
|
|
and response['file']['md5Checksum'] != hashlib.md5(dbpath): # nosec
|
2020-12-11 13:34:29 +01:00
|
|
|
tmp_dir = os.path.join(tempfile.gettempdir(), 'calibre_web')
|
|
|
|
if not os.path.isdir(tmp_dir):
|
|
|
|
os.mkdir(tmp_dir)
|
|
|
|
|
2020-10-27 19:39:11 +01:00
|
|
|
log.info('Database file updated')
|
2020-12-11 13:34:29 +01:00
|
|
|
copyfile(dbpath, os.path.join(tmp_dir, "metadata.db_" + str(current_milli_time())))
|
2020-10-27 19:39:11 +01:00
|
|
|
log.info('Backing up existing and downloading updated metadata.db')
|
2020-12-11 13:34:29 +01:00
|
|
|
gdriveutils.downloadFile(None, "metadata.db", os.path.join(tmp_dir, "tmp_metadata.db"))
|
2020-10-27 19:39:11 +01:00
|
|
|
log.info('Setting up new DB')
|
2020-12-11 13:34:29 +01:00
|
|
|
# prevent error on windows, as os.rename does on existing files, also allow cross hdd move
|
|
|
|
move(os.path.join(tmp_dir, "tmp_metadata.db"), dbpath)
|
2020-10-27 19:39:11 +01:00
|
|
|
calibre_db.reconnect_db(config, ub.app_DB_path)
|
2021-04-04 19:40:34 +02:00
|
|
|
except Exception as ex:
|
|
|
|
log.debug_or_exception(ex)
|
2019-07-13 20:45:48 +02:00
|
|
|
return ''
|