[books] Add etag update option to webdav imports
This commit is contained in:
@ -20,7 +20,7 @@ DEFAULT_EBIRD_PATH = "var/ebird/"
|
|||||||
DEFAULT_SCALE_PATH = "var/scale/"
|
DEFAULT_SCALE_PATH = "var/scale/"
|
||||||
|
|
||||||
|
|
||||||
def import_from_webdav_for_all_users(restart=False, update_retroarch_hash=False):
|
def import_from_webdav_for_all_users(restart=False, update_retroarch_hash=False, update_koreader_etag=False):
|
||||||
"""Iterate all WebDAV-enabled users, scanning each media-type directory."""
|
"""Iterate all WebDAV-enabled users, scanning each media-type directory."""
|
||||||
webdav_enabled_user_ids = UserProfile.objects.filter(
|
webdav_enabled_user_ids = UserProfile.objects.filter(
|
||||||
webdav_url__isnull=False,
|
webdav_url__isnull=False,
|
||||||
@ -42,7 +42,9 @@ def import_from_webdav_for_all_users(restart=False, update_retroarch_hash=False)
|
|||||||
logger.info("Scanning WebDAV for user %s", user_id)
|
logger.info("Scanning WebDAV for user %s", user_id)
|
||||||
|
|
||||||
logger.info("Scanning WebDAV koreader for user %s", user_id)
|
logger.info("Scanning WebDAV koreader for user %s", user_id)
|
||||||
ko_count += scan_webdav_for_koreader(client, user_id, restart)
|
ko_count += scan_webdav_for_koreader(
|
||||||
|
client, user_id, restart, update_etag_only=update_koreader_etag
|
||||||
|
)
|
||||||
logger.info("Scanning WebDAV gpx for user %s", user_id)
|
logger.info("Scanning WebDAV gpx for user %s", user_id)
|
||||||
gpx_count += scan_webdav_for_gpx(client, user_id)
|
gpx_count += scan_webdav_for_gpx(client, user_id)
|
||||||
logger.info("Scanning WebDAV retroarch for user %s", user_id)
|
logger.info("Scanning WebDAV retroarch for user %s", user_id)
|
||||||
@ -76,21 +78,26 @@ def import_from_webdav_for_all_users(restart=False, update_retroarch_hash=False)
|
|||||||
return ko_count, gpx_count, retro_count, bgstats_count, ebird_count, scale_count
|
return ko_count, gpx_count, retro_count, bgstats_count, ebird_count, scale_count
|
||||||
|
|
||||||
|
|
||||||
def scan_webdav_for_koreader(webdav_client, user_id, restart=False):
|
def scan_webdav_for_koreader(webdav_client, user_id, restart=False, update_etag_only=False):
|
||||||
"""Check for koreader statistics.sqlite3 and queue import if changed.
|
"""Check for koreader statistics.sqlite3 and queue import if changed.
|
||||||
|
|
||||||
Uses the remote file's ETag (from a single PROPFIND) to detect changes
|
Uses the remote file's ETag (from a single PROPFIND) to detect changes
|
||||||
without downloading the file, making the common no-change case fast.
|
without downloading the file, making the common no-change case fast.
|
||||||
|
|
||||||
|
When *update_etag_only* is True, stores the remote ETag on the last
|
||||||
|
completed import without re-downloading — useful when migrating to the
|
||||||
|
ETag-based check to avoid a one-time re-import.
|
||||||
"""
|
"""
|
||||||
# Don't queue if one is still being processed
|
if not update_etag_only:
|
||||||
if KoReaderImport.objects.filter(
|
# Don't queue if one is still being processed
|
||||||
user_id=user_id, processed_finished__isnull=True
|
if KoReaderImport.objects.filter(
|
||||||
).exists():
|
user_id=user_id, processed_finished__isnull=True
|
||||||
logger.info(
|
).exists():
|
||||||
"KoReader import already pending for user",
|
logger.info(
|
||||||
extra={"user_id": user_id},
|
"KoReader import already pending for user",
|
||||||
)
|
extra={"user_id": user_id},
|
||||||
return 0
|
)
|
||||||
|
return 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
files = webdav_client.list(DEFAULT_KOREADER_PATH, get_info=True)
|
files = webdav_client.list(DEFAULT_KOREADER_PATH, get_info=True)
|
||||||
@ -122,6 +129,17 @@ def scan_webdav_for_koreader(webdav_client, user_id, restart=False):
|
|||||||
.last()
|
.last()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if update_etag_only:
|
||||||
|
if last_import and remote_etag:
|
||||||
|
last_import.webdav_etag = remote_etag
|
||||||
|
last_import.save(update_fields=["webdav_etag"])
|
||||||
|
logger.info(
|
||||||
|
"Updated KoReader webdav_etag (%s) without re-import",
|
||||||
|
remote_etag[:16],
|
||||||
|
extra={"user_id": user_id, "last_import_id": last_import.id},
|
||||||
|
)
|
||||||
|
return 0
|
||||||
|
|
||||||
if last_import and last_import.webdav_etag and remote_etag:
|
if last_import and last_import.webdav_etag and remote_etag:
|
||||||
if last_import.webdav_etag == remote_etag:
|
if last_import.webdav_etag == remote_etag:
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|||||||
@ -15,13 +15,21 @@ class Command(BaseCommand):
|
|||||||
help="Update retroarch files_hash to new ETag-based scheme "
|
help="Update retroarch files_hash to new ETag-based scheme "
|
||||||
"without re-importing (migration helper)",
|
"without re-importing (migration helper)",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--update-koreader-etag",
|
||||||
|
action="store_true",
|
||||||
|
help="Store current WebDAV ETag on last KoReader import "
|
||||||
|
"without re-importing (migration helper)",
|
||||||
|
)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
restart = False
|
restart = False
|
||||||
if options["restart"]:
|
if options["restart"]:
|
||||||
restart = True
|
restart = True
|
||||||
update_hash = options.get("update_retroarch_hash", False)
|
update_hash = options.get("update_retroarch_hash", False)
|
||||||
|
update_etag = options.get("update_koreader_etag", False)
|
||||||
webdav.import_from_webdav_for_all_users(
|
webdav.import_from_webdav_for_all_users(
|
||||||
restart=restart,
|
restart=restart,
|
||||||
update_retroarch_hash=update_hash,
|
update_retroarch_hash=update_hash,
|
||||||
|
update_koreader_etag=update_etag,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user