Skip to content

Instantly share code, notes, and snippets.

@Bombe
Last active April 18, 2024 08:06
Show Gist options
  • Save Bombe/4d38ba7fbb79f163064f16fdc13d1b55 to your computer and use it in GitHub Desktop.
Save Bombe/4d38ba7fbb79f163064f16fdc13d1b55 to your computer and use it in GitHub Desktop.
Shell Script to Upload a Single File via FCP
#!/bin/bash
# 8e38d288-e581-44f3-bf47-bb0962d25b63
function client_hello() {
echo "ClientHello"
echo "Name=${1}"
echo "ExpectedVersion=2.0"
echo "EndMessage"
}
function client_put() {
echo "ClientPut"
echo "URI=${1}"
echo "Metadata.ContentType=${2}"
echo "Identifier=direct-insert-${9}-${RANDOM}"
echo "Global=true"
echo "PriorityClass=${5}"
echo "MaxRetries=-1"
echo "Persistence=forever"
echo "TargetFilename=${3}"
echo "DataLength=${4}"
if [ "${6}" == "1" ]; then
echo "DontCompress=true"
fi
if [ "${8}" != "0" ]; then
echo "UploadFrom=redirect"
echo "TargetURI=${8}"
fi
echo "CompatibilityMode=${7}"
echo "EndMessage"
}
function printHelp() {
echo "Syntax: $0 [<general options>] [<file-specific options>] <file> [[<file-specific options>] <file> [...]]"
echo "general options:"
echo "-h <host>, --host <host> Hostname of the node (default: localhost)"
echo "-p <port>, --port <port> The FCP port number of the node (default: 9481)"
echo "-v, --verbose Be verbose"
echo "-D, --debug Output FCP responses"
echo "file-specific options:"
echo "-i <identifier>, --identifier <identifier> The identifier for the insert"
echo "-u <uri>, --uri <uri> The target URI (default: CHK@)"
echo "-t <type>, --type <type> MIME type of the file (default: autodetect)"
echo "-n <name>, --name <name> Insert file as <name> (default: original filename)"
echo "-r <target>, --redirect <target> Insert a redirect instead of a file"
echo "-d, --dont-compress Don't compress the file (default: compress)"
echo "-P <priority>, --priority <priority> Use given priority (0 is highest, 6 is lowest, default is 3)"
echo "-f <filter>, --filter <filter> Run file through filter (default: none)"
echo "-C <compatibility mode> Use compatibility mode (default: COMPAT_CURRENT)"
}
# internal stuff
CLIENT_NAME="insert-file.sh:$$"
# general options
NODE_HOST="localhost"
NODE_PORT="9481"
VERBOSE="0"
DEBUG="0"
declare -a TARGET_URIS
declare -a CONTENT_TYPES
declare -a FILES
declare -a FILENAMES
declare -a DONT_COMPRESSES
declare -a PRIORITIES
declare -a FILTERS
declare -a COMPATIBILITIES
declare -a REDIRECT_TARGETS
declare -a IDENTIFIERS
inGeneralOptions="1"
while [ -n "${1}" ]; do
case "${1}" in
-h|--host)
if [ "${inGeneralOptions}" == "1" ]; then
NODE_HOST="${2}"
shift
else
echo "-h is not allowed in file-specific options."
exit 1
fi
;;
-p|--port)
if [ "${inGeneralOptions}" == "1" ]; then
NODE_PORT="${2}"
shift
else
echo "-p is not allowed in file-specific options."
exit 1
fi
;;
-v|--verbose)
if [ "${inGeneralOptions}" == "1" ]; then
VERBOSE="1"
else
echo "-v is not allowed in file-specific options."
exit 1
fi
;;
-D|--debug)
if [ "${inGeneralOptions}" == "1" ]; then
DEBUG="1"
else
echo "-D is not allowed in file-specific options."
exit 1
fi
;;
-i|--identifier)
inGeneralOptions="0"
IDENTIFIER="${2}"
shift
;;
-u|--uri)
inGeneralOptions="0"
TARGET_URI="${2}"
shift
;;
-t|--type)
inGeneralOptions="0"
CONTENT_TYPE="${2}"
shift
;;
-f|--filter)
inGeneralOptions="0"
FILTER="${2}"
shift
;;
-n|--name)
inGeneralOptions="0"
FILENAME="${2}"
shift
;;
-r|--redirect)
inGeneralOptions="0"
REDIRECT_TARGET="${2}"
shift
;;
-d|--dont-compress)
inGeneralOptions="0"
DONT_COMPRESS="1"
;;
-P|--priority)
inGeneralOptions="0"
PRIORITY="${2}"
shift
;;
-C)
inGeneralOptions="0"
COMPATIBILITY="${2}"
shift
;;
-*)
echo "unrecognized parameter: ${1}"
printHelp
exit
;;
*)
# assign collected values to arrays
TARGET_URIS=("${TARGET_URIS[@]}" "${TARGET_URI:-CHK@}")
CONTENT_TYPES=("${CONTENT_TYPES[@]}" "${CONTENT_TYPE}")
FILES=("${FILES[@]}" "${1}")
FILENAMES=("${FILENAMES[@]}" "${FILENAME}")
DONT_COMPRESSES=("${DONT_COMPRESSES[@]}" "${DONT_COMPRESS}")
PRIORITIES=("${PRIORITIES[@]}" "${PRIORITY:-4}")
FILTERS=("${FILTERS[@]}" "${FILTER:-/bin/cat}")
COMPATIBILITIES=("${COMPATIBILITIES[@]}" "${COMPATIBILITY:-COMPAT_CURRENT}")
REDIRECT_TARGETS=("${REDIRECT_TARGETS[@]}" "${REDIRECT_TARGET:-0}")
IDENTIFIERS=("${IDENTIFIERS[@]}" "${IDENTIFIER}")
# clear options values
unset TARGET_URI CONTENT_TYPE DONT_COMPRESS PRIORITY FILTER COMPATIBILITY REDIRECT_TARGET IDENTIFIER
;;
esac
shift
done
if [ "${#FILES[@]}" == "0" ]; then
echo "no file given."
printHelp
exit
fi
index="0"
if [ "${VERBOSE}" == "1" ]; then
echo "inserting ${#FILES[@]} files..."
fi
while [ -n "${FILES[${index}]}" ]; do
# extract parameters
FILE="${FILES[${index}]}"
FILENAME="${FILENAMES[${index}]}"
TARGET_URI="${TARGET_URIS[${index}]}"
CONTENT_TYPE="${CONTENT_TYPES[${index}]}"
DONT_COMPRESS="${DONT_COMPRESSES[${index}]}"
PRIORITY="${PRIORITIES[${index}]}"
FILTER="${FILTERS[${index}]}"
COMPATIBILITY="${COMPATIBILITIES[${index}]}"
REDIRECT_TARGET="${REDIRECT_TARGETS[${index}]}"
IDENTIFIER="${IDENTIFIERS[${index}]}"
if [ ! -f "${FILE}" ]; then
echo "invalid file given, skipping ${FILE}."
continue
fi
if [ -z "${FILENAME}" ]; then
FILENAME="$(basename "${FILE}")"
fi
if [ -z "${IDENTIFIER}" ]; then
IDENTIFIER="${FILENAME}"
fi
if [ -z "${CONTENT_TYPE}" ]; then
CONTENT_TYPE="$(/usr/bin/file -bI "${FILE}" 2> /dev/null)"
fi
if [ "${VERBOSE}" == "1" ]; then
echo "inserting ${FILE} (as ${FILENAME}) to ${TARGET_URI}..."
fi
LENGTH="$(wc -c < "${FILE}")"
if [ "${DEBUG}" == "1" ]; then
OUTPUT="/dev/stdout"
else
OUTPUT="/dev/null"
fi
(
client_hello "${CLIENT_NAME}"
client_put "${TARGET_URI}" "${CONTENT_TYPE}" "${FILENAME}" "${LENGTH}" "${PRIORITY}" "${DONT_COMPRESS}" "${COMPATIBILITY}" "${REDIRECT_TARGET}" "${IDENTIFIER}"
${FILTER} < "${FILE}"
) | nc "${NODE_HOST}" "${NODE_PORT}" > "${OUTPUT}"
index="$((${index} + 1))"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment