Merge branch 'master' of builder:git/ufw-tools

This commit is contained in:
Jack L. Frost 2015-11-19 18:33:05 +03:00
commit 81c636d2fc
2 changed files with 33 additions and 4 deletions

View File

@ -27,6 +27,7 @@ A very simple tool to upload files to the zfh.so file hosting
-F # Make a fullscreen shot instead of prompting for a window/area. Implies -s. -F # Make a fullscreen shot instead of prompting for a window/area. Implies -s.
-D <num> # Delay the shot by <num> seconds. -D <num> # Delay the shot by <num> seconds.
-p # Make the file private. Requires $secret to be set in the config. -p # Make the file private. Requires $secret to be set in the config.
-m # Maximum filesize (takes K, M and G suffixes).
Config options (~/.suprc): Config options (~/.suprc):
secret # Your personal token. Get it at https://zfh.so/settings_form secret # Your personal token. Get it at https://zfh.so/settings_form

36
ufw
View File

@ -13,8 +13,9 @@ fi
cfg_url_regex='^[A-Za-z]([A-Za-z0-9+.-]+)?://.+' cfg_url_regex='^[A-Za-z]([A-Za-z0-9+.-]+)?://.+'
cfg_tmp_dir="$XDG_RUNTIME_DIR/sup"; TEMPDIR="$cfg_tmp_dir" cfg_tmp_dir="$XDG_RUNTIME_DIR/sup"; TEMPDIR="$cfg_tmp_dir"
cfg_service_url='https://zfh.so' cfg_service_url='https://8fw.me'
cfg_screenshot_ext='png' cfg_screenshot_ext='png'
cfg_max_filesize='200M'
[[ -f $HOME/.suprc ]] && { source "$HOME/.suprc"; } [[ -f $HOME/.suprc ]] && { source "$HOME/.suprc"; }
@ -42,10 +43,12 @@ sup.usage() {
-D <num> # Delay the shot by <num> seconds. -D <num> # Delay the shot by <num> seconds.
-p # Make the file private. Requires \$secret to be set in the config. -p # Make the file private. Requires \$secret to be set in the config.
-u # Generate a shortlink from URL. -u # Generate a shortlink from URL.
-m # Maximum filesize (takes K, M and G suffixes).
Config options (~/.suprc): Config options (~/.suprc):
secret # Your personal token. Get it at https://zfh.so/settings_form secret # Your personal token. Get it at https://zfh.so/settings_form
cfg_screenshot_ext # Screenshot file type, used by scrot. cfg_screenshot_ext # Screenshot file type, used by scrot.
cfg_max_filesize # Maximum filesize in megabytes.
# Others are self-explanatory: # Others are self-explanatory:
cfg_url_regex cfg_url_regex
cfg_tmp_dir cfg_tmp_dir
@ -63,6 +66,22 @@ sup.get_hash() {
read file_hash _ < <( sha1sum "$1" ) read file_hash _ < <( sha1sum "$1" )
} }
sup.get_max_filesize() {
if [[ $cfg_max_filesize =~ ^[0-9]+[BKMG]?$ ]]; then
max_filesize_base="${cfg_max_filesize//[BKMG]/}"
case "$cfg_max_filesize" in
*K) max_filesize_bytes=$(( max_filesize_base * 1024 ));;
*M) max_filesize_bytes=$(( max_filesize_base * 1024 * 1024 ));;
*G) max_filesize_bytes=$(( max_filesize_base * 1024 * 1024 * 1024 ));;
*) max_filesize_bytes=$max_filesize_base;;
esac
else
sup.err "Wrong cfg_max_filesize: $cfg_max_filesize"
return 1
fi
}
sup.upload() { sup.upload() {
if (( flag_scrot )); then if (( flag_scrot )); then
flag_rm=1 flag_rm=1
@ -79,10 +98,19 @@ sup.upload() {
flag_rm=1 flag_rm=1
file=$(sup.mktemp "$cfg_tmp_dir") file=$(sup.mktemp "$cfg_tmp_dir")
curl -skL "$target" > "$file" || { sup.get_max_filesize || { return 1; }
sup.err "Could not download file."
curl --max-filesize "$max_filesize_bytes" -skL "$target" > "$file"
curl_result=$?
if (( curl_result )); then
case "$curl_result" in
63) sup.err "File exceeds cfg_max_filesize";;
*) sup.err "Could not download file.";;
esac
return 1 return 1
} fi
else else
file="$target" file="$target"