fix: use WebDAV getlastmodified instead of Nextcloud epoch creationdate
Nextcloud returns DAV:creationdate as 1970 for bulk-imported files; the real upload date is getlastmodified. Also pick the HTTP 200 propstat block.
This commit is contained in:
1 parent
3d81005588
commit
7e5e4584fc
2 files changed
+116
-17
No files matched your search
@@ -17,15 +17,20 @@ logger = logging.getLogger(__name__)
|
||||
# Reject HTML stubs / placeholders before download when PROPFIND reports size.
|
||||
MIN_WEBDAV_FILE_BYTES = 4 * 1024
|
||||
|
||||
OC_NS = "{http://owncloud.org/ns}"
|
||||
NC_NS = "{http://nextcloud.org/ns}"
|
||||
DAV_NS = "{DAV:}"
|
||||
|
||||
PROPFIND_BODY = b"""<?xml version="1.0" encoding="utf-8"?>
|
||||
<D:propfind xmlns:D="DAV:">
|
||||
<D:propfind xmlns:D="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
|
||||
<D:prop>
|
||||
<D:displayname/>
|
||||
<D:getetag/>
|
||||
<D:getcontentlength/>
|
||||
<D:getlastmodified/>
|
||||
<D:creationdate/>
|
||||
<oc:creation-time/>
|
||||
<nc:creation-time/>
|
||||
<D:resourcetype/>
|
||||
</D:prop>
|
||||
</D:propfind>
|
||||
@@ -49,6 +54,8 @@ class WebdavFile:
|
||||
etag: str
|
||||
modified: str
|
||||
created: str
|
||||
oc_creation_time: str
|
||||
nc_creation_time: str
|
||||
size: int
|
||||
|
||||
|
||||
@@ -137,14 +144,27 @@ def parse_webdav_iso_date(value: str) -> datetime | None:
|
||||
|
||||
|
||||
def webdav_file_reference_date(file: WebdavFile) -> datetime | None:
|
||||
if file.created:
|
||||
created = parse_webdav_iso_date(file.created)
|
||||
if created is not None and not _is_untrusted_webdav_date(created):
|
||||
return created
|
||||
created = parse_webdav_http_date(file.created)
|
||||
if created is not None and not _is_untrusted_webdav_date(created):
|
||||
return created
|
||||
return parse_webdav_http_date(file.modified)
|
||||
"""Reference date for year filtering.
|
||||
|
||||
Nextcloud often returns DAV:creationdate as Unix epoch (1970) for bulk-imported
|
||||
files. Prefer getlastmodified (upload time on server), then oc/nc creation-time,
|
||||
then creationdate when trustworthy.
|
||||
"""
|
||||
modified = parse_webdav_http_date(file.modified)
|
||||
if modified is not None:
|
||||
return modified
|
||||
|
||||
for raw in (file.oc_creation_time, file.nc_creation_time, file.created):
|
||||
if not raw:
|
||||
continue
|
||||
parsed = parse_webdav_iso_date(raw)
|
||||
if parsed is not None and not _is_untrusted_webdav_date(parsed):
|
||||
return parsed
|
||||
parsed = parse_webdav_http_date(raw)
|
||||
if parsed is not None and not _is_untrusted_webdav_date(parsed):
|
||||
return parsed
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _is_untrusted_webdav_date(value: datetime) -> bool:
|
||||
@@ -203,13 +223,30 @@ def _client(config: WebdavConfig, *, timeout: float = 60.0) -> httpx.Client:
|
||||
)
|
||||
|
||||
|
||||
def _element_text(parent: element_tree.Element, tag: str) -> str:
|
||||
node = parent.find(f"{DAV_NS}{tag}")
|
||||
def _element_text(parent: element_tree.Element, tag: str, ns: str = DAV_NS) -> str:
|
||||
node = parent.find(f"{ns}{tag}")
|
||||
if node is None or node.text is None:
|
||||
return ""
|
||||
return node.text.strip()
|
||||
|
||||
|
||||
def _propstat_ok(propstat: element_tree.Element) -> bool:
|
||||
status = _element_text(propstat, "status")
|
||||
if not status:
|
||||
return True
|
||||
return " 200 " in status or status.endswith(" 200 OK")
|
||||
|
||||
|
||||
def _select_propstat(response_elem: element_tree.Element) -> element_tree.Element | None:
|
||||
propstats = response_elem.findall(f"{DAV_NS}propstat")
|
||||
if not propstats:
|
||||
return None
|
||||
for propstat in propstats:
|
||||
if _propstat_ok(propstat):
|
||||
return propstat
|
||||
return propstats[0]
|
||||
|
||||
|
||||
def _is_collection(response_elem: element_tree.Element) -> bool:
|
||||
resource_type = response_elem.find(f"{DAV_NS}propstat/{DAV_NS}prop/{DAV_NS}resourcetype")
|
||||
if resource_type is None:
|
||||
@@ -253,7 +290,7 @@ def _parse_propfind_response(root_url: str, content: bytes) -> list[WebdavFile]:
|
||||
if name.startswith("."):
|
||||
continue
|
||||
|
||||
propstat = response_elem.find(f"{DAV_NS}propstat")
|
||||
propstat = _select_propstat(response_elem)
|
||||
if propstat is None:
|
||||
continue
|
||||
prop = propstat.find(f"{DAV_NS}prop")
|
||||
@@ -263,6 +300,8 @@ def _parse_propfind_response(root_url: str, content: bytes) -> list[WebdavFile]:
|
||||
etag = _element_text(prop, "getetag").strip('"')
|
||||
modified = _element_text(prop, "getlastmodified")
|
||||
created = _element_text(prop, "creationdate")
|
||||
oc_creation_time = _element_text(prop, "creation-time", OC_NS)
|
||||
nc_creation_time = _element_text(prop, "creation-time", NC_NS)
|
||||
size_text = _element_text(prop, "getcontentlength")
|
||||
try:
|
||||
size = int(size_text) if size_text else 0
|
||||
@@ -277,6 +316,8 @@ def _parse_propfind_response(root_url: str, content: bytes) -> list[WebdavFile]:
|
||||
etag=etag,
|
||||
modified=modified,
|
||||
created=created,
|
||||
oc_creation_time=oc_creation_time,
|
||||
nc_creation_time=nc_creation_time,
|
||||
size=size,
|
||||
)
|
||||
)
|
||||
|
||||
+63
-5
@@ -87,30 +87,49 @@ def _sample_file(*, modified: str = "", created: str = "", path: str = "a.pdf")
|
||||
etag="1",
|
||||
modified=modified,
|
||||
created=created,
|
||||
oc_creation_time="",
|
||||
nc_creation_time="",
|
||||
size=100,
|
||||
)
|
||||
|
||||
|
||||
def test_webdav_file_matches_min_year_by_creation_date():
|
||||
old = _sample_file(created="2024-06-01T10:00:00Z", modified="Mon, 01 Jan 2026 10:00:00 GMT")
|
||||
assert webdav_file_reference_date(old).year == 2024
|
||||
assert not webdav_file_matches_min_year(old, 2025)
|
||||
assert webdav_file_reference_date(old).year == 2026
|
||||
assert webdav_file_matches_min_year(old, 2025)
|
||||
|
||||
recent = _sample_file(modified="Mon, 15 Jun 2025 12:00:00 GMT")
|
||||
assert webdav_file_matches_min_year(recent, 2025)
|
||||
|
||||
|
||||
def test_webdav_file_reference_date_prefers_modified_over_epoch_creation():
|
||||
nextcloud_import = 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",
|
||||
oc_creation_time="",
|
||||
nc_creation_time="",
|
||||
size=50000,
|
||||
)
|
||||
assert webdav_file_reference_date(nextcloud_import).year == 2025
|
||||
assert webdav_file_matches_min_year(nextcloud_import, 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",
|
||||
modified="",
|
||||
created="1970-01-01T00:00:00+00:00",
|
||||
oc_creation_time="",
|
||||
nc_creation_time="",
|
||||
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(
|
||||
@@ -118,8 +137,10 @@ def test_webdav_file_matches_min_year_by_folder_when_epoch_date():
|
||||
name="invoice.pdf",
|
||||
href="/invoice.pdf",
|
||||
etag="1",
|
||||
modified="Mon, 06 Jan 2025 13:38:04 GMT",
|
||||
modified="",
|
||||
created="1970-01-01T00:00:00+00:00",
|
||||
oc_creation_time="",
|
||||
nc_creation_time="",
|
||||
size=50000,
|
||||
)
|
||||
assert not webdav_file_matches_min_year(epoch_in_2024_folder, 2025)
|
||||
@@ -134,6 +155,8 @@ def test_webdav_min_file_size():
|
||||
etag="1",
|
||||
modified="",
|
||||
created="",
|
||||
oc_creation_time="",
|
||||
nc_creation_time="",
|
||||
size=1997,
|
||||
)
|
||||
assert webdav_file_below_min_size(small)
|
||||
@@ -144,6 +167,8 @@ def test_webdav_min_file_size():
|
||||
etag="1",
|
||||
modified="",
|
||||
created="",
|
||||
oc_creation_time="",
|
||||
nc_creation_time="",
|
||||
size=0,
|
||||
)
|
||||
assert not webdav_file_below_min_size(unknown)
|
||||
@@ -154,6 +179,8 @@ def test_webdav_min_file_size():
|
||||
etag="1",
|
||||
modified="",
|
||||
created="",
|
||||
oc_creation_time="",
|
||||
nc_creation_time="",
|
||||
size=5000,
|
||||
)
|
||||
assert not webdav_file_below_min_size(large)
|
||||
@@ -161,6 +188,37 @@ def test_webdav_min_file_size():
|
||||
assert not webdav_bytes_below_min_size(b"x" * MIN_WEBDAV_FILE_BYTES)
|
||||
|
||||
|
||||
PROPFIND_MULTI_PROPSTAT = b"""<?xml version="1.0" encoding="utf-8"?>
|
||||
<D:multistatus xmlns:D="DAV:">
|
||||
<D:response>
|
||||
<D:href>/remote.php/dav/files/user/Faturas/doc.pdf</D:href>
|
||||
<D:propstat>
|
||||
<D:prop><D:creationdate/></D:prop>
|
||||
<D:status>HTTP/1.1 404 Not Found</D:status>
|
||||
</D:propstat>
|
||||
<D:propstat>
|
||||
<D:prop>
|
||||
<D:getlastmodified>Mon, 15 Jun 2025 12:00:00 GMT</D:getlastmodified>
|
||||
<D:creationdate>1970-01-01T00:00:00+00:00</D:creationdate>
|
||||
<D:getcontentlength>5000</D:getcontentlength>
|
||||
<D:getetag>"x"</D:getetag>
|
||||
<D:resourcetype/>
|
||||
</D:prop>
|
||||
<D:status>HTTP/1.1 200 OK</D:status>
|
||||
</D:propstat>
|
||||
</D:response>
|
||||
</D:multistatus>
|
||||
"""
|
||||
|
||||
|
||||
def test_parse_propfind_uses_ok_propstat():
|
||||
root = "https://cloud.example.com/remote.php/dav/files/user/Faturas/"
|
||||
files = _parse_propfind_response(root, PROPFIND_MULTI_PROPSTAT)
|
||||
assert len(files) == 1
|
||||
assert files[0].modified == "Mon, 15 Jun 2025 12:00:00 GMT"
|
||||
assert webdav_file_reference_date(files[0]).year == 2025
|
||||
|
||||
|
||||
def test_parse_propfind_response():
|
||||
root = "https://cloud.example.com/remote.php/dav/files/user/Faturas/"
|
||||
files = _parse_propfind_response(root, PROPFIND_SAMPLE)
|
||||
|
||||
Reference in new issue
Block a user