max filesize

This commit is contained in:
Jack L. Frost 2015-11-13 05:08:55 +03:00
parent a4e0a119a8
commit 8befbdf3da

34
sup
View File

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