diff --git a/download.sh b/download.sh index a00a8e9..553e28a 100755 --- a/download.sh +++ b/download.sh @@ -170,9 +170,30 @@ get_items() { # Expecting a single line: ["",""] JSON_PAIR_RE='^\["([^"]*)","([^"]*)"\]$' +#sanitize_filename() { +# local in=$1 +# local truncated=${in:0:80} +# # replace anything not alnum, underscore, dot or dash with underscore +# printf '%s' "$truncated" | sed 's/[^A-Za-z0-9_.-]/_/g' +#} + sanitize_filename() { local in=$1 - local truncated=${in:0:80} + local truncated + # find cutoff at first space after 80th character + if (( ${#in} > 80 )); then + local prefix=${in:0:80} + local remainder=${in:80} + if [[ $remainder == *' '* ]]; then + local next_space_pos + next_space_pos=$(expr index "$remainder" ' ') + truncated="${prefix}${remainder:0:next_space_pos}" + else + truncated=$in + fi + else + truncated=$in + fi # replace anything not alnum, underscore, dot or dash with underscore printf '%s' "$truncated" | sed 's/[^A-Za-z0-9_.-]/_/g' }