From e02610e2f0c5ae54c6af74c626811b008176a8d3 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 29 Jun 2024 10:50:44 +0200 Subject: [PATCH] Bugfixes change author --- cps/editbooks.py | 3 +++ cps/gdriveutils.py | 10 ++++++++++ cps/helper.py | 46 +++++++++++++++++++++++++++------------------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index 957e97dc..64786c99 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -264,6 +264,9 @@ def upload(): title_dir, meta.file_path, meta.extension.lower()) + for file_format in db_book.data: + file_format.name = (helper.get_valid_filename(title, chars=42) + ' - ' + + helper.get_valid_filename(input_authors[0], chars=42)) else: error = helper.update_dir_structure(book_id, config.get_book_path(), diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 926e5da3..c765b9ab 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -269,6 +269,7 @@ def getFile(pathId, fileName, drive): def getFolderId(path, drive): # drive = getDrive(drive) + log.info(f"GetFolder: {path}") currentFolderId = None try: currentFolderId = getEbooksFolderId(drive) @@ -354,6 +355,7 @@ def moveGdriveFolderRemote(origin_file, target_folder): fields='id, parents').execute() elif gFileTargetDir['title'] != target_folder: + deleteDatabasePath(gFileTargetDir['title']) # Folder is not existing, create, and move folder drive.auth.service.files().patch(fileId=origin_file['id'], body={'title': target_folder}, @@ -563,6 +565,14 @@ def deleteDatabaseEntry(ID): log.error_or_exception('Database error: {}'.format(ex)) session.rollback() +def deleteDatabasePath(Pathname): + session.query(GdriveId).filter(GdriveId.path.contains(Pathname)).delete() + try: + session.commit() + except OperationalError as ex: + log.error_or_exception('Database error: {}'.format(ex)) + session.rollback() + # Gets cover file from gdrive # ToDo: Check is this right everyone get read permissions on cover files? diff --git a/cps/helper.py b/cps/helper.py index abf5bb70..b70cbbe0 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -411,7 +411,7 @@ def rename_all_files_on_change(one_book, new_path, old_path, all_new_name, gdriv file_format.name = all_new_name -def rename_all_authors(first_author, renamed_author, calibre_path="", localbook=None, gdrive=False): +'''def rename_all_authors(first_author, renamed_author, calibre_path="", localbook=None, gdrive=False): # Create new_author_dir from parameter or from database # Create new title_dir from database and add id if first_author: @@ -424,6 +424,7 @@ def rename_all_authors(first_author, renamed_author, calibre_path="", localbook= g_file = gd.getFileFromEbooksFolder(None, old_author_dir) if g_file: gd.moveGdriveFolderRemote(g_file, new_author_rename_dir) + gd.updateDatabaseOnEdit(g_file['id'], new_author_rename_dir) else: if os.path.isdir(os.path.join(calibre_path, old_author_dir)): old_author_path = os.path.join(calibre_path, old_author_dir) @@ -437,7 +438,7 @@ def rename_all_authors(first_author, renamed_author, calibre_path="", localbook= src=old_author_path, dest=new_author_path, error=str(ex)) else: new_authordir = get_valid_filename(localbook.authors[0].name, chars=96) - return new_authordir + return new_authordir''' def rename_author_path(first_author, old_author_dir, renamed_author, calibre_path="", gdrive=False): @@ -465,7 +466,7 @@ def rename_author_path(first_author, old_author_dir, renamed_author, calibre_pat return new_authordir # Moves files in file storage during author/title rename, or from temp dir to file storage -def update_dir_structure_file(book_id, calibre_path, original_filepath, db_filename): +def update_dir_structure_file(book_id, calibre_path, original_filepath, new_author, db_filename): # get book database entry from id, if original path overwrite source with original_filepath local_book = calibre_db.get_book(book_id) if original_filepath: @@ -478,18 +479,20 @@ def update_dir_structure_file(book_id, calibre_path, original_filepath, db_filen 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 title_dir != new_title_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, - author_dir, + new_author_dir, new_title_dir, local_book, db_filename, original_filepath, path) - new_path = os.path.join(calibre_path, author_dir, new_title_dir).replace('\\', '/') + # old_path = os.path.join(calibre_path, author_dir, new_title_dir).replace('\\', '/') + new_path = os.path.join(calibre_path, new_author_dir, new_title_dir).replace('\\', '/') all_new_name = get_valid_filename(local_book.title, chars=42) + ' - ' \ - + get_valid_filename(author_dir, chars=42) + + get_valid_filename(new_author, chars=42) # Book folder already moved, only files need to be renamed rename_all_files_on_change(local_book, new_path, new_path, all_new_name) @@ -511,13 +514,13 @@ def upload_new_file_gdrive(book_id, first_author, title, title_dir, original_fil return False # rename_files_on_change(first_author, renamed_author, local_book=book, gdrive=True) -def update_dir_structure_gdrive(book_id): +def update_dir_structure_gdrive(book_id, first_author): book = calibre_db.get_book(book_id) authordir = book.path.split('/')[0] titledir = book.path.split('/')[1] # new_authordir = rename_all_authors(first_author, renamed_author, gdrive=True) - # new_authordir = get_valid_filename(book.title, chars=96) + new_authordir = get_valid_filename(first_author, chars=96) new_titledir = get_valid_filename(book.title, chars=96) + " (" + str(book_id) + ")" if titledir != new_titledir: @@ -529,7 +532,7 @@ def update_dir_structure_gdrive(book_id): else: 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) if g_file: gd.moveGdriveFolderRemote(g_file, new_authordir) @@ -537,25 +540,24 @@ def update_dir_structure_gdrive(book_id): gd.updateDatabaseOnEdit(g_file['id'], book.path) else: return _('File %(file)s not found on Google Drive', file=authordir) # file not found''' - if titledir != new_titledir: + if titledir != new_titledir or authordir != new_authordir : all_new_name = get_valid_filename(book.title, chars=42) + ' - ' \ - + get_valid_filename(authordir, chars=42) + + get_valid_filename(new_authordir, chars=42) rename_all_files_on_change(book, book.path, book.path, all_new_name, gdrive=True) # todo: Move filenames on gdrive # change location in database to new author/title path # book.path = os.path.join(authordir, new_titledir).replace('\\', '/') return False -def move_files_on_change(calibre_path, new_authordir, new_titledir, localbook, db_filename, original_filepath, path): - new_authordir = get_valid_filename(new_authordir, chars=96) - new_path = os.path.join(calibre_path, new_authordir, new_titledir) - new_name = get_valid_filename(localbook.title, chars=96) + ' - ' + new_authordir +def move_files_on_change(calibre_path, new_author_dir, new_titledir, localbook, db_filename, original_filepath, path): + new_path = os.path.join(calibre_path, new_author_dir, new_titledir) + # new_name = get_valid_filename(localbook.title, chars=96) + ' - ' + new_author_dir try: if original_filepath: if not os.path.isdir(new_path): os.makedirs(new_path) shutil.move(os.path.normcase(original_filepath), os.path.normcase(os.path.join(new_path, db_filename))) - log.debug("Moving title: %s to %s/%s", original_filepath, new_path, new_name) + log.debug("Moving title: %s to %s/%s", original_filepath, new_path) else: # Check new path is not valid path if not os.path.exists(new_path): @@ -569,8 +571,13 @@ def move_files_on_change(calibre_path, new_authordir, new_titledir, localbook, d for file in file_list: shutil.move(os.path.normcase(os.path.join(dir_name, file)), os.path.normcase(os.path.join(new_path + dir_name[len(path):], file))) + if not os.listdir(os.path.split(path)[0]): + try: + shutil.rmtree(os.path.split(path)[0]) + except (IOError, OSError) as ex: + log.error("Deleting authorpath for book %s failed: %s", localbook.id, ex) # change location in database to new author/title path - localbook.path = os.path.join(new_authordir, new_titledir).replace('\\', '/') + localbook.path = os.path.join(new_author_dir, new_titledir).replace('\\', '/') except OSError as ex: log.error_or_exception("Rename title from {} to {} failed with error: {}".format(path, new_path, ex)) return _("Rename title from: '%(src)s' to '%(dest)s' failed with error: %(error)s", @@ -729,6 +736,7 @@ def update_dir_structure(book_id, return update_dir_structure_file(book_id, calibre_path, original_filepath, + first_author, db_filename) @@ -949,7 +957,7 @@ def do_download_file(book, book_format, client, data, headers): download_name = filename = None if config.config_use_google_drive: # startTime = time.time() - df = gd.getFileFromEbooksFolder(book.path, book_name + "." + book_format) + df = gd.getFileFromEbooksFolder(book.path, data.name + "." + book_format) # log.debug('%s', time.time() - startTime) if df: if config.config_embed_metadata and (