Last active
April 17, 2021 13:51
-
-
Save bekcpear/90bd788a0d921fbb785420f974ecdb41 to your computer and use it in GitHub Desktop.
This is a script that can get a url related to the uploaded image/text from an implementation of vim-cn pastebin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# author: @Bekcpear <[email protected]> | |
# This is a script that can get a url related to the uploaded image/text from an implementation of vim-cn pastebin" | |
# | |
set -e | |
#vim-cn style pastebin urls | |
uptexturl='https://cfp.vim-cn.com/' | |
upimageurl='https://img.vim-cn.com/' | |
function sizeof(){ | |
if [ "x""$1" == "xfile" ]; then | |
eval "length=\$(wc -m $2 | cut -d ' ' -f 1)" | |
elif [ "x""$1" == "xclipb" ]; then | |
if [ "$2"x == "textx" ]; then | |
eval "length=\$(xclip -selection clipboard -t TEXT -o | wc -m)" | |
else | |
eval "length=\$(xclip -selection clipboard -t UTF8_STRING -o | wc -m)" | |
fi | |
else | |
eval "length=\$(echo \"\$2\" | wc -m)" | |
fi | |
if [ $length -lt 23 ]; then | |
echo "Text is too short. abort" > /dev/stderr | |
exit 1 | |
elif [ $length -gt 65536 ]; then | |
echo "Text is too long. abort" > /dev/stderr | |
exit 1 | |
fi | |
} | |
if [ -t 0 ]; then | |
function showHelp(){ | |
echo " Usage: ${1##*/} [<filepath>]" | |
echo " OR" | |
echo " <text output> | ${1##*/}" | |
echo " 1. contents will be uploaded from the clipboard if the argument is omitted" | |
echo " 2. only text/image type contents supported, they are judged automatically" | |
echo | |
echo " -t force to text type" | |
echo " -h show this help" | |
} | |
[ "$1"x == "-hx" ] && showHelp $0 && exit 0 | |
if [[ -n $2 ]]; then | |
if [[ "$1" == "-t" ]]; then | |
path="$2" | |
elif [[ "$2" == "-t" ]]; then | |
path="$1" | |
else | |
echo "Invalid argument(s) $2 (...)" > /dev/stderr && showHelp $0 && exit 1 | |
fi | |
forcetext=1 | |
else | |
path="$1" | |
fi | |
loadcon=0 | |
rawpath="" | |
type="text" | |
loadcmd='' | |
if [ "$1"x == ""x ]; then | |
if [[ "$(xclip -selection clipboard -t TARGETS -o | egrep 'text/(uri-list)|(x-moz-url)')" != "" ]]; then | |
rawpath="$(xclip -selection clipboard -t text/plain -o)" | |
fi | |
if [[ "${rawpath}" =~ "file://".* ]]; then | |
path="${rawpath#file://}" | |
else | |
loadcon=1 | |
fi | |
fi | |
if [ $loadcon -eq 0 ]; then | |
function _isText(){ | |
if [[ -n $forcetext ]]; then | |
return 0 | |
fi | |
local t="$(file --mime-type "$1" | awk '$NF ~ /^text\/.*$/ {printf $NF}')" | |
t+="$(file --mime-type "$1" | awk '$NF ~ /^application\/json$/ {printf $NF}')" | |
[[ $t != '' ]] | |
} | |
function _isImage(){ | |
local t="$(file --mime-type "$1" | awk '$NF ~ /^image\/.*$/ {printf $NF}')" | |
[[ $t != '' ]] | |
} | |
if _isText "${path}"; then | |
loadcmd="cat ${path}" | |
sizeof 'file' "${path}" | |
elif _isImage "${path}"; then | |
loadcmd="cat ${path}" | |
type="image" | |
else | |
echo "Invalid argument or unknown file type: ${path}" > /dev/stderr | |
showHelp $0 | |
exit 1 | |
fi | |
elif [ $loadcon -eq 1 ]; then | |
types=$(xclip -selection clipboard -t TARGETS -o) | |
[ $? -ne 0 ] && echo "no content in the clipboard. abort" > /dev/stderr && exit 1 | |
isutf8=$(xclip -selection clipboard -t TARGETS -o | grep "UTF8_STRING") || \ | |
istext=$(xclip -selection clipboard -t TARGETS -o | grep "TEXT") || true | |
if [[ "${istext}x" == "x" && "${isutf8}x" == "x" ]]; then | |
type="image" | |
case ${types} in | |
*"image/png"* ) | |
loadcmd='xclip -selection clipboard -t image/png -o' | |
break | |
;; | |
*"image/jpg"* ) | |
loadcmd='xclip -selection clipboard -t image/jpg -o' | |
break | |
;; | |
*"image/jpeg"* ) | |
loadcmd='xclip -selection clipboard -t image/jpeg -o' | |
break | |
;; | |
*"image/webp"* ) | |
loadcmd='xclip -selection clipboard -t image/webp -o' | |
break | |
;; | |
*"image/bmp"* ) | |
loadcmd='xclip -selection clipboard -t image/bmp -o' | |
break | |
;; | |
*) | |
;; | |
esac | |
elif [[ "${isutf8}" != "" ]]; then | |
loadcmd='xclip -selection clipboard -t UTF8_STRING -o' | |
sizeof 'clipb' 'utf8' | |
else | |
loadcmd='xclip -selection clipboard -t TEXT -o' | |
sizeof 'clipb' 'text' | |
fi | |
fi | |
if [[ "$1" == "" ]]; then | |
clipinfo="\e[36mClipboard contents:\e[0m\n" | |
if [[ "${type}" == "text" ]]; then | |
eval "examplec=\$($loadcmd)" | |
etc="" | |
if [[ ${#examplec} -gt 90 ]]; then | |
etc=" \e[4m\e[36m… and remaining $((${#examplec} - 89)) characters\e[0m" | |
fi | |
clipinfo="${clipinfo}\e[38;5;254m${examplec:0:89}\e[0m${etc}\n" | |
else | |
if [[ "${path}" != "" ]]; then | |
clipinfo="${clipinfo}file://${path}\n" | |
else | |
clipinfo="${clipinfo}[clipboard raw data] type: ${type}\n" | |
fi | |
fi | |
printf "${clipinfo//%/%%}" | |
echo -ne "\e[1m\e[33mUpload contents from clipboard?[y/N]\e[0m " | |
while read -n 1 -re read_parm; do | |
case ${read_parm} in | |
[yY]) | |
break | |
;; | |
*) | |
if [[ "${read_parm}" == "" ]]; then | |
echo | |
fi | |
echo "bye~" > /dev/stderr | |
exit 1 | |
;; | |
esac | |
done | |
fi | |
if [ $type == 'text' ];then | |
echo "Run: $loadcmd | curl -sF 'vimcn=<-' '${uptexturl}'" | |
eval "url=\$($loadcmd | curl -sF 'vimcn=<-' '${uptexturl}')" | |
elif [ $type == 'image' ];then | |
echo "Run: $loadcmd | curl -sF 'name=@-' '${upimageurl}'" | |
eval "url=\$($loadcmd | curl -sF 'name=@-' '${upimageurl}')" | |
fi | |
else | |
pipelinein="$(</dev/stdin)" | |
sizeof "pipelinein" "${pipelinein}" | |
echo "Run: <text output> | curl -sF 'vimcn=<-' '${uptexturl}'" | |
eval "url=\$(echo \"\${pipelinein}\" | curl -sF 'vimcn=<-' '${uptexturl}')" | |
fi | |
[ $? -ne 0 ] && echo 'Upload err' > /dev/stderr && exit 1 | |
eval "url=\$(echo \"\${url}\" | sed 's#http://#https://#')" | |
echo -n "Re: $url" && [[ "$url" =~ ^http ]] && { echo -n "$url" | xclip -selection 'clipboard' && echo ' <url copied>' || echo ' <url not copied>'; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment