Skip to content

Commit 70e2b51

Browse files
committed
Start adding hash on import/rename
1 parent 7812d67 commit 70e2b51

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

root/app/calibre-web/cps/editbooks.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from .file_helper import validate_mime_type
4949
from .usermanagement import user_login_required, login_required_if_no_ano
5050
from .string_helper import strip_whitespaces
51+
from kosync import update_document_hash
5152

5253
editbook = Blueprint('edit-book', __name__)
5354
log = logger.create()
@@ -145,7 +146,8 @@ def upload():
145146
calibre_db.set_metadata_dirty(book_id)
146147
# save data to database, reread data
147148
calibre_db.session.commit()
148-
149+
# update document_hash for kosync
150+
update_document_hash(book_id)
149151
if config.config_use_google_drive:
150152
gdriveutils.updateGdriveCalibreFromLocal()
151153
if error:
@@ -307,6 +309,7 @@ def edit_list_book(param):
307309
book.last_modified = datetime.now(timezone.utc)
308310

309311
calibre_db.session.commit()
312+
update_document_hash(book.id)
310313
# revert change for sort if automatic fields link is deactivated
311314
if param == 'title' and vals.get('checkT') == "false":
312315
book.sort = sort_param
@@ -424,6 +427,7 @@ def table_xchange_author_title():
424427
calibre_db.set_metadata_dirty(book.id)
425428
try:
426429
calibre_db.session.commit()
430+
update_document_hash(book.id)
427431
except (OperationalError, IntegrityError, StaleDataError) as e:
428432
calibre_db.session.rollback()
429433
log.error_or_exception("Database error: {}".format(e))
@@ -552,6 +556,7 @@ def do_edit_book(book_id, upload_formats=None):
552556

553557
calibre_db.session.merge(book)
554558
calibre_db.session.commit()
559+
update_document_hash(book.id)
555560
if config.config_use_google_drive:
556561
gdriveutils.updateGdriveCalibreFromLocal()
557562
if edit_error is not True and title_author_error is not True and cover_upload_success is not False:

root/app/calibre-web/cps/kosync.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,30 @@
2424

2525
log = logger.create()
2626

27+
def filename_hash(book_data):
28+
filename = f'{book_data.name}.{book_data.format.lower()}'
29+
hash = md5(filename.encode()).hexdigest()
30+
return hash
31+
2732
def populate_document_hashes():
28-
start = time.time()
2933
books_missing_hash = calibre_db.session.query(db.Data).outerjoin(ub.KosyncBooks,db.Data.id == ub.KosyncBooks.data_id).where(ub.KosyncBooks.data_id == None).all()
30-
log.debug(len(books_missing_hash))
3134
for missing_hash in books_missing_hash:
32-
filename = f'{missing_hash.name}.{missing_hash.format.lower()}'
33-
hash = md5(filename.encode()).hexdigest()
35+
hash = filename_hash(missing_hash)
3436
kosync_book = ub.KosyncBooks(data_id=missing_hash.id, book=missing_hash.book, document_hash=hash)
3537
ub.session.add(kosync_book)
3638
ub.session_commit()
37-
end = time.time()
38-
log.debug(f'Migrated {len(books_missing_hash)} records in {(end-start) * 10**3}ms')
39+
40+
def update_document_hash(book_id):
41+
books_formats = calibre_db.session.query(db.Data).where(db.Data.book == book_id).all()
42+
for format in books_formats:
43+
sync = ub.session.query(ub.KosyncBooks.data_id == format.id).scalar()
44+
if sync:
45+
sync.document_hash = filename_hash(format)
46+
if not sync:
47+
hash = filename_hash(format)
48+
sync = ub.KosyncBooks(data_id = format.id, book = format.book, document_hash = hash)
49+
ub.session.add(sync)
50+
ub.session_commit()
3951

4052
def requires_kosync_auth(f):
4153
@wraps(f)

0 commit comments

Comments
 (0)