Fix author rename on google drive

Bugfixes after testrun
This commit is contained in:
Ozzie Isaacs 2024-06-30 11:00:30 +02:00
parent e02610e2f0
commit daf8dd6f90
4 changed files with 802 additions and 890 deletions

View File

@ -21,6 +21,7 @@ import json
import shutil
import chardet
import ssl
import sqlite3
from flask import Response, stream_with_context
from sqlalchemy import create_engine
@ -258,13 +259,17 @@ def getEbooksFolderId(drive=None):
return gDriveId.gdrive_id
def getFile(pathId, fileName, drive):
metaDataFile = "'%s' in parents and trashed = false and title = '%s'" % (pathId, fileName.replace("'", r"\'"))
def getFile(pathId, fileName, drive, nocase):
metaDataFile = "'%s' in parents and trashed = false and title contains '%s'" % (pathId, fileName.replace("'", r"\'"))
fileList = drive.ListFile({'q': metaDataFile}).GetList()
if fileList.__len__() == 0:
return None
else:
if nocase:
return fileList[0]
for f in fileList:
if f['title'] == fileName:
return f
return None
def getFolderId(path, drive):
@ -303,7 +308,7 @@ def getFolderId(path, drive):
session.commit()
else:
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))
session.rollback()
except ApiRequestError as ex:
@ -314,7 +319,7 @@ def getFolderId(path, drive):
return currentFolderId
def getFileFromEbooksFolder(path, fileName):
def getFileFromEbooksFolder(path, fileName, nocase=False):
drive = getDrive(Gdrive.Instance().drive)
if path:
# sqlCheckPath=path if path[-1] =='/' else path + '/'
@ -322,7 +327,7 @@ def getFileFromEbooksFolder(path, fileName):
else:
folderId = getEbooksFolderId(drive)
if folderId:
return getFile(folderId, fileName, drive)
return getFile(folderId, fileName, drive, nocase)
else:
return None
@ -338,12 +343,12 @@ def downloadFile(path, filename, output):
f.GetContentFile(output)
def moveGdriveFolderRemote(origin_file, target_folder):
def moveGdriveFolderRemote(origin_file, target_folder, single_book=False):
drive = getDrive(Gdrive.Instance().drive)
previous_parents = ",".join([parent["id"] for parent in origin_file.get('parents')])
children = drive.auth.service.children().list(folderId=previous_parents).execute()
gFileTargetDir = getFileFromEbooksFolder(None, target_folder)
if not gFileTargetDir:
if single_book:
# gFileTargetDir = getFileFromEbooksFolder(None, target_folder, nocase=True)
gFileTargetDir = drive.CreateFile(
{'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}],
"mimeType": "application/vnd.google-apps.folder"})
@ -353,19 +358,20 @@ def moveGdriveFolderRemote(origin_file, target_folder):
addParents=gFileTargetDir['id'],
removeParents=previous_parents,
fields='id, parents').execute()
elif gFileTargetDir['title'] != target_folder:
deleteDatabasePath(gFileTargetDir['title'])
elif origin_file['title'] != target_folder:
#gFileTargetDir = getFileFromEbooksFolder(None, target_folder, nocase=True)
#if gFileTargetDir:
deleteDatabasePath(origin_file['title'])
# Folder is not existing, create, and move folder
drive.auth.service.files().patch(fileId=origin_file['id'],
body={'title': target_folder},
fields='title').execute()
else:
'''else:
# Move the file to the new folder
drive.auth.service.files().update(fileId=origin_file['id'],
addParents=gFileTargetDir['id'],
removeParents=previous_parents,
fields='id, parents').execute()
fields='id, parents').execute()'''
# if previous_parents has no children anymore, delete original fileparent
if len(children['items']) == 1:
deleteDatabaseEntry(previous_parents)

View File

@ -479,7 +479,10 @@ def update_dir_structure_file(book_id, calibre_path, original_filepath, new_auth
title_dir = local_book.path.split('/')[1]
new_title_dir = get_valid_filename(local_book.title, chars=96) + " (" + str(book_id) + ")"
new_author_dir = get_valid_filename(new_author, chars=96)
if new_author:
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:
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
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:
gd.moveGdriveFolderRemote(g_file, new_authordir)
gd.moveGdriveFolderRemote(g_file, new_authordir, single_book=True)
book.path = new_authordir + '/' + book.path.split('/')[1]
gd.updateDatabaseOnEdit(g_file['id'], book.path)
else:

View File

@ -107,7 +107,7 @@ def add_security_headers(resp):
resp.headers['X-Content-Type-Options'] = 'nosniff'
resp.headers['X-Frame-Options'] = 'SAMEORIGIN'
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

File diff suppressed because it is too large Load Diff