My build of nnn with minor changes
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

596 rindas
20 KiB

  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. # shellcheck disable=SC2034
  69. if is_mac; then
  70. screenshot_select_command="screencapture -i %img"
  71. screenshot_window_command="screencapture -iWa %img"
  72. screenshot_full_command="screencapture %img"
  73. open_command="open %url"
  74. else
  75. screenshot_select_command="scrot -s %img"
  76. screenshot_window_command="scrot %img"
  77. screenshot_full_command="scrot %img"
  78. open_command="xdg-open %url"
  79. fi
  80. open="true"
  81. mode="select"
  82. edit_command="gimp %img"
  83. edit="false"
  84. exit_on_album_creation_fail="true"
  85. log_file="${HOME}/.imgur-screenshot.log"
  86. auto_delete=""
  87. copy_url="true"
  88. keep_file="true"
  89. check_update="true"
  90. # NOTICE: if you make changes here, also edit the docs at
  91. # https://github.com/jomo/imgur-screenshot/wiki/Config
  92. # You can override the config in ~/.config/imgur-screenshot/settings.conf
  93. ############## END CONFIG ##############
  94. settings_path="${HOME}/.config/imgur-screenshot/settings.conf"
  95. if [ -f "${settings_path}" ]; then
  96. source "${settings_path}"
  97. fi
  98. # dependency check
  99. if [ "${1}" = "--check" ]; then
  100. (which grep &>/dev/null && echo "OK: found grep") || echo "ERROR: grep not found"
  101. if is_mac; then
  102. if which growlnotify &>/dev/null; then
  103. echo "OK: found growlnotify"
  104. elif which terminal-notifier &>/dev/null; then
  105. echo "OK: found terminal-notifier"
  106. else
  107. echo "ERROR: growlnotify nor terminal-notifier found"
  108. fi
  109. (which screencapture &>/dev/null && echo "OK: found screencapture") || echo "ERROR: screencapture not found"
  110. (which pbcopy &>/dev/null && echo "OK: found pbcopy") || echo "ERROR: pbcopy not found"
  111. else
  112. (which notify-send &>/dev/null && echo "OK: found notify-send") || echo "ERROR: notify-send (from libnotify-bin) not found"
  113. (which scrot &>/dev/null && echo "OK: found scrot") || echo "ERROR: scrot not found"
  114. (which xclip &>/dev/null && echo "OK: found xclip") || echo "ERROR: xclip not found"
  115. fi
  116. (which curl &>/dev/null && echo "OK: found curl") || echo "ERROR: curl not found"
  117. exit 0
  118. fi
  119. # notify <'ok'|'error'> <title> <text>
  120. function notify() {
  121. if is_mac; then
  122. if which growlnotify &>/dev/null; then
  123. growlnotify --icon "${imgur_icon_path}" --iconpath "${imgur_icon_path}" --title "${2}" --message "${3}"
  124. else
  125. terminal-notifier -appIcon "${imgur_icon_path}" -contentImage "${imgur_icon_path}" -title "imgur: ${2}" -message "${3}"
  126. fi
  127. else
  128. if [ "${1}" = "error" ]; then
  129. notify-send -a ImgurScreenshot -u critical -c "im.error" -i "${imgur_icon_path}" -t 500 "imgur: ${2}" "${3}"
  130. else
  131. notify-send -a ImgurScreenshot -u low -c "transfer.complete" -i "${imgur_icon_path}" -t 500 "imgur: ${2}" "${3}"
  132. fi
  133. fi
  134. }
  135. function take_screenshot() {
  136. echo "Please select area"
  137. is_mac || sleep 0.1 # https://bbs.archlinux.org/viewtopic.php?pid=1246173#p1246173
  138. cmd="screenshot_${mode}_command"
  139. cmd=${!cmd//\%img/${1}}
  140. if ! shot_err="$(${cmd} &>/dev/null)"; then #takes a screenshot with selection
  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" | grep -Em 1 --color 'tag_name":\s*".*"' | cut -d '"' -f 4)"
  149. if ! [ -z "$remote_version" ]; 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. if ! response="$(curl --compressed -fsSL --stderr - \
  220. -F "client_id=${imgur_acct_key}" \
  221. -F "client_secret=${imgur_secret}" \
  222. -F "grant_type=refresh_token" \
  223. -F "refresh_token=${refresh_token}" \
  224. "${token_url}"
  225. )"; then
  226. # curl failed
  227. handle_upload_error "${response}" "${token_url}"
  228. exit 1
  229. fi
  230. save_access_token "${response}" "${1}"
  231. }
  232. function save_access_token() {
  233. if ! grep -q "access_token" <<<"${1}"; then
  234. # server did not send access_token
  235. echo "Error: Something is wrong with your credentials:"
  236. echo "${1}"
  237. exit 1
  238. fi
  239. access_token="$(grep -Eo 'access_token":".*"' <<<"${1}" | cut -d '"' -f 3)"
  240. refresh_token="$(grep -Eo 'refresh_token":".*"' <<<"${1}" | cut -d '"' -f 3)"
  241. expires_in="$(grep -Eo 'expires_in":[0-9]*' <<<"${1}" | cut -d ':' -f 2)"
  242. token_expire_time="$(( $(date +%s) + expires_in ))"
  243. # create dir if not exist
  244. mkdir -p "$(dirname "${2}")" 2>/dev/null
  245. touch "${2}" && chmod 600 "${2}"
  246. cat <<EOF > "${2}"
  247. access_token="${access_token}"
  248. refresh_token="${refresh_token}"
  249. token_expire_time="${token_expire_time}"
  250. EOF
  251. }
  252. function fetch_account_info() {
  253. 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)"
  254. if grep -Eq '"success":\s*true' <<<"${response}"; then
  255. username="$(grep -Eo '"url":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  256. echo "Logged in as ${username}."
  257. echo "https://${username}.imgur.com"
  258. else
  259. echo "Failed to fetch info: ${response}"
  260. fi
  261. }
  262. function delete_image() {
  263. response="$(curl --compressed -X DELETE -fsSL --stderr - -H "Authorization: Client-ID ${1}" "https://api.imgur.com/3/image/${2}")"
  264. if grep -Eq '"success":\s*true' <<<"${response}"; then
  265. echo "Image successfully deleted (delete hash: ${2})." >> "${3}"
  266. else
  267. echo "The Image could not be deleted: ${response}." >> "${3}"
  268. fi
  269. }
  270. function upload_authenticated_image() {
  271. echo "Uploading '${1}'..."
  272. title="$(echo "${1}" | rev | cut -d "/" -f 1 | cut -d "." -f 2- | rev)"
  273. if [ -n "${album_id}" ]; then
  274. 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)"
  275. else
  276. 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)"
  277. fi
  278. # JSON parser premium edition (not really)
  279. if grep -Eq '"success":\s*true' <<<"${response}"; then
  280. img_id="$(grep -Eo '"id":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  281. 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!
  282. del_id="$(grep -Eo '"deletehash":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  283. if [ ! -z "${auto_delete}" ]; then
  284. export -f delete_image
  285. echo "Deleting image in ${auto_delete} seconds."
  286. nohup /bin/bash -c "sleep ${auto_delete} && delete_image ${imgur_anon_id} ${del_id} ${log_file}" &
  287. fi
  288. handle_upload_success "https://i.imgur.com/${img_id}.${img_ext}" "https://imgur.com/delete/${del_id}" "${1}"
  289. else # upload failed
  290. err_msg="$(grep -Eo '"error":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  291. test -z "${err_msg}" && err_msg="${response}"
  292. handle_upload_error "${err_msg}" "${1}"
  293. fi
  294. }
  295. function upload_anonymous_image() {
  296. echo "Uploading '${1}'..."
  297. title="$(echo "${1}" | rev | cut -d "/" -f 1 | cut -d "." -f 2- | rev)"
  298. if [ -n "${album_id}" ]; then
  299. 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)"
  300. else
  301. 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)"
  302. fi
  303. # JSON parser premium edition (not really)
  304. if grep -Eq '"success":\s*true' <<<"${response}"; then
  305. img_id="$(grep -Eo '"id":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  306. 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!
  307. del_id="$(grep -Eo '"deletehash":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  308. if [ ! -z "${auto_delete}" ]; then
  309. export -f delete_image
  310. echo "Deleting image in ${auto_delete} seconds."
  311. nohup /bin/bash -c "sleep ${auto_delete} && delete_image ${imgur_anon_id} ${del_id} ${log_file}" &
  312. fi
  313. handle_upload_success "https://i.imgur.com/${img_id}.${img_ext}" "https://imgur.com/delete/${del_id}" "${1}"
  314. else # upload failed
  315. err_msg="$(grep -Eo '"error":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  316. test -z "${err_msg}" && err_msg="${response}"
  317. handle_upload_error "${err_msg}" "${1}"
  318. fi
  319. }
  320. function handle_upload_success() {
  321. echo ""
  322. echo "image link: ${1}"
  323. echo "delete link: ${2}"
  324. if [ "${copy_url}" = "true" ] && [ -z "${album_title}" ]; then
  325. if is_mac; then
  326. echo -n "${1}" | pbcopy
  327. else
  328. echo -n "${1}" | xclip -selection clipboard
  329. fi
  330. echo "URL copied to clipboard"
  331. fi
  332. # print to log file: image link, image location, delete link
  333. echo -e "${1}\t${3}\t${2}" >> "${log_file}"
  334. notify ok "Upload done!" "${1}"
  335. # if [ ! -z "${open_command}" ] && [ "${open}" = "true" ]; then
  336. # open_cmd=${open_command//\%url/${1}}
  337. # open_cmd=${open_cmd//\%img/${2}}
  338. # echo "Opening '${open_cmd}'"
  339. # eval "${open_cmd}"
  340. # fi
  341. }
  342. function handle_upload_error() {
  343. error="Upload failed: \"${1}\""
  344. echo "${error}"
  345. echo -e "Error\t${2}\t${error}" >> "${log_file}"
  346. notify error "Upload failed :(" "${1}"
  347. }
  348. function handle_album_creation_success() {
  349. echo ""
  350. echo "Album link: ${1}"
  351. echo "Delete hash: ${2}"
  352. echo ""
  353. notify ok "Album created!" "${1}"
  354. if [ "${copy_url}" = "true" ]; then
  355. if is_mac; then
  356. echo -n "${1}" | pbcopy
  357. else
  358. echo -n "${1}" | xclip -selection clipboard
  359. fi
  360. echo "URL copied to clipboard"
  361. fi
  362. # print to log file: album link, album title, delete hash
  363. echo -e "${1}\t\"${3}\"\t${2}" >> "${log_file}"
  364. }
  365. function handle_album_creation_error() {
  366. error="Album creation failed: \"${1}\""
  367. echo -e "Error\t${2}\t${error}" >> "${log_file}"
  368. notify error "Album creation failed :(" "${1}"
  369. if [ ${exit_on_album_creation_fail} ]; then
  370. exit 1
  371. fi
  372. }
  373. while [ ${#} != 0 ]; do
  374. case "${1}" in
  375. -h | --help)
  376. echo "usage: ${0} [--debug] [-c | --check | -v | -h | -u]"
  377. echo " ${0} [--debug] [option]... [file]..."
  378. echo ""
  379. echo " --debug Enable debugging, must be first option"
  380. echo " -h, --help Show this help, exit"
  381. echo " -v, --version Show current version, exit"
  382. echo " --check Check if all dependencies are installed, exit"
  383. echo " -c, --connect Show connected imgur account, exit"
  384. echo " -o, --open <true|false> Override 'open' config"
  385. echo " -e, --edit <true|false> Override 'edit' config"
  386. echo " -i, --edit-command <command> Override 'edit_command' config (include '%img'), sets --edit 'true'"
  387. echo " -l, --login <true|false> Override 'login' config"
  388. echo " -a, --album <album_title> Create new album and upload there"
  389. echo " -A, --album-id <album_id> Override 'album_id' config"
  390. echo " -k, --keep-file <true|false> Override 'keep_file' config"
  391. echo " -d, --auto-delete <s> Automatically delete image after <s> seconds"
  392. echo " -u, --update Check for updates, exit"
  393. echo " file Upload file instead of taking a screenshot"
  394. exit 0;;
  395. -v | --version)
  396. echo "${current_version}"
  397. exit 0;;
  398. -s | --select)
  399. mode="select"
  400. shift;;
  401. -w | --window)
  402. mode="window"
  403. shift;;
  404. -f | --full)
  405. mode="full"
  406. shift;;
  407. -o | --open)
  408. # shellcheck disable=SC2034
  409. open="${2}"
  410. shift 2;;
  411. -e | --edit)
  412. edit="${2}"
  413. shift 2;;
  414. -i | --edit-command)
  415. edit_command="${2}"
  416. edit="true"
  417. shift 2;;
  418. -l | --login)
  419. login="${2}"
  420. shift 2;;
  421. -c | --connect)
  422. load_access_token
  423. fetch_account_info
  424. exit 0;;
  425. -a | --album)
  426. album_title="${2}"
  427. shift 2;;
  428. -A | --album-id)
  429. album_id="${2}"
  430. shift 2;;
  431. -k | --keep-file)
  432. keep_file="${2}"
  433. shift 2;;
  434. -d | --auto-delete)
  435. auto_delete="${2}"
  436. shift 2;;
  437. -u | --update)
  438. check_for_update
  439. exit 0;;
  440. *)
  441. upload_files=("${@}")
  442. break;;
  443. esac
  444. done
  445. if [ "${login}" = "true" ]; then
  446. # load before changing directory
  447. load_access_token
  448. fi
  449. if [ -n "${album_title}" ]; then
  450. if [ "${login}" = "true" ]; then
  451. response="$(curl -fsSL --stderr - \
  452. -F "title=${album_title}" \
  453. -H "Authorization: Bearer ${access_token}" \
  454. https://api.imgur.com/3/album)"
  455. else
  456. response="$(curl -fsSL --stderr - \
  457. -F "title=${album_title}" \
  458. -H "Authorization: Client-ID ${imgur_anon_id}" \
  459. https://api.imgur.com/3/album)"
  460. fi
  461. if grep -Eq '"success":\s*true' <<<"${response}"; then # Album creation successful
  462. echo "Album '${album_title}' successfully created"
  463. album_id="$(grep -Eo '"id":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  464. del_id="$(grep -Eo '"deletehash":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  465. handle_album_creation_success "http://imgur.com/a/${album_id}" "${del_id}" "${album_title}"
  466. if [ "${login}" = "false" ]; then
  467. album_id="${del_id}"
  468. fi
  469. else # Album creation failed
  470. err_msg="$(grep -Eo '"error":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
  471. test -z "${err_msg}" && err_msg="${response}"
  472. handle_album_creation_error "${err_msg}" "${album_title}"
  473. fi
  474. fi
  475. if [ -z "${upload_files[*]}" ]; then
  476. upload_files[0]=""
  477. fi
  478. for upload_file in "${upload_files[@]}"; do
  479. if [ -z "${upload_file}" ]; then
  480. cd "${file_dir}" || exit 1
  481. # new filename with date
  482. img_file="$(date +"${file_name_format}")"
  483. take_screenshot "${img_file}"
  484. else
  485. # upload file instead of screenshot
  486. img_file="${upload_file}"
  487. fi
  488. # get full path
  489. #cd "$(dirname "$(realpath "${img_file}")")"
  490. #img_file="$(realpath "${img_file}")"
  491. # check if file exists
  492. if ! [ -f "${img_file}" ]; then
  493. echo "file '${img_file}' doesn't exist !"
  494. read -r _
  495. exit 1
  496. fi
  497. # open image in editor if configured
  498. if [ "${edit}" = "true" ]; then
  499. edit_cmd=${edit_command//\%img/${img_file}}
  500. echo "Opening editor '${edit_cmd}'"
  501. if ! (eval "${edit_cmd}"); then
  502. 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}"
  503. notify error "Something went wrong :(" "Information has been logged"
  504. exit 1
  505. fi
  506. fi
  507. if [ "${login}" = "true" ]; then
  508. upload_authenticated_image "${img_file}"
  509. else
  510. upload_anonymous_image "${img_file}"
  511. fi
  512. # delete file if configured
  513. if [ "${keep_file}" = "false" ] && [ -z "${1}" ]; then
  514. echo "Deleting temp file ${file_dir}/${img_file}"
  515. rm -rf "${img_file}"
  516. fi
  517. echo ""
  518. done
  519. if [ "${check_update}" = "true" ]; then
  520. check_for_update
  521. fi
  522. read -r _