My build of nnn with minor changes
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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