[models/utils] Catch and log exception when opening file fails

This commit is contained in:
derrod 2020-05-23 16:55:18 +02:00
parent e9a959e3a7
commit 1ac1875a86
2 changed files with 14 additions and 9 deletions

View file

@ -137,3 +137,4 @@ class VerifyResult(Enum):
HASH_MATCH = 0
HASH_MISMATCH = 1
FILE_MISSING = 2
OTHER_ERROR = 3

View file

@ -50,16 +50,20 @@ def validate_files(base_path: str, filelist: List[tuple], hash_type='sha1') -> I
yield VerifyResult.FILE_MISSING, file_path, ''
continue
with open(full_path, 'rb') as f:
real_file_hash = hashlib.new(hash_type)
while chunk := f.read(8192):
real_file_hash.update(chunk)
try:
with open(full_path, 'rb') as f:
real_file_hash = hashlib.new(hash_type)
while chunk := f.read(1024*1024):
real_file_hash.update(chunk)
result_hash = real_file_hash.hexdigest()
if file_hash != result_hash:
yield VerifyResult.HASH_MISMATCH, file_path, result_hash
else:
yield VerifyResult.HASH_MATCH, file_path, result_hash
result_hash = real_file_hash.hexdigest()
if file_hash != result_hash:
yield VerifyResult.HASH_MISMATCH, file_path, result_hash
else:
yield VerifyResult.HASH_MATCH, file_path, result_hash
except Exception as e:
logger.fatal(f'Could not verify "{file_path}"; opening failed with: {e!r}')
yield VerifyResult.OTHER_ERROR, file_path, ''
def clean_filename(filename):