#!/bin/bash ADDRESS="" process_error() { if [ $1 -ne 200 ] && [ $1 -ne 204 ]; then echo "Error: HTTP $1" exit 1 fi } send_request() { local method="$1" local endpoint="$2" local data="$3" local file="$4" local curl_cmd="curl -s -w '\n%{http_code}'" if [ "$method" = "POST" ]; then if [ ! -z "$file" ]; then curl_cmd="$curl_cmd -X POST -F 'file=@$file'" else curl_cmd="$curl_cmd -X POST -H 'Content-Type: application/json'" if [ ! -z "$data" ]; then curl_cmd="$curl_cmd -d '$data'" fi fi fi local response=$(eval "$curl_cmd '${ADDRESS}${endpoint}'") local http_code=$(echo "$response" | tail -n1) local body=$(echo "$response" | sed '$d') process_error "$http_code" echo "$body" } upload_file() { local src="$1" local dst="$2" if [ ! -f "$src" ]; then echo "Error: Source file '$src' does not exist" >&2 exit 1 fi echo "Uploading: $src -> $dst" if ! curl -# --write-out "\nUpload completed: %{size_upload} bytes in %{time_total}s\n" \ -X POST -F "file=@$src" "${ADDRESS}/robotfs/file/upload?path=$dst" > /dev/null; then echo "Error: Failed to upload file" >&2 exit 1 fi } download_file() { local src="$1" local dst="$2" if [ -d "$dst" ]; then local filename=$(echo "$src" | sed 's/.*\///') dst="$dst/$filename" fi local dir=$(dirname "$dst") if [ ! -d "$dir" ]; then mkdir -p "$dir" fi echo "Downloading: $src -> $dst" if ! curl -# --write-out "\nDownload completed: %{size_download} bytes in %{time_total}s\n" \ -o "$dst" "${ADDRESS}/robotfs/file/download?path=$src&preview=false"; then echo "Error: Failed to download file" >&2 exit 1 fi } list_directory() { local path="$1" local start_file="" local more_available=true while [ "$more_available" = "true" ]; do local url="/robotfs/dir/list?path=${path}&inclusive=true&limit=50" if [ ! -z "$start_file" ]; then url="${url}&startFileName=${start_file}" fi local response=$(send_request "GET" "$url") echo "$response" | sed 's/.*"entries":\[//;s/\].*$//' | sed 's/},{/}\n{/g' | \ while IFS= read -r line; do if [ -z "$line" ]; then continue fi name=$(echo "$line" | grep -o '"name":"[^"]*"' | cut -d'"' -f4) is_dir=$(echo "$line" | grep -o '"is_dir":[^,]*' | cut -d':' -f2) size=$(echo "$line" | grep -o '"size":[^,]*' | cut -d':' -f2) if [ "$is_dir" = "true" ]; then echo " - $name [DIR]" else printf " - %s %s [FILE]\n" "$name" "$size" fi done more_available=$(echo "$response" | grep -o '"more_available":[^,]*' | cut -d':' -f2) if [ "$more_available" = "true" ]; then start_file=$(echo "$response" | grep -o '"last_file_name":"[^"]*"' | cut -d'"' -f4) fi done } move_item() { local src="$1" local dst="$2" local data="{\"SrcPath\":\"$src\",\"DstPath\":\"$dst\",\"IsDir\":false}" echo "Moving: $src -> $dst" if ! send_request "POST" "/robotfs/file/rename" "$data" > /dev/null; then echo "Error: Failed to move file" >&2 exit 1 fi echo "Move completed" } copy_item() { local src="$1" local dst="$2" local data="{\"SrcPath\":\"$src\",\"DstPath\":\"$dst\",\"IsDir\":false}" echo "Copying: $src -> $dst" if ! send_request "POST" "/robotfs/file/copy" "$data" > /dev/null; then echo "Error: Failed to copy file" >&2 exit 1 fi echo "Copy completed" } remove_item() { local path="$1" local data="{\"Path\":\"$path\",\"IsDir\":false}" echo "Removing: $path" if ! send_request "POST" "/robotfs/file/delete" "$data" > /dev/null; then echo "Error: Failed to remove file" >&2 exit 1 fi echo "Remove completed" } move_dir() { local src="$1" local dst="$2" local data="{\"SrcPath\":\"$src\",\"DstPath\":\"$dst\",\"IsDir\":true}" echo "Moving directory: $src -> $dst" if ! send_request "POST" "/robotfs/dir/rename" "$data" > /dev/null; then echo "Error: Failed to move directory" >&2 exit 1 fi echo "Move completed" } copy_dir() { local src="$1" local dst="$2" local data="{\"SrcPath\":\"$src\",\"DstPath\":\"$dst\",\"IsDir\":true}" echo "Copying directory: $src -> $dst" if ! send_request "POST" "/robotfs/dir/copy" "$data" > /dev/null; then echo "Error: Failed to copy directory" >&2 exit 1 fi echo "Copy completed" } remove_dir() { local path="$1" local data="{\"Path\":\"$path\",\"IsDir\":true}" echo "Removing directory: $path" if ! send_request "POST" "/robotfs/dir/delete" "$data" > /dev/null; then echo "Error: Failed to remove directory" >&2 exit 1 fi echo "Remove completed" } copy_dir() { local src="$1" local dst="$2" local data="{\"SrcPath\":\"$src\",\"DstPath\":\"$dst\",\"IsDir\":true}" echo "Copying directory: $src -> $dst" if ! send_request "POST" "/robotfs/dir/copy" "$data" > /dev/null; then echo "Error: Failed to copy directory" >&2 exit 1 fi echo "Copy completed" } make_directory() { local path="$1" local data="{\"Path\":\"$path\"}" echo "Creating directory: $path" if ! send_request "POST" "/robotfs/mkdir" "$data" > /dev/null; then echo "Error: Failed to create directory" >&2 exit 1 fi echo "Create completed" } show_usage() { echo "Usage: $0
[arguments]" echo echo "Address format:" echo " : Example: localhost:8080 or 127.0.0.1:8080" echo echo "Commands:" echo " mkdir Create directory" echo " ls List directory contents" echo " mv [-r] Move file or directory (-r for directory)" echo " cp [-r] Copy file or directory (-r for directory)" echo " rm [-r] Remove file or directory (-r for directory)" echo " put Put local file to remote path" echo " get Get remote file to local path" } main() { if [ $# -lt 2 ]; then show_usage exit 1 fi ADDRESS="$1" shift COMMAND="$1" shift if [[ ! $ADDRESS =~ ^http:// ]]; then ADDRESS="http://$ADDRESS" fi ADDRESS="${ADDRESS%/}" case "$COMMAND" in "ls") if [ $# -ne 1 ]; then echo "Usage: $0
ls " exit 1 fi list_directory "$1" ;; "mv") if [ $# -lt 2 ] || [ $# -gt 3 ]; then echo "Usage: $0
mv [-r] " exit 1 fi if [ "$1" = "-r" ]; then shift move_dir "$1" "$2" else move_item "$1" "$2" fi ;; "cp") if [ $# -lt 2 ] || [ $# -gt 3 ]; then echo "Usage: $0
cp [-r] " exit 1 fi if [ "$1" = "-r" ]; then shift copy_dir "$1" "$2" else copy_item "$1" "$2" fi ;; "rm") if [ $# -lt 1 ] || [ $# -gt 2 ]; then echo "Usage: $0
rm [-r] " exit 1 fi if [ "$1" = "-r" ]; then shift remove_dir "$1" else remove_item "$1" fi ;; "mkdir") if [ $# -ne 1 ]; then echo "Usage: $0
mkdir " exit 1 fi make_directory "$1" ;; "put") if [ $# -ne 2 ]; then echo "Usage: $0
put " exit 1 fi upload_file "$1" "$2" ;; "get") if [ $# -ne 2 ]; then echo "Usage: $0
get " exit 1 fi download_file "$1" "$2" ;; *) echo "Unknown command: $COMMAND" show_usage exit 1 ;; esac } main "$@"