fix(in-transit-transfer): 证据文件改由当前站点下载以避免浏览器拦截

This commit is contained in:
hugo committed 2026-10-26 20:28:03 +08:00
1 parent dfd48e08e7
commit 440afe097f
7 files changed
+186 -21

No files matched your search

@@ -10,7 +10,8 @@
unconfigured 后再各写最多一个 batch;
证据下载(POST /sources/inbound-files)按同一函数打包各来源最新可用
入站/出站文件;当天清单(GET /sources/<code>/evidence-files)复用可用
条件返回当天全部文件,并分别签发预览地址与带保存名的下载地址;
条件返回当天全部文件;单文件下载(GET .../evidence-files/<file_id>)
由当前站点回传字节,不把 HTTP MinIO 地址交给 HTTPS 页面;
匹配创建(POST /matchings)与进度恢复(GET /matchings/{id});执行为 D8
异步契约(POST /items/execute → 202 queued + run_no,单在提交时组建);
result_unknown 单人工重新触发(POST /runs/{run_no}/documents/{doc_no}/
@@ -369,10 +370,9 @@ def list_source_evidence_files_api(source_code: str):
查询参数 record_kind 缺省 inbound,只接受 inbound 或 outbound。
可用条件与证据压缩包相同,但返回当天全部可用文件,不只最新一份。
每项含 file_id、original_filename、created_at、sha256、file_size、
presigned_url、download_url。不解析 Excel,不含行数。 预览与下载各签一次
presigned_get_url(storage_key, expires_seconds=300);主机取 MINIO_PUBLIC_BASE_URL,
容器内读写仍用 MINIO_ENDPOINT。下载地址额外带
download_name(库存源_东八区年月日_时分.扩展名),避免浏览器用对象键哈希当保存名。
presigned_url、download_url。不解析 Excel,不含行数。预览仍打开
presigned_url。单文件下载不使用 download_url,改走
GET evidence-files/<file_id>,由当前站点回传。
签发时写 TRANSFER_INBOUND_FILES_DOWNLOADED,aggregate_id 为 source_code。
"""
from app.services.file_assets.storage import file_asset_storage
@@ -402,6 +402,47 @@ def list_source_evidence_files_api(source_code: str):
return _ok(payload)
@in_transit_transfer_bp.route("/sources/<source_code>/evidence-files/<int:file_id>", methods=["GET"])
@require_transfer_permission
def download_source_evidence_file_api(source_code: str, file_id: int):
"""把一份仍可用的证据文件经当前站点返回给浏览器。
查询参数 record_kind 缺省 inbound,只接受 inbound 或 outbound。
只接受该来源、该记录类型下仍在可用窗口内的归档。保存名用
evidence_download_filename。页面是 HTTPS,MinIO 公网地址是 HTTP,
直接打开预签名链接会被浏览器当成不安全下载拦掉。
Args:
source_code: 来源实例编码。
file_id: 归档文件主键。
Returns:
附件响应。找不到或已不可用时 404,错误码 SHIPPING.INBOUND_FILE_NOT_FOUND。
"""
import io
import mimetypes
from app.services.file_assets.storage import file_asset_storage
kind = str(request.args.get("record_kind") or "inbound").strip()
if kind not in {"inbound", "outbound"}:
raise ValidationError("record_kind must be inbound or outbound")
code = str(source_code or "").strip()
report = next((item for item in _usable_archive_files(code, kind) if item.id == file_id), None)
if report is None:
raise NotFoundError("evidence file is not available", code="SHIPPING.INBOUND_FILE_NOT_FOUND")
data = file_asset_storage.get_bytes(report.storage_key)
download_name = evidence_download_filename(code, report.created_at, report.original_filename)
_record_evidence_download_audit(aggregate_id=code)
mime = mimetypes.guess_type(download_name)[0] or "application/octet-stream"
return send_file(
io.BytesIO(data),
mimetype=mime,
as_attachment=True,
download_name=download_name,
)
@in_transit_transfer_bp.route("/runs/<run_no>", methods=["GET"])
@require_transfer_permission
def get_run_api(run_no: str):
@@ -2255,3 +2255,51 @@ def test_evidence_files_list_returns_today_size_and_presigned_url(
event_type="TRANSFER_INBOUND_FILES_DOWNLOADED",
).one()
assert audit.aggregate_id == EVIDENCE_SOURCE
def test_evidence_file_download_streams_through_the_site(
app, client, admin_headers, monkeypatch,
):
"""单文件下载由当前站点回传,保存名不用对象键。"""
now = datetime.utcnow().replace(microsecond=0)
source = _ensure_evidence_source()
report = _seed_evidence_file(
source, kind="inbound", filename="Inbound.xlsx",
storage_key="evidence/one", sha="61" * 32, file_size=12,
created_at=now,
)
_patch_archive_bytes(monkeypatch, {"evidence/one": b"PK-xlsx"})
response = client.get(
f"{API_PREFIX}/sources/{EVIDENCE_SOURCE}/evidence-files/{report.id}?record_kind=inbound",
headers=admin_headers,
)
assert response.status_code == 200
# [断言意图] 响应体是对象内容,不是跳到 MinIO 的地址。
assert response.data == b"PK-xlsx"
from app.api.in_transit_transfer import evidence_download_filename
download_name = evidence_download_filename(EVIDENCE_SOURCE, now, "Inbound.xlsx")
# [断言意图] 保存名沿用库存源加东八区时间,浏览器不会落到哈希对象键。
assert download_name in response.headers.get("Content-Disposition", "")
def test_evidence_file_download_rejects_unavailable_file(app, client, admin_headers):
"""已清理或不在可用窗口的归档不能经站点下载。"""
now = datetime.utcnow().replace(microsecond=0)
source = _ensure_evidence_source()
report = _seed_evidence_file(
source, kind="inbound", filename="Deleted.xlsx",
storage_key="evidence/gone", sha="62" * 32, file_size=12,
created_at=now, deleted=True,
)
response = client.get(
f"{API_PREFIX}/sources/{EVIDENCE_SOURCE}/evidence-files/{report.id}?record_kind=inbound",
headers=admin_headers,
)
# [断言意图] 清单里不会出现的文件,单文件下载同样拒绝。
assert response.status_code == 404
assert response.get_json()["error"]["code"] == "SHIPPING.INBOUND_FILE_NOT_FOUND"