fix: trust WebDAV folder year over 1970 epoch dates
Files under Faturas/2025/ or /2026/ are eligible even when Nextcloud reports creationdate 1970. Add --requeue-folder-years to reset wrongly skipped records.
This commit is contained in:
1 parent
a0f90bd8ef
commit
3d81005588
3 files changed
+80
-4
No files matched your search
@@ -139,14 +139,26 @@ def parse_webdav_iso_date(value: str) -> datetime | None:
|
|||||||
def webdav_file_reference_date(file: WebdavFile) -> datetime | None:
|
def webdav_file_reference_date(file: WebdavFile) -> datetime | None:
|
||||||
if file.created:
|
if file.created:
|
||||||
created = parse_webdav_iso_date(file.created)
|
created = parse_webdav_iso_date(file.created)
|
||||||
if created is not None:
|
if created is not None and not _is_untrusted_webdav_date(created):
|
||||||
return created
|
return created
|
||||||
created = parse_webdav_http_date(file.created)
|
created = parse_webdav_http_date(file.created)
|
||||||
if created is not None:
|
if created is not None and not _is_untrusted_webdav_date(created):
|
||||||
return created
|
return created
|
||||||
return parse_webdav_http_date(file.modified)
|
return parse_webdav_http_date(file.modified)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_untrusted_webdav_date(value: datetime) -> bool:
|
||||||
|
"""Nextcloud often reports missing creation as Unix epoch (1970)."""
|
||||||
|
return value.year <= 1971
|
||||||
|
|
||||||
|
|
||||||
|
def webdav_folder_year_meets_min_year(relative_path: str, min_year: int) -> bool:
|
||||||
|
folder_year = webdav_year_folder(relative_path)
|
||||||
|
if folder_year is None:
|
||||||
|
return False
|
||||||
|
return int(folder_year) >= min_year
|
||||||
|
|
||||||
|
|
||||||
def webdav_min_year_cutoff(min_year: int) -> datetime:
|
def webdav_min_year_cutoff(min_year: int) -> datetime:
|
||||||
return datetime(min_year, 1, 1, tzinfo=timezone.utc)
|
return datetime(min_year, 1, 1, tzinfo=timezone.utc)
|
||||||
|
|
||||||
@@ -154,6 +166,10 @@ def webdav_min_year_cutoff(min_year: int) -> datetime:
|
|||||||
def webdav_file_matches_min_year(file: WebdavFile, min_year: int | None) -> bool:
|
def webdav_file_matches_min_year(file: WebdavFile, min_year: int | None) -> bool:
|
||||||
if min_year is None:
|
if min_year is None:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
if webdav_folder_year_meets_min_year(file.relative_path, min_year):
|
||||||
|
return True
|
||||||
|
|
||||||
reference_date = webdav_file_reference_date(file)
|
reference_date = webdav_file_reference_date(file)
|
||||||
if reference_date is None:
|
if reference_date is None:
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -100,6 +100,31 @@ def test_webdav_file_matches_min_year_by_creation_date():
|
|||||||
assert webdav_file_matches_min_year(recent, 2025)
|
assert webdav_file_matches_min_year(recent, 2025)
|
||||||
|
|
||||||
|
|
||||||
|
def test_webdav_file_matches_min_year_by_folder_when_epoch_date():
|
||||||
|
epoch_in_2025_folder = WebdavFile(
|
||||||
|
relative_path="Faturas/2025/01_Janeiro/invoice.pdf",
|
||||||
|
name="invoice.pdf",
|
||||||
|
href="/invoice.pdf",
|
||||||
|
etag="1",
|
||||||
|
modified="Mon, 06 Jan 2025 13:38:04 GMT",
|
||||||
|
created="1970-01-01T00:00:00+00:00",
|
||||||
|
size=50000,
|
||||||
|
)
|
||||||
|
assert webdav_file_reference_date(epoch_in_2025_folder).year == 2025
|
||||||
|
assert webdav_file_matches_min_year(epoch_in_2025_folder, 2025)
|
||||||
|
|
||||||
|
epoch_in_2024_folder = WebdavFile(
|
||||||
|
relative_path="Faturas/2024/invoice.pdf",
|
||||||
|
name="invoice.pdf",
|
||||||
|
href="/invoice.pdf",
|
||||||
|
etag="1",
|
||||||
|
modified="Mon, 06 Jan 2025 13:38:04 GMT",
|
||||||
|
created="1970-01-01T00:00:00+00:00",
|
||||||
|
size=50000,
|
||||||
|
)
|
||||||
|
assert not webdav_file_matches_min_year(epoch_in_2024_folder, 2025)
|
||||||
|
|
||||||
|
|
||||||
def test_webdav_min_file_size():
|
def test_webdav_min_file_size():
|
||||||
assert MIN_WEBDAV_FILE_BYTES == 4096
|
assert MIN_WEBDAV_FILE_BYTES == 4096
|
||||||
small = WebdavFile(
|
small = WebdavFile(
|
||||||
|
|||||||
@@ -16,8 +16,10 @@ from forwarder.webdav.client import (
|
|||||||
get_webdav_message_key,
|
get_webdav_message_key,
|
||||||
list_files,
|
list_files,
|
||||||
normalize_webdav_folder_path,
|
normalize_webdav_folder_path,
|
||||||
|
parse_webdav_message_key,
|
||||||
webdav_file_matches_min_year,
|
webdav_file_matches_min_year,
|
||||||
webdav_file_reference_date,
|
webdav_file_reference_date,
|
||||||
|
webdav_folder_year_meets_min_year,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -28,13 +30,18 @@ def main() -> int:
|
|||||||
"--min-year",
|
"--min-year",
|
||||||
type=int,
|
type=int,
|
||||||
default=2025,
|
default=2025,
|
||||||
help="Minimum file date year (creation or last-modified) — default 2025",
|
help="Minimum year — folder path (e.g. /2025/) or file date when no folder year",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--mark-skipped",
|
"--mark-skipped",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Insert skipped records for files outside allowed years",
|
help="Insert skipped records for files outside allowed years",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--requeue-folder-years",
|
||||||
|
action="store_true",
|
||||||
|
help="Delete cutoff-skip records for files in min-year+ folders so sync re-sends them",
|
||||||
|
)
|
||||||
parser.add_argument("--dry-run", action="store_true")
|
parser.add_argument("--dry-run", action="store_true")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -58,7 +65,8 @@ def main() -> int:
|
|||||||
skip = len(files) - keep
|
skip = len(files) - keep
|
||||||
|
|
||||||
print(f"Account {account.id} ({account.name}): {len(files)} file(s) listed")
|
print(f"Account {account.id} ({account.name}): {len(files)} file(s) listed")
|
||||||
print(f"Min year (by file date): {args.min_year} → keep {keep}, skip {skip}")
|
print(f"Min year: {args.min_year} → keep {keep}, skip {skip}")
|
||||||
|
print("(Folder year in path takes priority over WebDAV dates, including 1970 epoch.)")
|
||||||
print("By file date year:")
|
print("By file date year:")
|
||||||
for year in sorted(by_year):
|
for year in sorted(by_year):
|
||||||
flag = "KEEP" if int(year) >= args.min_year else "SKIP"
|
flag = "KEEP" if int(year) >= args.min_year else "SKIP"
|
||||||
@@ -66,6 +74,33 @@ def main() -> int:
|
|||||||
if undated:
|
if undated:
|
||||||
print(f" (no date from WebDAV): {undated}")
|
print(f" (no date from WebDAV): {undated}")
|
||||||
|
|
||||||
|
if args.requeue_folder_years:
|
||||||
|
from forwarder.db.models import Message
|
||||||
|
|
||||||
|
cutoff_msg = "skipped: file date before cutoff"
|
||||||
|
candidates = (
|
||||||
|
db.query(Message)
|
||||||
|
.filter(
|
||||||
|
Message.account_id == account.id,
|
||||||
|
Message.error_message == cutoff_msg,
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
to_delete = []
|
||||||
|
for message in candidates:
|
||||||
|
relative_path = parse_webdav_message_key(message.message_key)
|
||||||
|
if relative_path and webdav_folder_year_meets_min_year(
|
||||||
|
relative_path, args.min_year
|
||||||
|
):
|
||||||
|
to_delete.append(message)
|
||||||
|
if args.dry_run:
|
||||||
|
print(f"Would requeue {len(to_delete)} file(s) from {args.min_year}+ folders")
|
||||||
|
else:
|
||||||
|
for message in to_delete:
|
||||||
|
db.delete(message)
|
||||||
|
db.commit()
|
||||||
|
print(f"Requeued {len(to_delete)} file(s) — next sync will forward them")
|
||||||
|
|
||||||
if not args.mark_skipped:
|
if not args.mark_skipped:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user