Skip to content

Instantly share code, notes, and snippets.

@desyncr
Forked from xor-freenet/insert-file
Created July 13, 2016 20:49
Show Gist options
  • Save desyncr/983cf81f69b4a1dc1bd2f0fac74a62e8 to your computer and use it in GitHub Desktop.
Save desyncr/983cf81f69b4a1dc1bd2f0fac74a62e8 to your computer and use it in GitHub Desktop.
bash script to insert a file into Freenet (using FCP) (works on OS X)
#!/bin/bash
#
# GUID:F9FC4F74-501A-46BC-B57E-F9D8B4EFB83E
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-${3}-${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
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 "-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 "-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
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
;;
-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
;;
-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}")
# clear options values
unset TARGET_URI CONTENT_TYPE DONT_COMPRESS PRIORITY FILTER COMPATIBILITY
;;
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}]}"
if [ ! -f "${FILE}" ]; then
echo "invalid file given, skipping ${FILE}."
continue
fi
if [ -z "${FILENAME}" ]; then
FILENAME="$(basename "${FILE}")"
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="$(stat -f "%z" "${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}"
${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