Fix author rename on google drive
Bugfixes after testrun
This commit is contained in:
parent
e02610e2f0
commit
daf8dd6f90
|
@ -21,6 +21,7 @@ import json
|
||||||
import shutil
|
import shutil
|
||||||
import chardet
|
import chardet
|
||||||
import ssl
|
import ssl
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
from flask import Response, stream_with_context
|
from flask import Response, stream_with_context
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
|
@ -258,13 +259,17 @@ def getEbooksFolderId(drive=None):
|
||||||
return gDriveId.gdrive_id
|
return gDriveId.gdrive_id
|
||||||
|
|
||||||
|
|
||||||
def getFile(pathId, fileName, drive):
|
def getFile(pathId, fileName, drive, nocase):
|
||||||
metaDataFile = "'%s' in parents and trashed = false and title = '%s'" % (pathId, fileName.replace("'", r"\'"))
|
metaDataFile = "'%s' in parents and trashed = false and title contains '%s'" % (pathId, fileName.replace("'", r"\'"))
|
||||||
fileList = drive.ListFile({'q': metaDataFile}).GetList()
|
fileList = drive.ListFile({'q': metaDataFile}).GetList()
|
||||||
if fileList.__len__() == 0:
|
if fileList.__len__() == 0:
|
||||||
return None
|
return None
|
||||||
else:
|
if nocase:
|
||||||
return fileList[0]
|
return fileList[0]
|
||||||
|
for f in fileList:
|
||||||
|
if f['title'] == fileName:
|
||||||
|
return f
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def getFolderId(path, drive):
|
def getFolderId(path, drive):
|
||||||
|
@ -303,7 +308,7 @@ def getFolderId(path, drive):
|
||||||
session.commit()
|
session.commit()
|
||||||
else:
|
else:
|
||||||
currentFolderId = storedPathName.gdrive_id
|
currentFolderId = storedPathName.gdrive_id
|
||||||
except (OperationalError, IntegrityError, StaleDataError) as ex:
|
except (OperationalError, IntegrityError, StaleDataError, sqlite3.IntegrityError) as ex:
|
||||||
log.error_or_exception('Database error: {}'.format(ex))
|
log.error_or_exception('Database error: {}'.format(ex))
|
||||||
session.rollback()
|
session.rollback()
|
||||||
except ApiRequestError as ex:
|
except ApiRequestError as ex:
|
||||||
|
@ -314,7 +319,7 @@ def getFolderId(path, drive):
|
||||||
return currentFolderId
|
return currentFolderId
|
||||||
|
|
||||||
|
|
||||||
def getFileFromEbooksFolder(path, fileName):
|
def getFileFromEbooksFolder(path, fileName, nocase=False):
|
||||||
drive = getDrive(Gdrive.Instance().drive)
|
drive = getDrive(Gdrive.Instance().drive)
|
||||||
if path:
|
if path:
|
||||||
# sqlCheckPath=path if path[-1] =='/' else path + '/'
|
# sqlCheckPath=path if path[-1] =='/' else path + '/'
|
||||||
|
@ -322,7 +327,7 @@ def getFileFromEbooksFolder(path, fileName):
|
||||||
else:
|
else:
|
||||||
folderId = getEbooksFolderId(drive)
|
folderId = getEbooksFolderId(drive)
|
||||||
if folderId:
|
if folderId:
|
||||||
return getFile(folderId, fileName, drive)
|
return getFile(folderId, fileName, drive, nocase)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -338,12 +343,12 @@ def downloadFile(path, filename, output):
|
||||||
f.GetContentFile(output)
|
f.GetContentFile(output)
|
||||||
|
|
||||||
|
|
||||||
def moveGdriveFolderRemote(origin_file, target_folder):
|
def moveGdriveFolderRemote(origin_file, target_folder, single_book=False):
|
||||||
drive = getDrive(Gdrive.Instance().drive)
|
drive = getDrive(Gdrive.Instance().drive)
|
||||||
previous_parents = ",".join([parent["id"] for parent in origin_file.get('parents')])
|
previous_parents = ",".join([parent["id"] for parent in origin_file.get('parents')])
|
||||||
children = drive.auth.service.children().list(folderId=previous_parents).execute()
|
children = drive.auth.service.children().list(folderId=previous_parents).execute()
|
||||||
gFileTargetDir = getFileFromEbooksFolder(None, target_folder)
|
if single_book:
|
||||||
if not gFileTargetDir:
|
# gFileTargetDir = getFileFromEbooksFolder(None, target_folder, nocase=True)
|
||||||
gFileTargetDir = drive.CreateFile(
|
gFileTargetDir = drive.CreateFile(
|
||||||
{'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}],
|
{'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}],
|
||||||
"mimeType": "application/vnd.google-apps.folder"})
|
"mimeType": "application/vnd.google-apps.folder"})
|
||||||
|
@ -353,19 +358,20 @@ def moveGdriveFolderRemote(origin_file, target_folder):
|
||||||
addParents=gFileTargetDir['id'],
|
addParents=gFileTargetDir['id'],
|
||||||
removeParents=previous_parents,
|
removeParents=previous_parents,
|
||||||
fields='id, parents').execute()
|
fields='id, parents').execute()
|
||||||
|
elif origin_file['title'] != target_folder:
|
||||||
elif gFileTargetDir['title'] != target_folder:
|
#gFileTargetDir = getFileFromEbooksFolder(None, target_folder, nocase=True)
|
||||||
deleteDatabasePath(gFileTargetDir['title'])
|
#if gFileTargetDir:
|
||||||
|
deleteDatabasePath(origin_file['title'])
|
||||||
# Folder is not existing, create, and move folder
|
# Folder is not existing, create, and move folder
|
||||||
drive.auth.service.files().patch(fileId=origin_file['id'],
|
drive.auth.service.files().patch(fileId=origin_file['id'],
|
||||||
body={'title': target_folder},
|
body={'title': target_folder},
|
||||||
fields='title').execute()
|
fields='title').execute()
|
||||||
else:
|
'''else:
|
||||||
# Move the file to the new folder
|
# Move the file to the new folder
|
||||||
drive.auth.service.files().update(fileId=origin_file['id'],
|
drive.auth.service.files().update(fileId=origin_file['id'],
|
||||||
addParents=gFileTargetDir['id'],
|
addParents=gFileTargetDir['id'],
|
||||||
removeParents=previous_parents,
|
removeParents=previous_parents,
|
||||||
fields='id, parents').execute()
|
fields='id, parents').execute()'''
|
||||||
# if previous_parents has no children anymore, delete original fileparent
|
# if previous_parents has no children anymore, delete original fileparent
|
||||||
if len(children['items']) == 1:
|
if len(children['items']) == 1:
|
||||||
deleteDatabaseEntry(previous_parents)
|
deleteDatabaseEntry(previous_parents)
|
||||||
|
|
|
@ -479,7 +479,10 @@ def update_dir_structure_file(book_id, calibre_path, original_filepath, new_auth
|
||||||
title_dir = local_book.path.split('/')[1]
|
title_dir = local_book.path.split('/')[1]
|
||||||
|
|
||||||
new_title_dir = get_valid_filename(local_book.title, chars=96) + " (" + str(book_id) + ")"
|
new_title_dir = get_valid_filename(local_book.title, chars=96) + " (" + str(book_id) + ")"
|
||||||
|
if new_author:
|
||||||
new_author_dir = get_valid_filename(new_author, chars=96)
|
new_author_dir = get_valid_filename(new_author, chars=96)
|
||||||
|
else:
|
||||||
|
new_author = new_author_dir = author_dir
|
||||||
|
|
||||||
if title_dir != new_title_dir or author_dir != new_author_dir or original_filepath:
|
if title_dir != new_title_dir or author_dir != new_author_dir or original_filepath:
|
||||||
error = move_files_on_change(calibre_path,
|
error = move_files_on_change(calibre_path,
|
||||||
|
@ -533,9 +536,9 @@ def update_dir_structure_gdrive(book_id, first_author):
|
||||||
return _('File %(file)s not found on Google Drive', file=book.path) # file not found
|
return _('File %(file)s not found on Google Drive', file=book.path) # file not found
|
||||||
|
|
||||||
if authordir != new_authordir:
|
if authordir != new_authordir:
|
||||||
g_file = gd.getFileFromEbooksFolder(os.path.dirname(book.path), new_titledir)
|
g_file = gd.getFileFromEbooksFolder(authordir, new_titledir)
|
||||||
if g_file:
|
if g_file:
|
||||||
gd.moveGdriveFolderRemote(g_file, new_authordir)
|
gd.moveGdriveFolderRemote(g_file, new_authordir, single_book=True)
|
||||||
book.path = new_authordir + '/' + book.path.split('/')[1]
|
book.path = new_authordir + '/' + book.path.split('/')[1]
|
||||||
gd.updateDatabaseOnEdit(g_file['id'], book.path)
|
gd.updateDatabaseOnEdit(g_file['id'], book.path)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -107,7 +107,7 @@ def add_security_headers(resp):
|
||||||
resp.headers['X-Content-Type-Options'] = 'nosniff'
|
resp.headers['X-Content-Type-Options'] = 'nosniff'
|
||||||
resp.headers['X-Frame-Options'] = 'SAMEORIGIN'
|
resp.headers['X-Frame-Options'] = 'SAMEORIGIN'
|
||||||
resp.headers['X-XSS-Protection'] = '1; mode=block'
|
resp.headers['X-XSS-Protection'] = '1; mode=block'
|
||||||
resp.headers['Strict-Transport-Security'] = 'max-age=31536000;'
|
resp.headers['Strict-Transport-Security'] = 'max-age=31536000';
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user