diff --git a/domain/bulk_document_download/package_plan.py b/domain/bulk_document_download/package_plan.py index bcd122aad..535928596 100644 --- a/domain/bulk_document_download/package_plan.py +++ b/domain/bulk_document_download/package_plan.py @@ -8,6 +8,7 @@ orchestrator does the S3/DB work; this module only lays out the archive. from __future__ import annotations +import posixpath from dataclasses import dataclass from datetime import datetime from typing import Optional, Sequence @@ -57,6 +58,14 @@ class DownloadPackagePlan: skipped: tuple[SkippedDocument, ...] +def _extension_of(s3_key: str) -> str: + """The file extension of an S3 key (``.pdf``, ``.zip``, …), empty when the + key's basename has none. Used to keep the packaged file openable while + naming it by Document Type.""" + _root, ext = posixpath.splitext(posixpath.basename(s3_key)) + return ext + + def plan_download_package( documents: Sequence[ResolvedDocument], ) -> DownloadPackagePlan: @@ -83,7 +92,7 @@ def plan_download_package( entries: list[PackageEntry] = [ PackageEntry( zip_path=f"{document.address} ({document.landlord_property_id})/" - f"{document.document_type}", + f"{document.document_type}{_extension_of(document.s3_key)}", s3_bucket=document.s3_bucket, s3_key=document.s3_key, )