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
Example Zsh/Bash function for transfer.sh homepage #475
Comments
nice @keks24 , thanks for sharing I will add to if you are able to write a cross-shell (bash/zsh at least) version of your function, including echoing delete command (that's really great!) we could promote it to frontend. please let me know what do you think |
Yeah, delete command is not shown when using zsh |
I just tested this on my end and it works for What output do you get, if you upload a small file via $ echo "some_content" > "deleteme"
$ curl --request PUT --progress-bar --dump-header - --upload-file "deleteme" "https://transfer.sh/deleteme"
Maybe the key These are the package versions I am using and an upload test, which I did in $ zsh --version
zsh 5.8.1 (x86_64-pc-linux-gnu) $ curl --version
curl 7.79.1 (x86_64-pc-linux-gnu) libcurl/7.79.1 OpenSSL/1.1.1n zlib/1.2.11 zstd/1.5.2 nghttp2/1.45.1
Release-Date: 2021-09-22
Protocols: dict file ftp ftps http https imap imaps mqtt pop3 pop3s rtsp smtp smtps tftp
Features: HTTP2 HTTPS-proxy IPv6 Largefile libz NTLM SSL TLS-SRP UnixSockets zstd $ ls -l $(command -v awk)
lrwxrwxrwx 1 root root 4 Nov 13 2018 /usr/bin/awk -> gawk
$ gawk --version
GNU Awk 5.1.1, API: 3.1
Copyright (C) 1989, 1991-2021 Free Software Foundation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/. $ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. I refactored the function to intercept errors, but the origin function above works as well for [...]
# Put your fun stuff here.
## upload files to "transfer.sh"
transfer()
{
local file="${1}"
if [[ ! -f "${file}" ]]
then
echo -e "\e[01;31mCould not find file: '${file}'.\e[0m" >&2
return 1
else
local filename="${file##*/}"
# show delete link from the response header after upload. the command "sed" is necessary to clean up the output, "gsub()" in "awk" does not work.
curl --request PUT --progress-bar --dump-header - --upload-file "${file}" "https://transfer.sh/${filename}" \
| sed "s/#//g" \
| awk '/x-url-delete/ { print "Delete command: curl --request DELETE " $2 } END{ print "Download link: " $1 }'
fi
} $ bash
$ cd $(mktemp --directory)
$ dd if=/dev/zero of=image.img bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.00520936 s, 2.0 GB/s
$ ls -lh
total 10M
-rw-r--r-- 1 ramon ramon 10M Apr 3 14:30 image.img
$ transfer image.img
######################################################################################################################################################################################################################################## 100.0%
Delete command: curl --request DELETE https://transfer.sh/QW3WDd/image.img/euCXr1ibfNK9
Download link: https://transfer.sh/QW3WDd/image.img
$ curl --request DELETE https://transfer.sh/QW3WDd/image.img/euCXr1ibfNK9 |
I found the issue! It seems, that Since there are different versions of This function should work now: ## upload files to "transfer.sh"
transfer()
{
local file="${1}"
if [[ ! -f "${file}" ]]
then
echo -e "\e[01;31mCould not find file: '${file}'.\e[0m" >&2
return 1
else
local filename="${file##*/}"
# show delete link from the response header after upload. the command "sed" is necessary to clean up the output, "gsub()" in "awk" does not work.
curl --request PUT --progress-bar --dump-header - --upload-file "${file}" "https://transfer.sh/${filename}" \
| sed "s/#//g" \
| awk 'tolower($0) ~ /x-url-delete/ { print "Delete command: curl --request DELETE " $2 } END{ print "Download link: " $1 }'
fi
} |
not sure about that |
I think, that has something to do with Does this one with $ curl --request PUT --silent --show-error --dump-header - --upload-file "image.img" "https://transfer.sh/image.img" | awk 'tolower($0) ~ /x-url-delete/ { print "Delete command: curl --request DELETE " $2 } END{ print "Download link: " $1 }' Leaving out the parameter $ curl --request PUT --dump-header - --upload-file "image.img" "https://transfer.sh/image.img" | awk 'tolower($0) ~ /x-url-delete/ { print "Delete command: curl --request DELETE " $2 } END{ print "Download link: " $1 }'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 10.0M 100 36 100 10.0M 6 1726k 0:00:06 0:00:05 0Delete command: curl --request DELETE https://transfer.sh/jwnvkk/image.img/mJTdLmALDh7N
100 10.0M 100 36 100 10.0M 6 1726k 0:00:06 0:00:05 0:00:01 887k
Download link: https://transfer.sh/jwnvkk/image.img |
This comment was marked as outdated.
This comment was marked as outdated.
I could not help myself, but to improve the function once again. It is slowly growing, where I almost got to the point to create a separate repository for it, but I will keep it like this for now. I consider this the final version now. It can now upload multiple files, shows the size of each file in $ transfer --help
transfer - Upload arbitrary files to "transfer.sh".
Usage: transfer [options] [<file>]...
OPTIONS:
-h, --help
show this message
EXAMPLES:
Upload a single file from the current working directory:
transfer "image.img"
Upload multiple files from the current working directory:
transfer "image.img" "image2.img"
Upload a file from a different directory:
transfer "/tmp/some_file"
Upload all files from the current working directory. Be aware of the webserver's rate limiting!:
transfer *
Upload a single file from the current working directory and filter out the delete command:
transfer "image.img" | awk --field-separator="Delete command:" '/Delete command:/ { print $2 }'
Show help text from "transfer.sh":
curl --request GET "https://transfer.sh" ## upload files to "transfer.sh"
transfer()
{
local upload_files
local curl_output
local awk_output
local filename
local file
declare -a file_array
file_array=("${@}")
if [[ "${file_array[@]}" == "" || "${1}" == "--help" || "${1}" == "-h" ]]
then
echo "${0} - Upload arbitrary files to \"transfer.sh\"."
echo ""
echo "Usage: ${0} [options] [<file>]..."
echo ""
echo "OPTIONS:"
echo " -h, --help"
echo " show this message"
echo ""
echo "EXAMPLES:"
echo " Upload a single file from the current working directory:"
echo " ${0} \"image.img\""
echo ""
echo " Upload multiple files from the current working directory:"
echo " ${0} \"image.img\" \"image2.img\""
echo ""
echo " Upload a file from a different directory:"
echo " ${0} \"/tmp/some_file\""
echo ""
echo " Upload all files from the current working directory. Be aware of the webserver's rate limiting!:"
echo " ${0} *"
echo ""
echo " Upload a single file from the current working directory and filter out the delete token and download link:"
echo " ${0} \"image.img\" | awk --field-separator=\": \" '/Delete token:/ { print \$2 } /Download link:/ { print \$2 }'"
echo ""
echo " Show help text from \"transfer.sh\":"
echo " curl --request GET \"https://transfer.sh\""
return 0
else
for file in "${file_array[@]}"
do
if [[ ! -f "${file}" ]]
then
echo -e "\e[01;31m'${file}' could not be found or is not a file.\e[0m" >&2
return 1
fi
done
fi
unset file
du --total --block-size="K" --dereference "${file_array[@]}" >&2
# be compatible with "bash"
if [[ "${ZSH_NAME}" == "zsh" ]]
then
read $'upload_files?\e[01;31mDo you really want to upload the above files ('"${#file_array[@]}"$') to "transfer.sh"? (Y/n): \e[0m'
elif [[ "${BASH}" == *"bash"* ]]
then
read -p $'\e[01;31mDo you really want to upload the above files ('"${#file_array[@]}"$') to "transfer.sh"? (Y/n): \e[0m' upload_files
fi
case "${upload_files:-y}" in
"y"|"Y")
# for the sake of the progress bar, execute "curl" for each file.
# the parameters "--include" and "--form" will suppress the progress bar.
for file in "${file_array[@]}"
do
filename="${file##*/}"
# show delete link and filter out the delete token from the response header after upload.
# it is important to save "curl's" "stdout" via a subshell to a variable or redirect it to another command,
# which just redirects to "stdout" in order to have a sane output afterwards.
# the progress bar is redirected to "stderr" and is only displayed,
# if "stdout" is redirected to something; e.g. ">/dev/null", "tee /dev/null" or "| <some_command>".
# the response header is redirected to "stdout", so redirecting "stdout" to "/dev/null" does not make any sense.
# redirecting "curl's" "stderr" to "stdout" ("2>&1") will suppress the progress bar.
curl_output=$(curl --request PUT --progress-bar --dump-header - --upload-file "${file}" "https://transfer.sh/${filename}")
awk_output=$(awk 'tolower($1) ~ /x-url-delete/ \
{ gsub("\r", "", $0); print "Delete command: curl --request DELETE " "\""$2"\""; \
gsub(".*/", "", $2); print "Delete token: " $2 } \
END{ print "Download link: " $0 }' <<< "${curl_output}")
# return the results via "stdout", "awk" does not do this for some reason.
echo -e "${awk_output}\n"
# avoid rate limiting as much as possible; nginx: too many requests.
if (( ${#file_array[@]} > 4 ))
then
sleep 5
fi
done
;;
"n"|"N")
return 1
;;
*)
echo -e "\e[01;31mWrong input: '${upload_files}'.\e[0m" >&2
return 1
esac
} $ ls -lh
total 20M
-rw-r--r-- 1 ramon ramon 10M Apr 4 21:08 image.img
-rw-r--r-- 1 ramon ramon 10M Apr 4 21:08 image2.img
$ transfer image*
10240K image2.img
10240K image.img
20480K total
Do you really want to upload the above files (2) to "transfer.sh"? (Y/n):
######################################################################################################################################################################################################################################## 100.0%
Delete command: curl --request DELETE "https://transfer.sh/wJw9pz/image2.img/mSctGx7pYCId"
Delete token: mSctGx7pYCId
Download link: https://transfer.sh/wJw9pz/image2.img
######################################################################################################################################################################################################################################## 100.0%
Delete command: curl --request DELETE "https://transfer.sh/ljJc5I/image.img/nw7qaoiKUwCU"
Delete token: nw7qaoiKUwCU
Download link: https://transfer.sh/ljJc5I/image.img
$ transfer "image.img" | awk --field-separator=": " '/Delete token:/ { print $2 } /Download link:/ { print $2 }'
10240K image.img
10240K total
Do you really want to upload the above files (1) to "transfer.sh"? (Y/n):
######################################################################################################################################################################################################################################## 100.0%
tauN5dE3fWJe
https://transfer.sh/MYkuqn/image.img |
@keks24 thank you very much for you effort! I will try to add this to the frontend in the weekend |
My pleasure! 😃 If it is too big for the Some details
$ some_command |
@keks24 , thank you very much if you have time and you can open a PR to directly update the I'm a little busy this days and I'm not sure when I'll be able to do on my own 🙏 |
I already did that in #478, which also got merged by @stefanbenten. :) |
lol, thanks :) |
Hello,
I just want to share this. If you consider my function as an worthy example for the homepage, go for it!:
Example outputs:
-Ramon
The text was updated successfully, but these errors were encountered: