(session_id:str)
| 461 | return hashlib.md5(data_str.encode('utf-8')).hexdigest() |
| 462 | |
| 463 | def compare_checksums(session_id:str)->tuple[bool, str|None]: |
| 464 | try: |
| 465 | session = context.get_session(session_id) |
| 466 | if session and session.get('id', False): |
| 467 | hash_algorithm:str = 'sha256' |
| 468 | checksum_path = os.path.join(session['process_dir'], 'checksum') |
| 469 | hash_func = hashlib.new(hash_algorithm) |
| 470 | with open(session['ebook'], 'rb') as f: |
| 471 | while chunk := f.read(8192): |
| 472 | hash_func.update(chunk) |
| 473 | new_checksum = hash_func.hexdigest() |
| 474 | if not os.path.exists(checksum_path): |
| 475 | with open(checksum_path, 'w', encoding='utf-8') as f: |
| 476 | f.write(new_checksum) |
| 477 | return False, None |
| 478 | else: |
| 479 | with open(checksum_path, 'r', encoding='utf-8') as f: |
| 480 | saved_checksum = f.read().strip() |
| 481 | if saved_checksum == new_checksum: |
| 482 | return True, None |
| 483 | else: |
| 484 | with open(checksum_path, 'w', encoding='utf-8') as f: |
| 485 | f.write(new_checksum) |
| 486 | return False, None |
| 487 | error = f'compare_checksums() error: session does not exist' |
| 488 | return False, error |
| 489 | except Exception as e: |
| 490 | error = f'compare_checksums() error: {e}' |
| 491 | return False, error |
| 492 | |
| 493 | def compare_dict_keys(d1, d2): |
| 494 | if not isinstance(d1, Mapping) or not isinstance(d2, Mapping): |
no test coverage detected