making sure files are not overwritten
This commit is contained in:
44
download.sh
44
download.sh
@ -15,8 +15,7 @@ SCRIPT_NAME=${0##*/}
|
||||
VERSION="1.2.2"
|
||||
|
||||
# ------------------------------- logging & utils -------------------------------
|
||||
log() { printf '[%s] %s
|
||||
' "$(date '+%F %T%z')" "$*" >&2; }
|
||||
log() { printf '[%s] %s\n' "$(date '+%F %T%z')" "$*" >&2; }
|
||||
die() { log "ERROR: $*"; exit 1; }
|
||||
need() { command -v "$1" >/dev/null 2>&1 || die "Kräver '$1' i PATH"; }
|
||||
|
||||
@ -80,6 +79,36 @@ trap cleanup EXIT INT TERM
|
||||
OUTDIR=${OUTDIR:-$PWD}
|
||||
mkdir -p "$OUTDIR"
|
||||
|
||||
# ---- non-overwriting target helper (added) ----
|
||||
# Ensure target filename never overwrites: foo.mp4 -> foo.1.mp4, foo.2.mp4, ...
|
||||
unique_target() {
|
||||
local path="$1"
|
||||
[[ ! -e "$path" ]] && { printf '%s\n' "$path"; return 0; }
|
||||
|
||||
local dir file base ext i candidate
|
||||
dir=${path%/*}
|
||||
file=${path##*/}
|
||||
|
||||
if [[ "$file" == *.* ]]; then
|
||||
base=${file%.*}
|
||||
ext=${file##*.}
|
||||
else
|
||||
base=$file
|
||||
ext=""
|
||||
fi
|
||||
|
||||
i=1
|
||||
while :; do
|
||||
if [[ -n "$ext" ]]; then
|
||||
candidate="$dir/${base}.${i}.${ext}"
|
||||
else
|
||||
candidate="$dir/${base}.${i}"
|
||||
fi
|
||||
[[ ! -e "$candidate" ]] && { printf '%s\n' "$candidate"; return 0; }
|
||||
((i++))
|
||||
done
|
||||
}
|
||||
|
||||
# ---------------------------- idx path resolution -----------------------------
|
||||
IDX="" # will be set via set_idx_from_url
|
||||
|
||||
@ -237,8 +266,15 @@ download_and_save_link() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
log "Finalizing -> $final"
|
||||
mv -f "$tmp" "$final"
|
||||
# ---- changed: pick a non-conflicting final target ----
|
||||
local final_target
|
||||
final_target=$(unique_target "$final")
|
||||
if [[ $final_target != "$final" ]]; then
|
||||
log "Target exists, using: $final_target"
|
||||
fi
|
||||
|
||||
log "Finalizing -> $final_target"
|
||||
mv -f "$tmp" "$final_target"
|
||||
}
|
||||
|
||||
do_post() {
|
||||
|
||||
Reference in New Issue
Block a user