orcli/src/lib/init_import.sh

58 lines
2.0 KiB
Bash
Raw Normal View History

2022-04-13 21:55:47 +00:00
# common import tasks to support multiple files and URLs
# shellcheck shell=bash
function init_import() {
2022-12-06 11:39:08 +00:00
local files file
2022-04-13 21:55:47 +00:00
# catch args, convert the space delimited string to an array
files=()
eval "files=(${args[file]})"
# create tmp directory
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' 0 2 3 15
# download files if name starts with http:// or https://
for i in "${!files[@]}"; do
if [[ ${files[$i]} == "http://"* ]] || [[ ${files[$i]} == "https://"* ]]; then
2022-04-20 09:39:08 +02:00
if ! curl -fs --location "${files[$i]}" >"${tmpdir}/${files[$i]//[^A-Za-z0-9._-]/_}"; then
2022-04-13 21:55:47 +00:00
error "download of ${files[$i]} failed!"
fi
2022-04-20 09:39:08 +02:00
files[$i]="${tmpdir}/${files[$i]//[^A-Za-z0-9._-]/_}"
2022-04-13 21:55:47 +00:00
fi
done
2022-04-20 09:40:55 +02:00
# read pipes if name starts with /dev/fd
for i in "${!files[@]}"; do
if [[ ${files[$i]} == "/dev/fd"* ]]; then
if ! cat "${files[$i]}" >"${tmpdir}/${files[$i]//[^A-Za-z0-9._-]/_}"; then
error "reading of ${files[$i]} failed!"
fi
files[$i]="${tmpdir}/${files[$i]//[^A-Za-z0-9._-]/_}"
fi
done
2022-04-13 21:55:47 +00:00
# create a zip archive if there are multiple files
if [[ ${#files[@]} -gt 1 ]]; then
file="$tmpdir/Untitled.zip"
2022-04-20 09:45:32 +02:00
if ! zip --quiet --must-match "$file" "${files[@]}"; then
error "creating zip archive with ${files[*]} failed!"
fi
2022-04-13 21:55:47 +00:00
else
file="${files[0]}"
fi
# basic post data
if [[ ${file} == "-" ]]; then
data+=("project-file=@-")
else
if ! path=$(readlink -e "${file}"); then
2022-11-15 23:06:41 +00:00
error "cannot open ${file} (no such file)!"
2022-04-13 21:55:47 +00:00
fi
data+=("project-file=@${path}")
fi
if [[ ${args[--projectName]} ]]; then
data+=("project-name=${args[--projectName]}")
else
if [[ ${file} == "-" ]]; then
name="Untitled"
else
name="$(basename "${path}" | tr '.' ' ')"
fi
data+=("project-name=${name}")
fi
}