feat: batch download

This commit is contained in:
tao.chen
2026-08-27 19:35:30 +08:00
parent 7225b72906
commit a897a08534
10 changed files with 443 additions and 77 deletions
+70
View File
@@ -270,6 +270,76 @@ else
ko "gin mode" "not in release mode"
fi
# ──────────────────────────────────────────────────────────────
section "N. 批量下载 (POST /download-zip)"
# ──────────────────────────────────────────────────────────────
echo "batch-1" > "$TMPDIR/batch1.txt"
echo "batch-2" > "$TMPDIR/batch2.txt"
upload "$TMPDIR/batch1.txt" "batch1.txt" > /dev/null
upload "$TMPDIR/batch2.txt" "batch2.txt" > /dev/null
zipfile="$TMPDIR/batch.zip"
hdrs=$(curl -s -D - -o "$zipfile" -X POST -H "Content-Type: application/json" \
-d '{"files":["batch1.txt","batch2.txt"]}' "$BASE/download-zip")
status=$(echo "$hdrs" | tr -d '\r' | awk 'NR==1{print $2}')
ctype=$(echo "$hdrs" | tr -d '\r' | awk -F': ' '/^[Cc]ontent-[Tt]ype/ {print $2}')
if [[ "$status" == "200" ]] && echo "$ctype" | grep -qi 'application/zip'; then
if unzip -l "$zipfile" 2>/dev/null | grep -qE 'batch1\.txt|batch2\.txt'; then
ok "POST /download-zip -> 200 application/zip with both files"
else
ko "zip missing files" "unzip -l: $(unzip -l "$zipfile" 2>&1)"
fi
else
ko "batch download baseline" "status=$status ctype=$ctype"
fi
# 非法文件名: ".." 单独出现 -> 400; "../main.go" 清洗后变 "main.go" 不存在 -> 200+miss
status=$(curl -s -o /dev/null -w "%{http_code}" -X POST -H "Content-Type: application/json" \
-d '{"files":[".."]}' "$BASE/download-zip")
[[ "$status" == "400" ]] && ok "\"..\" -> 400" || ko "explicit dotdot" "got $status"
status=$(curl -s -o /dev/null -w "%{http_code}" -X POST -H "Content-Type: application/json" \
-d '{"files":["."]}' "$BASE/download-zip")
[[ "$status" == "400" ]] && ok "\".\" -> 400" || ko "explicit dot" "got $status"
status=$(curl -s -o /dev/null -w "%{http_code}" -X POST -H "Content-Type: application/json" \
-d '{"files":["../main.go"]}' "$BASE/download-zip")
[[ "$status" == "200" || "$status" == "404" ]] && ok "../main.go sanitized, miss handled -> $status" || ko "sanitize+miss" "got $status"
# 不存在的文件: zip 仍生成, 含真实文件, 不含不存在的
zipfile2="$TMPDIR/batch2.zip"
status=$(curl -s -o "$zipfile2" -w "%{http_code}" -X POST -H "Content-Type: application/json" \
-d '{"files":["batch1.txt","does-not-exist-12345.bin"]}' "$BASE/download-zip")
if [[ "$status" == "200" ]]; then
listing=$(unzip -l "$zipfile2" 2>/dev/null || true)
if echo "$listing" | grep -q 'batch1.txt' && ! echo "$listing" | grep -q 'does-not-exist'; then
ok "missing file skipped, real file still packed"
else
ko "missing file handling" "unzip -l: $listing"
fi
else
ko "missing file request" "got $status"
fi
# 超量
files=$(python3 -c 'import json; print(json.dumps({"files":["x"]*101}))')
status=$(curl -s -o /dev/null -w "%{http_code}" -X POST -H "Content-Type: application/json" \
-d "$files" "$BASE/download-zip")
[[ "$status" == "400" ]] && ok "101 files -> 400" || ko "101 files" "got $status"
# Query 形式
status=$(curl -s -o "$TMPDIR/batch_q.zip" -w "%{http_code}" -X POST \
"$BASE/download-zip?files=batch1.txt&files=batch2.txt")
[[ "$status" == "200" ]] && ok "POST /download-zip?files=... -> 200" || ko "query form" "got $status"
# 空 files
status=$(curl -s -o /dev/null -w "%{http_code}" -X POST -H "Content-Type: application/json" \
-d '{"files":[]}' "$BASE/download-zip")
[[ "$status" == "400" ]] && ok "empty files array -> 400" || ko "empty files" "got $status"
# 清理
curl -s -X DELETE "$BASE/files/batch1.txt" > /dev/null
curl -s -X DELETE "$BASE/files/batch2.txt" > /dev/null
rm -f "$TMPDIR/batch.zip" "$TMPDIR/batch2.zip" "$TMPDIR/batch_q.zip"
# ──────────────────────────────────────────────────────────────
echo
printf "\n\033[1m========== 总计 ==========\033[0m\n"