WORKDIR
This commit is contained in:
40
download.sh
40
download.sh
@ -33,7 +33,16 @@ log "Starting $SCRIPT_NAME v$VERSION (PID $$)"
|
||||
# Safer temp dir for partial files, etc.
|
||||
TMPDIR=${TMPDIR:-/tmp}
|
||||
WORKDIR=$(mktemp -d "$TMPDIR/${SCRIPT_NAME%.sh}.XXXXXX")
|
||||
cleanup_tmp() { rm -rf "$WORKDIR" 2>/dev/null || true; }
|
||||
cleanup_tmp() {
|
||||
# Remove WORKDIR only if empty; otherwise leave it for recovery
|
||||
if [[ -d "$WORKDIR" ]]; then
|
||||
if find "$WORKDIR" -mindepth 1 -print -quit | grep -q .; then
|
||||
log "Leaving WORKDIR (not empty): $WORKDIR"
|
||||
else
|
||||
rmdir "$WORKDIR" 2>/dev/null && log "Removed empty WORKDIR: $WORKDIR"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# ----------------------------- lock/unlock logic ------------------------------
|
||||
# Use a fixed numeric FD (200) for wider Bash compatibility (e.g., macOS bash 3.2).
|
||||
@ -185,7 +194,31 @@ download_and_save_link() {
|
||||
local base_name=$2
|
||||
local sanitized
|
||||
sanitized=$(sanitize_filename "$base_name")
|
||||
local outpath="$OUTDIR/${sanitized}.mp4"
|
||||
local tmp="$WORKDIR/${sanitized}.mp4.part"
|
||||
local final="$OUTDIR/${sanitized}.mp4"
|
||||
|
||||
log "Downloading $url -> $tmp"
|
||||
(( NOOP )) && { log "(dry-run) Skipping download"; return 0; }
|
||||
|
||||
# Ensure OUTDIR exists before final move
|
||||
mkdir -p "$OUTDIR"
|
||||
|
||||
if ! curl "${CURL_OPTS[@]}" -o "$tmp" "$url"; then
|
||||
log "Curl failed to download the URL: $url"
|
||||
rm -f "$tmp" 2>/dev/null || true
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Basic sanity check: non-empty file
|
||||
if [[ ! -s "$tmp" ]]; then
|
||||
log "Downloaded file is empty: $tmp"
|
||||
rm -f "$tmp" 2>/dev/null || true
|
||||
return 1
|
||||
fi
|
||||
|
||||
log "Finalizing -> $final"
|
||||
mv -f "$tmp" "$final"
|
||||
}.mp4"
|
||||
log "Downloading $url -> $outpath"
|
||||
(( NOOP )) && { log "(dry-run) Skipping download"; return 0; }
|
||||
curl "${CURL_OPTS[@]}" -o "$outpath.part" "$url" || { rm -f "$outpath.part"; return 1; }
|
||||
@ -292,10 +325,11 @@ Options:
|
||||
-h Help
|
||||
|
||||
Env vars:
|
||||
OUTDIR Output directory (default: current dir)
|
||||
OUTDIR Output directory for completed files (default: current dir)
|
||||
PAGES How many collection pages to traverse (default: $PAGES)
|
||||
SLEEP_BETWEEN_PAGES Seconds between pages (default: $SLEEP_BETWEEN_PAGES)
|
||||
LOCAL_RESOLVER_URL Resolver endpoint (default: $LOCAL_RESOLVER_URL)
|
||||
TMPDIR Base directory for WORKDIR mktemp (default: /tmp)
|
||||
EOF
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user