|
|
@ -123,38 +123,36 @@ def revoke_watch_gdrive():
|
|
|
|
|
|
|
|
|
|
|
|
@gdrive.route("/gdrive/watch/callback", methods=['GET', 'POST'])
|
|
|
|
@gdrive.route("/gdrive/watch/callback", methods=['GET', 'POST'])
|
|
|
|
def on_received_watch_confirmation():
|
|
|
|
def on_received_watch_confirmation():
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
return redirect(url_for('admin.configuration'))
|
|
|
|
|
|
|
|
|
|
|
|
log.debug('%r', request.headers)
|
|
|
|
log.debug('%r', request.headers)
|
|
|
|
if request.headers.get('X-Goog-Channel-Token') == gdrive_watch_callback_token \
|
|
|
|
log.debug('%r', request.data)
|
|
|
|
and request.headers.get('X-Goog-Resource-State') == 'change' \
|
|
|
|
log.info('Change received from gdrive')
|
|
|
|
and request.data:
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = request.data
|
|
|
|
j = json.loads(request.data)
|
|
|
|
|
|
|
|
log.info('Getting change details')
|
|
|
|
def updateMetaData():
|
|
|
|
response = gdriveutils.getChangeById(gdriveutils.Gdrive.Instance().drive, j['id'])
|
|
|
|
log.info('Change received from gdrive')
|
|
|
|
log.debug('%r', response)
|
|
|
|
log.debug('%r', data)
|
|
|
|
if response:
|
|
|
|
try:
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
j = json.loads(data)
|
|
|
|
dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
|
|
|
|
log.info('Getting change details')
|
|
|
|
else:
|
|
|
|
response = gdriveutils.getChangeById(gdriveutils.Gdrive.Instance().drive, j['id'])
|
|
|
|
dbpath = os.path.join(config.config_calibre_dir, "metadata.db").encode()
|
|
|
|
log.debug('%r', response)
|
|
|
|
if not response['deleted'] and response['file']['title'] == 'metadata.db' \
|
|
|
|
if response:
|
|
|
|
and response['file']['md5Checksum'] != hashlib.md5(dbpath):
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
tmpDir = tempfile.gettempdir()
|
|
|
|
dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
|
|
|
|
log.info('Database file updated')
|
|
|
|
else:
|
|
|
|
copyfile(dbpath, os.path.join(tmpDir, "metadata.db_" + str(current_milli_time())))
|
|
|
|
dbpath = os.path.join(config.config_calibre_dir, "metadata.db").encode()
|
|
|
|
log.info('Backing up existing and downloading updated metadata.db')
|
|
|
|
if not response['deleted'] and response['file']['title'] == 'metadata.db' \
|
|
|
|
gdriveutils.downloadFile(None, "metadata.db", os.path.join(tmpDir, "tmp_metadata.db"))
|
|
|
|
and response['file']['md5Checksum'] != hashlib.md5(dbpath):
|
|
|
|
log.info('Setting up new DB')
|
|
|
|
tmpDir = tempfile.gettempdir()
|
|
|
|
# prevent error on windows, as os.rename does on exisiting files
|
|
|
|
log.info('Database file updated')
|
|
|
|
move(os.path.join(tmpDir, "tmp_metadata.db"), dbpath)
|
|
|
|
copyfile(dbpath, os.path.join(tmpDir, "metadata.db_" + str(current_milli_time())))
|
|
|
|
calibre_db.reconnect_db(config, ub.app_DB_path)
|
|
|
|
log.info('Backing up existing and downloading updated metadata.db')
|
|
|
|
except Exception as e:
|
|
|
|
gdriveutils.downloadFile(None, "metadata.db", os.path.join(tmpDir, "tmp_metadata.db"))
|
|
|
|
log.exception(e)
|
|
|
|
log.info('Setting up new DB')
|
|
|
|
|
|
|
|
# prevent error on windows, as os.rename does on exisiting files
|
|
|
|
|
|
|
|
move(os.path.join(tmpDir, "tmp_metadata.db"), dbpath)
|
|
|
|
|
|
|
|
calibre_db.reconnect_db(config, ub.app_DB_path)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
log.exception(e)
|
|
|
|
|
|
|
|
updateMetaData()
|
|
|
|
|
|
|
|
return ''
|
|
|
|
return ''
|
|
|
|