sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

imgur (20777B)


      1 #!/usr/bin/env bash
      2 
      3 ##########################################################################
      4 # The MIT License
      5 #
      6 # Copyright (c) jomo
      7 #
      8 # Permission is hereby granted, free of charge,
      9 # to any person obtaining a copy of this software and
     10 # associated documentation files (the "Software"), to
     11 # deal in the Software without restriction, including
     12 # without limitation the rights to use, copy, modify,
     13 # merge, publish, distribute, sublicense, and/or sell
     14 # copies of the Software, and to permit persons to whom
     15 # the Software is furnished to do so,
     16 # subject to the following conditions:
     17 #
     18 # The above copyright notice and this permission notice
     19 # shall be included in all copies or substantial portions of the Software.
     20 #
     21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     22 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
     23 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     24 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
     25 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     26 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     27 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     28 ##########################################################################
     29 
     30 # https://github.com/jomo/imgur-screenshot
     31 # https://help.imgur.com/hc/en-us/articles/209592766-Tools-for-Imgur
     32 #
     33 # Slightly modified for `nnn` integration
     34 #
     35 # Shell: Bash
     36 # Description: Upload an image file to imgur
     37 
     38 if [ "${1}" = "--debug" ]; then
     39   echo "########################################"
     40   echo "Enabling debug mode"
     41   echo "Please remove credentials before pasting"
     42   echo "########################################"
     43   echo ""
     44   uname -a
     45   for arg in ${0} "${@}"; do
     46     echo -n "'${arg}' "
     47   done
     48   echo -e "\n"
     49   shift
     50   set -x
     51 fi
     52 
     53 current_version="v1.7.4"
     54 
     55 function is_mac() {
     56   uname | grep -q "Darwin"
     57 }
     58 
     59 ### IMGUR-SCREENSHOT DEFAULT CONFIG ####
     60 
     61 # You can override the config in ~/.config/imgur-screenshot/settings.conf
     62 
     63 imgur_anon_id="ea6c0ef2987808e"
     64 imgur_icon_path="${HOME}/Pictures/imgur.png"
     65 
     66 imgur_acct_key=""
     67 imgur_secret=""
     68 login="false"
     69 album_title=""
     70 album_id=""
     71 credentials_file="${HOME}/.config/imgur-screenshot/credentials.conf"
     72 
     73 file_name_format="imgur-%Y_%m_%d-%H:%M:%S.png" # when using scrot, must end with .png!
     74 file_dir="${HOME}/Pictures"
     75 
     76 upload_connect_timeout="5"
     77 upload_timeout="120"
     78 upload_retries="1"
     79 
     80 # shellcheck disable=SC2034
     81 if is_mac; then
     82   screenshot_select_command="screencapture -i %img"
     83   screenshot_window_command="screencapture -iWa %img"
     84   screenshot_full_command="screencapture %img"
     85   open_command="open %url"
     86 else
     87   screenshot_select_command="scrot -s %img"
     88   screenshot_window_command="scrot %img"
     89   screenshot_full_command="scrot %img"
     90   open_command="xdg-open %url"
     91 fi
     92 open="true"
     93 
     94 mode="select"
     95 edit_command="gimp %img"
     96 edit="false"
     97 exit_on_album_creation_fail="true"
     98 
     99 log_file="${HOME}/.imgur-screenshot.log"
    100 
    101 auto_delete=""
    102 copy_url="true"
    103 keep_file="true"
    104 check_update="true"
    105 
    106 # NOTICE: if you make changes here, also edit the docs at
    107 # https://github.com/jomo/imgur-screenshot/wiki/Config
    108 
    109 # You can override the config in ~/.config/imgur-screenshot/settings.conf
    110 
    111 ############## END CONFIG ##############
    112 
    113 settings_path="${HOME}/.config/imgur-screenshot/settings.conf"
    114 if [ -f "${settings_path}" ]; then
    115   source "${settings_path}"
    116 fi
    117 
    118 # dependency check
    119 if [ "${1}" = "--check" ]; then
    120   (type grep &>/dev/null && echo "OK: found grep") || echo "ERROR: grep not found"
    121   if is_mac; then
    122     if type growlnotify &>/dev/null; then
    123       echo "OK: found growlnotify"
    124     elif type terminal-notifier &>/dev/null; then
    125       echo "OK: found terminal-notifier"
    126     else
    127       echo "ERROR: growlnotify nor terminal-notifier found"
    128     fi
    129     (type screencapture &>/dev/null && echo "OK: found screencapture") || echo "ERROR: screencapture not found"
    130     (type pbcopy &>/dev/null && echo "OK: found pbcopy") || echo "ERROR: pbcopy not found"
    131   else
    132     (type notify-send &>/dev/null && echo "OK: found notify-send") || echo "ERROR: notify-send (from libnotify-bin) not found"
    133     (type scrot &>/dev/null && echo "OK: found scrot") || echo "ERROR: scrot not found"
    134     (type xclip &>/dev/null && echo "OK: found xclip") || echo "ERROR: xclip not found"
    135   fi
    136   (type curl &>/dev/null && echo "OK: found curl") || echo "ERROR: curl not found"
    137   exit 0
    138 fi
    139 
    140 
    141 # notify <'ok'|'error'> <title> <text>
    142 function notify() {
    143   if is_mac; then
    144     if type growlnotify &>/dev/null; then
    145       growlnotify  --icon "${imgur_icon_path}" --iconpath "${imgur_icon_path}" --title "${2}" --message "${3}"
    146     else
    147       terminal-notifier -appIcon "${imgur_icon_path}" -contentImage "${imgur_icon_path}" -title "imgur: ${2}" -message "${3}"
    148     fi
    149   else
    150     if [ "${1}" = "error" ]; then
    151       notify-send -a ImgurScreenshot -u critical -c "im.error" -i "${imgur_icon_path}" -t 500 "imgur: ${2}" "${3}"
    152     else
    153       notify-send -a ImgurScreenshot -u low -c "transfer.complete" -i "${imgur_icon_path}" -t 500 "imgur: ${2}" "${3}"
    154     fi
    155   fi
    156 }
    157 
    158 function take_screenshot() {
    159   echo "Please select area"
    160   is_mac || sleep 0.1 # https://bbs.archlinux.org/viewtopic.php?pid=1246173#p1246173
    161 
    162   cmd="screenshot_${mode}_command"
    163   cmd=${!cmd//\%img/${1}}
    164 
    165   if ! shot_err="$(${cmd} &>/dev/null)"; then  #takes a screenshot with selection
    166     echo "Failed to take screenshot '${1}': '${shot_err}'. For more information visit https://github.com/jomo/imgur-screenshot/wiki/Troubleshooting" | tee -a "${log_file}"
    167     notify error "Something went wrong :(" "Information has been logged"
    168     exit 1
    169   fi
    170 }
    171 
    172 function check_for_update() {
    173   # exit non-zero on HTTP error, output only the body (no stats) but output errors, follow redirects, output everything to stdout
    174   remote_version="$(curl --compressed -fsSL --stderr - "https://api.github.com/repos/jomo/imgur-screenshot/releases" | grep -Em 1 --color 'tag_name":\s*".*"' | cut -d '"' -f 4)"
    175   if [ -n "$remote_version" ]; then
    176     if [ ! "${current_version}" = "${remote_version}" ] && [ -n "${current_version}" ] && [ -n "${remote_version}" ]; then
    177       echo "Update found!"
    178       echo "Version ${remote_version} is available (You have ${current_version})"
    179       notify ok "Update found" "Version ${remote_version} is available (You have ${current_version}). https://github.com/jomo/imgur-screenshot"
    180       echo "Check https://github.com/jomo/imgur-screenshot/releases/${remote_version} for more info."
    181     elif [ -z "${current_version}" ] || [ -z "${remote_version}" ]; then
    182       echo "Invalid empty version string"
    183       echo "Current (local) version: '${current_version}'"
    184       echo "Latest (remote) version: '${remote_version}'"
    185     else
    186       echo "Version ${current_version} is up to date."
    187     fi
    188   else
    189     echo "Failed to check for latest version: ${remote_version}"
    190   fi
    191 }
    192 
    193 function check_oauth2_client_secrets() {
    194   if [ -z "${imgur_acct_key}" ] || [ -z "${imgur_secret}" ]; then
    195     echo "In order to upload to your account, register a new application at:"
    196     echo "https://api.imgur.com/oauth2/addclient"
    197     echo "Select 'OAuth 2 authorization without a callback URL'"
    198     echo "Then, set the imgur_acct_key (Client ID) and imgur_secret in your config."
    199     exit 1
    200   fi
    201 }
    202 
    203 function load_access_token() {
    204   token_expire_time=0
    205   # check for saved access_token and its expiration date
    206   if [ -f "${credentials_file}" ]; then
    207     source "${credentials_file}"
    208   fi
    209   current_time="$(date +%s)"
    210   preemptive_refresh_time="$((10*60))"
    211   expired="$((current_time > (token_expire_time - preemptive_refresh_time)))"
    212   if [ -n "${refresh_token}" ]; then
    213     # token already set
    214     if [ "${expired}" -eq "0" ]; then
    215       # token expired
    216       refresh_access_token "${credentials_file}"
    217     fi
    218   else
    219     acquire_access_token "${credentials_file}"
    220   fi
    221 }
    222 
    223 function acquire_access_token() {
    224   check_oauth2_client_secrets
    225   # prompt for a PIN
    226   authorize_url="https://api.imgur.com/oauth2/authorize?client_id=${imgur_acct_key}&response_type=pin"
    227   echo "Go to"
    228   echo "${authorize_url}"
    229   echo "and grant access to this application."
    230   read -rp "Enter the PIN: " imgur_pin
    231 
    232   if [ -z "${imgur_pin}" ]; then
    233     echo "PIN not entered, exiting"
    234     exit 1
    235   fi
    236 
    237   # exchange the PIN for access token and refresh token
    238   response="$(curl --compressed -fsSL --stderr - \
    239     -F "client_id=${imgur_acct_key}" \
    240     -F "client_secret=${imgur_secret}" \
    241     -F "grant_type=pin" \
    242     -F "pin=${imgur_pin}" \
    243     https://api.imgur.com/oauth2/token)"
    244   save_access_token "${response}" "${1}"
    245 }
    246 
    247 function refresh_access_token() {
    248   check_oauth2_client_secrets
    249   token_url="https://api.imgur.com/oauth2/token"
    250   # exchange the refresh token for access_token and refresh_token
    251   if ! response="$(curl --compressed -fsSL --stderr - \
    252                     -F "client_id=${imgur_acct_key}" \
    253                     -F "client_secret=${imgur_secret}" \
    254                     -F "grant_type=refresh_token" \
    255                     -F "refresh_token=${refresh_token}" \
    256                     "${token_url}"
    257                     )"; then
    258     # curl failed
    259     handle_upload_error "${response}" "${token_url}"
    260     exit 1
    261   fi
    262   save_access_token "${response}" "${1}"
    263 }
    264 
    265 function save_access_token() {
    266   if ! grep -q "access_token" <<<"${1}"; then
    267     # server did not send access_token
    268     echo "Error: Something is wrong with your credentials:"
    269     echo "${1}"
    270     exit 1
    271   fi
    272 
    273   access_token="$(grep -Eo 'access_token":".*"' <<<"${1}" | cut -d '"' -f 3)"
    274   refresh_token="$(grep -Eo 'refresh_token":".*"' <<<"${1}" | cut -d '"' -f 3)"
    275   expires_in="$(grep -Eo 'expires_in":[0-9]*' <<<"${1}" | cut -d ':' -f 2)"
    276   token_expire_time="$(( $(date +%s) + expires_in ))"
    277 
    278   # create dir if not exist
    279   mkdir -p "$(dirname "${2}")" 2>/dev/null
    280   touch "${2}" && chmod 600 "${2}"
    281   cat <<EOF > "${2}"
    282 access_token="${access_token}"
    283 refresh_token="${refresh_token}"
    284 token_expire_time="${token_expire_time}"
    285 EOF
    286 }
    287 
    288 function fetch_account_info() {
    289   response="$(curl --compressed --connect-timeout "${upload_connect_timeout}" -m "${upload_timeout}" --retry "${upload_retries}" -fsSL --stderr - -H "Authorization: Bearer ${access_token}" https://api.imgur.com/3/account/me)"
    290   if grep -Eq '"success":\s*true' <<<"${response}"; then
    291     username="$(grep -Eo '"url":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    292     echo "Logged in as ${username}."
    293     echo "https://${username}.imgur.com"
    294   else
    295     echo "Failed to fetch info: ${response}"
    296   fi
    297 }
    298 
    299 function delete_image() {
    300   response="$(curl --compressed -X DELETE  -fsSL --stderr - -H "Authorization: Client-ID ${1}" "https://api.imgur.com/3/image/${2}")"
    301   if grep -Eq '"success":\s*true' <<<"${response}"; then
    302     echo "Image successfully deleted (delete hash: ${2})." >> "${3}"
    303   else
    304     echo "The Image could not be deleted: ${response}." >> "${3}"
    305   fi
    306 }
    307 
    308 function upload_authenticated_image() {
    309   echo "Uploading '${1}'..."
    310   title="$(echo "${1}" | rev | cut -d "/" -f 1 | cut -d "." -f 2- | rev)"
    311   if [ -n "${album_id}" ]; then
    312     response="$(curl --compressed --connect-timeout "${upload_connect_timeout}" -m "${upload_timeout}" --retry "${upload_retries}" -fsSL --stderr - -F "title=${title}" -F "image=@\"${1}\"" -F "album=${album_id}" -H "Authorization: Bearer ${access_token}" https://api.imgur.com/3/image)"
    313   else
    314     response="$(curl --compressed --connect-timeout "${upload_connect_timeout}" -m "${upload_timeout}" --retry "${upload_retries}" -fsSL --stderr - -F "title=${title}" -F "image=@\"${1}\"" -H "Authorization: Bearer ${access_token}" https://api.imgur.com/3/image)"
    315   fi
    316 
    317   # JSON parser premium edition (not really)
    318   if grep -Eq '"success":\s*true' <<<"${response}"; then
    319     img_id="$(grep -Eo '"id":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    320     img_ext="$(grep -Eo '"link":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4 | rev | cut -d "." -f 1 | rev)" # "link" itself has ugly '\/' escaping and no https!
    321     del_id="$(grep -Eo '"deletehash":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    322 
    323     if [ -n "${auto_delete}" ]; then
    324       export -f delete_image
    325       echo "Deleting image in ${auto_delete} seconds."
    326       nohup /bin/bash -c "sleep ${auto_delete} && delete_image ${imgur_anon_id} ${del_id} ${log_file}" &
    327     fi
    328 
    329     handle_upload_success "https://i.imgur.com/${img_id}.${img_ext}" "https://imgur.com/delete/${del_id}" "${1}"
    330   else # upload failed
    331     err_msg="$(grep -Eo '"error":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    332     test -z "${err_msg}" && err_msg="${response}"
    333     handle_upload_error "${err_msg}" "${1}"
    334   fi
    335 }
    336 
    337 function upload_anonymous_image() {
    338   echo "Uploading '${1}'..."
    339   title="$(echo "${1}" | rev | cut -d "/" -f 1 | cut -d "." -f 2- | rev)"
    340   if [ -n "${album_id}" ]; then
    341     response="$(curl --compressed --connect-timeout "${upload_connect_timeout}" -m "${upload_timeout}" --retry "${upload_retries}" -fsSL --stderr - -H "Authorization: Client-ID ${imgur_anon_id}" -F "title=${title}" -F "image=@\"${1}\"" -F "album=${album_id}" https://api.imgur.com/3/image)"
    342   else
    343     response="$(curl --compressed --connect-timeout "${upload_connect_timeout}" -m "${upload_timeout}" --retry "${upload_retries}" -fsSL --stderr - -H "Authorization: Client-ID ${imgur_anon_id}" -F "title=${title}" -F "image=@\"${1}\"" https://api.imgur.com/3/image)"
    344   fi
    345   # JSON parser premium edition (not really)
    346   if grep -Eq '"success":\s*true' <<<"${response}"; then
    347     img_id="$(grep -Eo '"id":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    348     img_ext="$(grep -Eo '"link":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4 | rev | cut -d "." -f 1 | rev)" # "link" itself has ugly '\/' escaping and no https!
    349     del_id="$(grep -Eo '"deletehash":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    350 
    351     if [ -n "${auto_delete}" ]; then
    352       export -f delete_image
    353       echo "Deleting image in ${auto_delete} seconds."
    354       nohup /bin/bash -c "sleep ${auto_delete} && delete_image ${imgur_anon_id} ${del_id} ${log_file}" &
    355     fi
    356 
    357     handle_upload_success "https://i.imgur.com/${img_id}.${img_ext}" "https://imgur.com/delete/${del_id}" "${1}"
    358   else # upload failed
    359     err_msg="$(grep -Eo '"error":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    360     test -z "${err_msg}" && err_msg="${response}"
    361     handle_upload_error "${err_msg}" "${1}"
    362   fi
    363 }
    364 
    365 function handle_upload_success() {
    366   echo ""
    367   echo "image  link: ${1}"
    368   echo "delete link: ${2}"
    369 
    370   if [ "${copy_url}" = "true" ] && [ -z "${album_title}" ]; then
    371     if is_mac; then
    372       echo -n "${1}" | pbcopy
    373     else
    374       echo -n "${1}" | xclip -selection clipboard
    375     fi
    376     echo "URL copied to clipboard"
    377   fi
    378 
    379   # print to log file: image link, image location, delete link
    380   echo -e "${1}\t${3}\t${2}" >> "${log_file}"
    381 
    382   notify ok "Upload done!" "${1}"
    383 
    384 #  if [ ! -z "${open_command}" ] && [ "${open}" = "true" ]; then
    385 #    open_cmd=${open_command//\%url/${1}}
    386 #    open_cmd=${open_cmd//\%img/${2}}
    387 #    echo "Opening '${open_cmd}'"
    388 #    eval "${open_cmd}"
    389 #  fi
    390 }
    391 
    392 function handle_upload_error() {
    393   error="Upload failed: \"${1}\""
    394   echo "${error}"
    395   echo -e "Error\t${2}\t${error}" >> "${log_file}"
    396   notify error "Upload failed :(" "${1}"
    397 }
    398 
    399 function handle_album_creation_success() {
    400   echo ""
    401   echo "Album  link: ${1}"
    402   echo "Delete hash: ${2}"
    403   echo ""
    404 
    405   notify ok "Album created!" "${1}"
    406 
    407   if [ "${copy_url}" = "true" ]; then
    408     if is_mac; then
    409       echo -n "${1}" | pbcopy
    410     else
    411       echo -n "${1}" | xclip -selection clipboard
    412     fi
    413     echo "URL copied to clipboard"
    414   fi
    415 
    416   # print to log file: album link, album title, delete hash
    417   echo -e "${1}\t\"${3}\"\t${2}" >> "${log_file}"
    418 }
    419 
    420 function handle_album_creation_error() {
    421   error="Album creation failed: \"${1}\""
    422   echo -e "Error\t${2}\t${error}" >> "${log_file}"
    423   notify error "Album creation failed :(" "${1}"
    424   if [ ${exit_on_album_creation_fail} ]; then
    425     exit 1
    426   fi
    427 }
    428 
    429 while [ ${#} != 0 ]; do
    430   case "${1}" in
    431   -h | --help)
    432     echo "usage: ${0} [--debug] [-c | --check | -v | -h | -u]"
    433     echo "       ${0} [--debug] [option]... [file]..."
    434     echo ""
    435     echo "      --debug                  Enable debugging, must be first option"
    436     echo "  -h, --help                   Show this help, exit"
    437     echo "  -v, --version                Show current version, exit"
    438     echo "      --check                  Check if all dependencies are installed, exit"
    439     echo "  -c, --connect                Show connected imgur account, exit"
    440     echo "  -o, --open <true|false>      Override 'open' config"
    441     echo "  -e, --edit <true|false>      Override 'edit' config"
    442     echo "  -i, --edit-command <command> Override 'edit_command' config (include '%img'), sets --edit 'true'"
    443     echo "  -l, --login <true|false>     Override 'login' config"
    444     echo "  -a, --album <album_title>    Create new album and upload there"
    445     echo "  -A, --album-id <album_id>    Override 'album_id' config"
    446     echo "  -k, --keep-file <true|false> Override 'keep_file' config"
    447     echo "  -d, --auto-delete <s>        Automatically delete image after <s> seconds"
    448     echo "  -u, --update                 Check for updates, exit"
    449     echo "  file                         Upload file instead of taking a screenshot"
    450     exit 0;;
    451   -v | --version)
    452     echo "${current_version}"
    453     exit 0;;
    454   -s | --select)
    455     mode="select"
    456     shift;;
    457   -w | --window)
    458     mode="window"
    459     shift;;
    460   -f | --full)
    461     mode="full"
    462     shift;;
    463   -o | --open)
    464     # shellcheck disable=SC2034
    465     open="${2}"
    466     shift 2;;
    467   -e | --edit)
    468     edit="${2}"
    469     shift 2;;
    470   -i | --edit-command)
    471     edit_command="${2}"
    472     edit="true"
    473     shift 2;;
    474   -l | --login)
    475     login="${2}"
    476     shift 2;;
    477   -c | --connect)
    478     load_access_token
    479     fetch_account_info
    480     exit 0;;
    481   -a | --album)
    482     album_title="${2}"
    483     shift 2;;
    484   -A | --album-id)
    485     album_id="${2}"
    486     shift 2;;
    487   -k | --keep-file)
    488     keep_file="${2}"
    489     shift 2;;
    490   -d | --auto-delete)
    491     auto_delete="${2}"
    492     shift 2;;
    493   -u | --update)
    494     check_for_update
    495     exit 0;;
    496   *)
    497     upload_files=("${@}")
    498     break;;
    499   esac
    500 done
    501 
    502 if [ "${login}" = "true" ]; then
    503   # load before changing directory
    504   load_access_token
    505 fi
    506 
    507 
    508 if [ -n "${album_title}" ]; then
    509   if [ "${login}" = "true" ]; then
    510     response="$(curl -fsSL --stderr - \
    511       -F "title=${album_title}" \
    512       -H "Authorization: Bearer ${access_token}" \
    513       https://api.imgur.com/3/album)"
    514   else
    515     response="$(curl -fsSL --stderr - \
    516       -F "title=${album_title}" \
    517       -H "Authorization: Client-ID ${imgur_anon_id}" \
    518       https://api.imgur.com/3/album)"
    519   fi
    520   if grep -Eq '"success":\s*true' <<<"${response}"; then # Album creation successful
    521     echo "Album '${album_title}' successfully created"
    522     album_id="$(grep -Eo '"id":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    523     del_id="$(grep -Eo '"deletehash":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    524     handle_album_creation_success "https://imgur.com/a/${album_id}" "${del_id}" "${album_title}"
    525 
    526     if [ "${login}" = "false" ]; then
    527       album_id="${del_id}"
    528     fi
    529   else # Album creation failed
    530     err_msg="$(grep -Eo '"error":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
    531     test -z "${err_msg}" && err_msg="${response}"
    532     handle_album_creation_error "${err_msg}" "${album_title}"
    533   fi
    534 fi
    535 
    536 if [ -z "${upload_files[*]}" ]; then
    537   upload_files[0]=""
    538 fi
    539 
    540 for upload_file in "${upload_files[@]}"; do
    541 
    542   if [ -z "${upload_file}" ]; then
    543     cd "${file_dir}" || exit 1
    544 
    545     # new filename with date
    546     img_file="$(date +"${file_name_format}")"
    547     take_screenshot "${img_file}"
    548   else
    549     # upload file instead of screenshot
    550     img_file="${upload_file}"
    551   fi
    552 
    553   # get full path
    554   #cd "$(dirname "$(realpath "${img_file}")")"
    555   #img_file="$(realpath "${img_file}")"
    556 
    557   # check if file exists
    558   if ! [ -f "${img_file}" ]; then
    559     echo "file '${img_file}' doesn't exist !"
    560     read -r _
    561     exit 1
    562   fi
    563 
    564   # open image in editor if configured
    565   if [ "${edit}" = "true" ]; then
    566     edit_cmd=${edit_command//\%img/${img_file}}
    567     echo "Opening editor '${edit_cmd}'"
    568     if ! (eval "${edit_cmd}"); then
    569       echo "Error for image '${img_file}': command '${edit_cmd}' failed, not uploading. For more information visit https://github.com/jomo/imgur-screenshot/wiki/Troubleshooting" | tee -a "${log_file}"
    570       notify error "Something went wrong :(" "Information has been logged"
    571       exit 1
    572     fi
    573   fi
    574 
    575   if [ "${login}" = "true" ]; then
    576     upload_authenticated_image "${img_file}"
    577   else
    578     upload_anonymous_image "${img_file}"
    579   fi
    580 
    581   # delete file if configured
    582   if [ "${keep_file}" = "false" ] && [ -z "${1}" ]; then
    583     echo "Deleting temp file ${file_dir}/${img_file}"
    584     rm -rf "${img_file}"
    585   fi
    586 
    587   echo ""
    588 done
    589 
    590 
    591 if [ "${check_update}" = "true" ]; then
    592   check_for_update
    593 fi
    594 
    595 read -r _