pz / entrypoint.sh
github-actions[bot]
GitHub deploy: 8e0cf89c62ff7a04ee3ab3eb49ace583254bec93
a87464b
#!/bin/bash
set -euo pipefail
UPLOADED_FILES_RECORD="/app/uploaded_files.txt"
WATCH_DIR="pdf2zh_files"
cleanup() {
echo "正在退出..."
exit 0
}
trap cleanup SIGTERM SIGINT
echo "$APP_USERS" > /app/users.txt
chmod 600 /app/users.txt
pdf2zh -i --authorized /app/users.txt &
if [ ! -f "$UPLOADED_FILES_RECORD" ]; then
touch "$UPLOADED_FILES_RECORD"
fi
if [ ! -d "$WATCH_DIR" ]; then
mkdir "$WATCH_DIR"
fi
upload_file() {
local file="$1"
(
flock -n 9 || exit 1
if grep -Fxq "$file" "$UPLOADED_FILES_RECORD"; then
echo "文件 $file 已经上传过,跳过。"
exit 0
fi
echo "正在上传 $file..."
encoded_file=$(python3 -c "import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1]))" "$file")
if curl -T "$WATCH_DIR/$file" "$WEBDAV_URL/$encoded_file" --user "$WEBDAV_USER:$WEBDAV_PASS"; then
echo "成功上传 $file"
echo "$file" >> "$UPLOADED_FILES_RECORD"
else
echo "上传 $file 失败" >&2
fi
) 9>>"$UPLOADED_FILES_RECORD.lock"
}
if command -v inotifywait >/dev/null 2>&1; then
echo "开始监控 $WATCH_DIR 文件夹..."
inotifywait -m -e create -e moved_to --format '%f' "$WATCH_DIR" |
while read -r filename; do
# 检查文件是否为 PDF,并且存在
if [[ "$filename" =~ \.pdf$ ]] && [ -f "$WATCH_DIR/$filename" ]; then
upload_file "$filename"
fi
done
else
echo "未安装 inotifywait,使用定期检查方式。您可以通过 'sudo apt-get install inotify-tools' 安装 inotifywait。"
while true; do
for local_file in "$WATCH_DIR"/*.pdf; do
if [ -f "$local_file" ]; then
filename=$(basename "$local_file")
# 检查文件是否已上传
if ! grep -Fxq "$filename" "$UPLOADED_FILES_RECORD"; then
upload_file "$filename"
fi
fi
done
sleep 60
done
fi