My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

imgur 20 KiB

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