Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URL encoding to transfer function to handle whitespaces #548

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

snowphone
Copy link
Contributor

Add URL encoding functionality to the transfer function in README.md so that file names with spaces can be uploaded correctly (e.g. hello world.json).

Also, the code uses the _urlencode function written in Python3. I know not every *nix OS includes Python3 by default but I'm familiar with Python and I don't know how to make it more portable.

This commit includes changes to the following:

  • Update the transfer function to use _urlencode for encoding the file names as URL.
  • Add the _urlencode function to URL encode the file names.

Since this oneliner is too hard to review, I uploaded formatted code too:
before.txt
after.txt

If this PR is okay, I'll make a PR to tranfer.sh-web too

Add URL encoding functionality to the `transfer` function in `README.md` so that file names with spaces can be uploaded correctly.

Also, the code uses the `_urlencode` function written in Python.

This commit includes changes to the following:

- Update the `transfer` function to use `_urlencode` for encoding the file names as URL.
- Add the `_urlencode` function to URL encode the file names.

Note: This change improves the functionality of the `transfer` function by making it more usable.
@aspacca
Copy link
Collaborator

aspacca commented Apr 29, 2023

hi @snowphone , thanks for the PR

please, see my comment at #546 (comment)

the best solution would be to avoid cat and do not add the filename in the url

cat "$file"|curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"

vs

curl --progress-bar --upload-file "$file" "https://transfer.sh/"

so no need for urlencode and $file_name

The transfer function no more relies on _urlencode function and just
uploads to https://transfer.sh
@snowphone
Copy link
Contributor Author

Wow it's too simple 😂
I changed this PR as you advised.

@@ -243,7 +243,7 @@ You need to create an OAuth Client id from console.cloud.google.com, download th
### Bash and zsh (multiple files uploaded as zip archive)
##### Add this to .bashrc or .zshrc or its equivalent
```bash
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/$file_name"|tee /dev/null,;else cat "$file"|curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"|tee /dev/null;fi;else file_name=$1;curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"|tee /dev/null;fi;}
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/"|tee /dev/null;else cat "$file"|curl --progress-bar --upload-file "-" "https://transfer.sh/"|tee /dev/null;fi;else file_name=$1;curl --progress-bar --upload-file "-" "https://transfer.sh/"|tee /dev/null;fi;}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if [ -d "$file" ];then file_name="$file_name.zip" ,;(cd "$file"&&zip -r -q - .)|curl --progress-bar --upload-file "-" "https://transfer.sh/"|tee /dev/null;

here $file is a directory, we cd into it and zip recursively sending output to stdout, curl get from stdin the upload file from stdin.
we should change to zip to file and use it for curl

else cat "$file"|curl --progress-bar --upload-file "-" "https://transfer.sh/"|tee /dev/null;fi

same here: it's not enough to remove $file_name, we should remove cat and curl getting the upload file from stdin:

else curl --progress-bar --upload-file "$file" "https://transfer.sh/"|tee /dev/null;fi
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_name=$(basename "$1");
        if [ ! -e "$1" ];then 
            echo "$1: No such file or directory">&2;
            return 1;
        fi;
        if [ -d   "$1" ];then 
            zip_file="$1.zip";
            pushd "$1";
            zip -r -q "$zip_file" . && curl --progress-bar --upload-file "$zip_file" "https://transfer.sh/"|tee /dev/null && rm "$zip_file";
            popd;
        else 
            curl --progress-bar --upload-file "$file" "https://transfer.sh/"|tee /dev/null;
        fi;
    else 
        curl --progress-bar --upload-file "$1" "https://transfer.sh/"|tee /dev/null;
    fi;
}

this should be the proper one: please @snowphone check if it works for you and feel free to push the change :)

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants