Can't upload files with spaces in filename #71

Closed
opened 2026-01-19 18:28:51 +00:00 by michael · 1 comment
Owner

Originally created by @mpr1255 on GitHub.

Problem is simple. This works:

echo 'hello' > test1.txt && transfer test1.txt

This doesn't:

echo 'hello' > "Test 2.txt" && transfer "Test 2.txt"

Update: I just asked GPT-4 and it proposed this solution which I trialed and it works well. I also got it to fix it for bash & zsh compatibility.

The key was just the URL encoding function so instead of passing the filename straight, it encodes it. Neat!

transfer () {
    if [ $# -eq 0 ]
    then
        echo "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>" >&2
        return 1
    fi
    if tty -s
    then
        file="$1"
        file_name=$(basename "$file")
        if [ ! -e "$file" ]
        then
            echo "$file: No such file or directory" >&2
            return 1
        fi
        if [ -d "$file" ]
        then
            file_name="$file_name.zip"
            (
                cd "$file" && zip -r -q - .
            ) | curl --progress-bar --upload-file "-" "https://transfer.sh/$(urlencode "$file_name")" | tee /dev/null
        else
            cat "$file" | curl --progress-bar --upload-file "-" "https://transfer.sh/$(urlencode "$file_name")" | tee /dev/null
        fi
    else
        file_name=$1
        curl --progress-bar --upload-file "-" "https://transfer.sh/$(urlencode "$file_name")" | tee /dev/null
    fi
}

urlencode() {
    local string="${1}"
    local length="${#string}"
    for (( i=0; i<$length; i++ )); do
        local c="${string:$i:1}"
        case $c in
            [a-zA-Z0-9.~_-]) printf "$c" ;;
            *) printf '%%%02X' "'$c"
        esac
    done
}
Originally created by @mpr1255 on GitHub. Problem is simple. This works: `echo 'hello' > test1.txt && transfer test1.txt` This doesn't: `echo 'hello' > "Test 2.txt" && transfer "Test 2.txt"` Update: I just asked GPT-4 and it proposed this solution which I trialed and it works well. I also got it to fix it for bash & zsh compatibility. The key was just the URL encoding function so instead of passing the filename straight, it encodes it. Neat! ``` transfer () { if [ $# -eq 0 ] then echo "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>" >&2 return 1 fi if tty -s then file="$1" file_name=$(basename "$file") if [ ! -e "$file" ] then echo "$file: No such file or directory" >&2 return 1 fi if [ -d "$file" ] then file_name="$file_name.zip" ( cd "$file" && zip -r -q - . ) | curl --progress-bar --upload-file "-" "https://transfer.sh/$(urlencode "$file_name")" | tee /dev/null else cat "$file" | curl --progress-bar --upload-file "-" "https://transfer.sh/$(urlencode "$file_name")" | tee /dev/null fi else file_name=$1 curl --progress-bar --upload-file "-" "https://transfer.sh/$(urlencode "$file_name")" | tee /dev/null fi } urlencode() { local string="${1}" local length="${#string}" for (( i=0; i<$length; i++ )); do local c="${string:$i:1}" case $c in [a-zA-Z0-9.~_-]) printf "$c" ;; *) printf '%%%02X' "'$c" esac done } ```
Author
Owner

@paolafrancesca commented on GitHub:

@mpr1255 this was reported in https://github.com/dutchcoders/transfer.sh/issues/483 and have an alternative shell function at https://github.com/dutchcoders/transfer.sh#bash-and-zsh-with-delete-url-delete-token-output-and-prompt-before-uploading

the transfer shell function in the frontend was not fixed.
there's no need to urlencode the filename, not providing a filename and using the file and not stdin for --upload-file (without the cat $file|) is enough:

curl -v --progress-bar --upload-file hello\ 2 "https://transfer.sh/"

I would rather go for this solution, since it decreases a lot the overall complexity of the function

@paolafrancesca commented on GitHub: @mpr1255 this was reported in https://github.com/dutchcoders/transfer.sh/issues/483 and have an alternative shell function at https://github.com/dutchcoders/transfer.sh#bash-and-zsh-with-delete-url-delete-token-output-and-prompt-before-uploading the `transfer` shell function in the frontend was not fixed. there's no need to urlencode the filename, not providing a filename and using the file and not stdin for `--upload-file` (without the `cat $file|`) is enough: ``` curl -v --progress-bar --upload-file hello\ 2 "https://transfer.sh/" ``` I would rather go for this solution, since it decreases a lot the overall complexity of the function
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dutchcoders/transfer.sh#71