truncating

This commit is contained in:
Urban Modig
2025-10-26 23:45:10 +01:00
parent aff8b5e31b
commit c261dca844

View File

@ -170,9 +170,30 @@ get_items() {
# Expecting a single line: ["<filename>","<download_url>"]
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'
}