#!/bin/bash # # "THE BEER-WARE LICENSE" (Revision 42): # Matěj Cepl, wrote this file. As long as you retain # this notice you can do whatever you want with this stuff. If we meet # some day, and you think this stuff is worth it, you can buy me a beer # in return. Matěj Cepl # # Instructions for setting up appropriate folders on # file.rdu.redhat.com see https://mojo.redhat.com/docs/DOC-14590 # See also variable URL_VARIANTS below for necessary configuration. set -eux # import os # import os.path # import subprocess # import sys # import urllib.parse DEBUG=0 debug() { [ DEBUG -eq 1 ] && printf "%s\n" "$@" } error() { printf "%s\n" "$@" exit 1 } COMPRESSED_EXTENSIONS=('.jpg' '.webm' '.ogg' '.gz' '.bz2' '.zip' \ '.xz' '.odp' '.odt' '.ods' '.mp4' '.mp3' \ '.png' '.pdf' '.apk' '.rpm' '.epub' '.mkv' \ '.avi' '.jar' '.opus' '.ogv' '.flac' '.msi' \ '.m4a') # THE VARIABLE URL_VARIANTS IS MEANT TO BE EDITED. Change various # attributes to correspond to your situation. # If new section is added (as here I have also 'tmp' for fedorapeople.org) # and the script has a symlink named as the section (so I have tmp as # a symlink to this script) it saves the file to the location defined in # such section when called under the other name. # So, `tmp filename` saves file filename to fedorapeople for me. declare -A URLS_barstool URLS_barstool['base_url']='mcepl@shell.eng.rdu.redhat.com:public_html/' URLS_barstool['target_url']='http://file.rdu.redhat.com/~mcepl/' URLS_barstool['shorten_api']='https://url.corp.redhat.com/new?' declare -A URLS_wotan URLS_wotan['base_url']='wotan:Export/' URLS_wotan['target_url']='https://w3.suse.de/~mcepl/' URLS_wotan['shorten_api']='' declare -A URLS_tmp URLS_tmp['base_url']='fedorapeople.org:public_html/tmp/' URLS_tmp['target_url']='https://mcepl.fedorapeople.org/tmp/' # URLS_tmp['shorten_api']='http://is.gd/create.php?format=simple&url=' URLS_tmp['shorten_api']='https://da.gd/s?url=' declare -A URLS # b=( "${a[@]}" ) URLS['barstool']=( "${URLS_barstool[@]}" ) URLS['wotan']=( "${URLS_wotan[@]" ) URLS['tmp']=( "${URLS_tmp[@]" ) # Requires GNU sed CMDNAME="$(echo "$(basename $0)"|sed 's/./\L&/g')" declare -A config echo ${@:1} for elem in "${URLS[$CMDNAME]}" do # echo "key : ${elem}" -- "value: ${config[${elem}]}" config[${elem}]=${URLS[$CMDNAME][${elem}]} done echo "${config[@]}" echo "${!config[@]}" exit 0 [ $# -lt 1 ] && exit 1 for processed_file in ${@:1} sys.argv[1:] ; do st=os.stat(processed_file).st_size file_ext=os.path.splitext(processed_file)[1] debug "size of $processed_file is ${st}" debug "extension is ${file_ext}" if st > 1000000 and ${file,,} not in COMPRESSED_EXTENSIONS ; then debug "file ${processed_file} should be compressed (${st} b)" gzip --keep --best ${processed_file} || error 'Compressing the file failed!' fname="${processed_file}.gz" COMPRESSED=1 else: debug "file ${processed_file} should not be compressed (${st} b)" fname="${processed_file}" COMPRESSED=0 fi chmod 644 fname # To make scp happy modified_fname="$(echo ${fname}|tr ':' '_')" mv "${fname}" "${modified_fname}" debug "Unshorten fname=${modified_fname}" scp "${modified_fname}" "${config['base_url']}" target_URL="${config['target_url']}$(basename ${modified_fname})" # Make target_URL into a safe URL safe_target_URL=urllib.parse.quote(target_URL).replace('%3A', ':', 1) echo "${safe_target_URL}" # curl -s 'http://is.gd/create.php?format=simple&url=www.example.com' # http://is.gd/MOgh5q if 'shorten_api' in config ; then shortened_URL="$(curl -s "${config['shorten_api']}""${safe_target_URL}")" echo "${shortened_URL}" fi # The script should have no side-effects if [ "${COMPRESSED}" -eq 1 ] ; then rm "${modified_fname}" else: mv "${modified_fname}" "${fname}" fi done