[boardgames] Hash lrtl files and only import if changed
All checks were successful
build & deploy / test (push) Successful in 2m3s
build & deploy / build-and-deploy (push) Successful in 33s

This commit is contained in:
2026-05-22 12:25:42 -04:00
parent 26176ccd73
commit c5f1ee2d64
3 changed files with 48 additions and 1 deletions

View File

@ -226,6 +226,32 @@ def scan_webdav_for_retroarch(webdav_client, user_id):
if not downloaded:
return 0
# Compute content hash of all downloaded files
import hashlib
hasher = hashlib.md5()
for basename in downloaded:
filepath = os.path.join(download_dir, basename)
hasher.update(basename.encode())
with open(filepath, "rb") as f:
hasher.update(f.read())
content_hash = hasher.hexdigest()
# Skip if the last completed import already has this hash
last_import = (
RetroarchImport.objects.filter(
user_id=user_id, processed_finished__isnull=False
)
.order_by("-processed_finished")
.first()
)
if last_import and last_import.files_hash == content_hash:
logger.info(
"Retroarch lrtl files unchanged for user, skipping",
extra={"user_id": user_id},
)
return 0
zip_path = os.path.join(
download_dir, f"retroarch-batch-{datetime.now().strftime('%Y%m%d%H%M%S')}.zip"
)
@ -236,6 +262,7 @@ def scan_webdav_for_retroarch(webdav_client, user_id):
imp = RetroarchImport.objects.create(
user_id=user_id,
original_filename=f"batch-{len(downloaded)}-files",
files_hash=content_hash,
)
with open(zip_path, "rb") as f:
imp.lrtl_file.save(
@ -243,9 +270,10 @@ def scan_webdav_for_retroarch(webdav_client, user_id):
)
process_retroarch_import.delay(imp.id)
logger.info(
"Queued retroarch import %s with %d file(s)",
"Queued retroarch import %s with %d file(s) (hash=%s)",
imp.uuid,
len(downloaded),
content_hash[:12],
)
return 1
finally: