Files
xhgitandClaude Code cf17c9ea42 Initial commit: 小南娘 - 面向可爱的蓝孩子的 git 学习实践项目
仿 Dress 的照片投稿仓库, 适配自建 Gitea 平台 git.xhmcl.com:
- README / GUIDE (PR 教程) / CONTRIBUTING / Q&A / EXIF / PARTIAL_CLONE (中英双语)
- A-Z + # 首字母目录结构
- .gitea: PR 模板、维护者名单、7 个纯 bash + API 的 Actions 工作流 (可选功能)

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-07 17:17:27 +08:00

147 lines
6.7 KiB
YAML

# Check Image EXIF Data
#
# 仅在 PR 为图片提交时检查变更图片是否含高敏感 EXIF (GPS / 地址 / 联系方式).
# 普通摄影参数 (光圈, 快门等) 不拦截. (纯 shell + 平台 API)
#
# 要求:
# - 平台启用 Gitea Actions 并注册 ubuntu runner (需要 apt 安装 exiftool)
# - 若 Gitea 版本不支持 pull_request_target, 请改为 pull_request
name: Check Image EXIF Data
on:
pull_request_target:
types: [opened, synchronize, reopened]
concurrency:
group: exif-pr-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
check-exif:
runs-on: ubuntu-latest
steps:
- name: Install exiftool and jq
run: |
sudo apt-get install -y --no-install-recommends libimage-exiftool-perl jq \
|| (sudo apt-get update && sudo apt-get install -y --no-install-recommends libimage-exiftool-perl jq)
- name: Check EXIF in changed images
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
API: ${{ gitea.server_url || 'https://git.xhmcl.com' }}/api/v1
REPO: ${{ github.repository }}
PR_NUM: ${{ github.event.pull_request.number }}
HEAD_OWNER: ${{ github.event.pull_request.head.repo.owner.login }}
HEAD_REPO: ${{ github.event.pull_request.head.repo.name }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
OWNER="${REPO%%/*}"
NAME="${REPO#*/}"
IMAGE_EXT_RE='\.(jpe?g|jfif|png|webp|tiff?|hei[cf]|avif|gif|bmp)$'
ACTIVE_RE='^(added|modified|renamed|copied)$'
# GPS / IPTC address / XMP location & contact
HIGH_SENS_ARGS=(
'-GPS:GPSLatitude' '-GPS:GPSLongitude' '-GPS:GPSAltitude'
'-GPS:GPSDateStamp' '-GPS:GPSTimeStamp'
'-GPS:GPSSpeed' '-GPS:GPSTrack' '-GPS:GPSImgDirection'
'-IPTC:City' '-IPTC:Country-PrimaryLocationName'
'-IPTC:Sub-location' '-IPTC:Province-State'
'-XMP-photoshop:City' '-XMP-photoshop:Country' '-XMP-photoshop:State'
'-XMP-iptcCore:CreatorWorkEmail' '-XMP-iptcCore:CreatorWorkTelephone'
'-XMP-iptcCore:CreatorCity' '-XMP-iptcCore:CreatorCountry'
'-XMP-iptcCore:CreatorPostalCode'
)
api() {
curl -fsS -H "Authorization: token ${TOKEN}" -H "Content-Type: application/json" "$@"
}
PR_BODY=$(api "$API/repos/$OWNER/$NAME/pulls/$PR_NUM" | jq -r '.body // ""')
PR_TYPE=$(printf '%s\n' "$PR_BODY" | grep -oE 'pr-type:[[:space:]]*[A-Za-z_0-9]+' | head -1 | cut -d: -f2 | tr -d '[:space:]')
PR_TYPE=${PR_TYPE:-images}
if [ "$PR_TYPE" != "images" ]; then
echo "pr-type is \"$PR_TYPE\", skip EXIF check."
exit 0
fi
FILES_TMP=$(mktemp)
page=1
while :; do
resp=$(api "$API/repos/$OWNER/$NAME/pulls/$PR_NUM/files?limit=50&page=$page")
n=$(echo "$resp" | jq 'if type=="array" then length else 0 end')
echo "$resp" | jq -r '.[] | "\(.status // "unknown")\t\(.filename // "")"' >> "$FILES_TMP" || true
[ "$n" -lt 50 ] && break
page=$((page + 1))
done
IMAGES=$(awk -F'\t' -v re="$ACTIVE_RE" '$1 ~ re {print $2}' "$FILES_TMP" | grep -iE "$IMAGE_EXT_RE" || true)
if [ -z "$IMAGES" ]; then
echo "No changed images found, skip EXIF check."
exit 0
fi
echo "Checking $(printf '%s\n' "$IMAGES" | grep -c .) image(s) for high-sensitivity EXIF."
encode_path() {
printf '%s' "$1" | sed -e 's/%/%25/g' -e 's/#/%23/g' -e 's/?/%3F/g' -e 's/ /%20/g'
}
TMP_DIR=$(mktemp -d)
EXIF_FILES=()
while IFS= read -r file; do
[ -z "$file" ] && continue
enc=$(encode_path "$file")
content_json=$(api "$API/repos/$HEAD_OWNER/$HEAD_REPO/contents/$enc?ref=$HEAD_SHA" 2>/dev/null || true)
b64=$(echo "$content_json" | jq -r 'if type=="object" and .content then .content else empty end' 2>/dev/null || true)
if [ -z "$b64" ]; then
echo "Warning: cannot fetch $file, skip."
continue
fi
tmp_file="$TMP_DIR/$(printf '%s' "$file" | tr '/\\' '__')"
printf '%s' "$b64" | base64 -d > "$tmp_file" 2>/dev/null || { echo "Warning: decode failed $file, skip."; continue; }
# Drop empty/undef/all-zero placeholders (e.g. GPS IFD with no coords).
count=$(exiftool "${HIGH_SENS_ARGS[@]}" -S -q -- "$tmp_file" 2>/dev/null \
| grep -Ev ': *(undef|00:00:00)?$' | wc -l)
count=${count// /}
if [ "${count:-0}" -gt 0 ]; then
EXIF_FILES+=("$file")
echo "Sensitive EXIF found: $file ($count tags)"
fi
rm -f "$tmp_file"
done <<< "$IMAGES"
rm -rf "$TMP_DIR"
if [ "${#EXIF_FILES[@]}" -gt 0 ]; then
MSG="我们检测到以下文件包含**高敏感 EXIF 信息**(如 GPS 坐标, 地址或联系方式), 请移除后再次提交:
We detected **high-sensitivity EXIF data** (e.g. GPS coordinates, address, or contact fields) in the following files. Please remove them and resubmit:
$(printf '%s\n' "${EXIF_FILES[@]}" | sed 's/^/- /')
**仅高敏感字段 (GPS / 地址 / 联系方式) 需要移除, 普通摄影参数 (光圈, 快门等) 无需处理.**
**Only high-sensitivity fields (GPS / address / contact info) need to be removed. Regular photography parameters (aperture, shutter speed, etc.) are fine to keep.**
[EXIF 说明 / EXIF Guide](https://git.xhmcl.com/$OWNER/$NAME/src/branch/main/EXIF.md) · [CONTRIBUTING.md](https://git.xhmcl.com/$OWNER/$NAME/src/branch/main/CONTRIBUTING.md)
---
温馨提示: 您可以直接使用 \`/auto-fix\` 命令来自动修复这些文件.
Warm reminder: You can use the \`/auto-fix\` command to automatically fix these files.
该命令会将图片压缩到 1 MiB 内, 并且去除**所有的 EXIF**信息, 若您不希望文件被改动, 请自行处理后重新提交 PR
This command will compress images to under 1 MiB and strip all EXIF metadata.
If you do not want your files modified, please fix them yourself and push again."
BODY_JSON=$(jq -n --arg b "$MSG" '{"body": $b}')
api -X POST -d "$BODY_JSON" "$API/repos/$OWNER/$NAME/issues/$PR_NUM/comments" \
|| echo "Warning: cannot comment (check bot account permission)."
echo "High-sensitivity EXIF data detected."
exit 1
fi
echo "No high-sensitivity EXIF data found."