diff --git a/src/commands/blob.sh b/src/commands/blob.sh index ad5d049..93b931a 100644 --- a/src/commands/blob.sh +++ b/src/commands/blob.sh @@ -81,5 +81,10 @@ function atfile.blob_upload() { fi atfile.say.debug "Uploading blob...\n↳ File: $file" - com.atproto.sync.uploadBlob "$file" | jq + result="$(com.atproto.sync.uploadBlob "$file")" + error="$(atfile.util.get_xrpc_error $? "$result")" + + [[ -n "$error" ]] && atfile.die.xrpc_error "Unable to upload blob" "$error" + + echo "$result" | jq } diff --git a/src/commands/download.sh b/src/commands/download.sh index 6bf8f19..85a64a9 100644 --- a/src/commands/download.sh +++ b/src/commands/download.sh @@ -3,52 +3,50 @@ function atfile.download() { key="$1" decrypt=$2 - success=1 downloaded_file="" # shellcheck disable=SC2154 atfile.say.debug "Getting record...\n↳ NSID: $_nsid_upload\n↳ Repo: $_username\n↳ Key: $key" record="$(com.atproto.repo.getRecord "$_username" "$_nsid_upload" "$key")" - [[ $? != 0 || -z "$record" || "$record" == "{}" || "$record" == *"\"error\":"* ]] && success=0 + error="$(atfile.util.get_xrpc_error $? "$record")" - if [[ $success == 1 ]]; then - blob_uri="$(atfile.util.build_blob_uri "$(echo "$record" | jq -r ".uri" | cut -d "/" -f 3)" "$(echo "$record" | jq -r ".value.blob.ref.\"\$link\"")")" - file_name="$(echo "$record" | jq -r '.value.file.name')" - key="$(atfile.util.get_rkey_from_at_uri "$(echo "$record" | jq -r ".uri")")" - downloaded_file="$(atfile.util.build_out_filename "$key" "$file_name")" + [[ -n "$error" ]] && atfile.die.xrpc_error "Unable to download '$key'" "$error" - curl -H "User-Agent: $(atfile.util.get_uas)" --silent "$blob_uri" -o "$downloaded_file" - # shellcheck disable=SC2181 - [[ $? != 0 ]] && success=0 + blob_uri="$(atfile.util.build_blob_uri "$(echo "$record" | jq -r ".uri" | cut -d "/" -f 3)" "$(echo "$record" | jq -r ".value.blob.ref.\"\$link\"")")" + file_name="$(echo "$record" | jq -r '.value.file.name')" + key="$(atfile.util.get_rkey_from_at_uri "$(echo "$record" | jq -r ".uri")")" + downloaded_file="$(atfile.util.build_out_filename "$key" "$file_name")" + + curl -H "User-Agent: $(atfile.util.get_uas)" --silent "$blob_uri" -o "$downloaded_file" + # shellcheck disable=SC2181 + if [[ $? != 0 ]]; then + [[ -f "$downloaded_file" ]] && rm -f "$downloaded_file" + atfile.die "Unable to download '$key'" fi - if [[ $decrypt == 1 && $success == 1 ]]; then + if [[ $decrypt == 1 ]]; then new_downloaded_file="$(echo "$downloaded_file" | sed -s s/.gpg//)" gpg --quiet --output "$new_downloaded_file" --decrypt "$downloaded_file" # shellcheck disable=SC2181 if [[ $? != 0 ]]; then - success=0 + [[ -f "$downloaded_file" ]] && rm -f "$downloaded_file" + atfile.die "Unable to decrypt '$key'" else rm -f "$downloaded_file" downloaded_file="$new_downloaded_file" fi fi - if [[ $success == 1 ]]; then - # shellcheck disable=SC2154 - if [[ $_output_json == 1 ]]; then - is_decrypted="false" - [[ $decrypt == 1 ]] && is_decrypted="true" - echo -e "{ \"decrypted\": $is_decrypted, \"name\": \"$(basename "${downloaded_file}")\", \"path\": \"$(atfile.util.get_realpath "${downloaded_file}")\" }" | jq - else - echo -e "Downloaded: $key" - [[ $decrypt == 1 ]] && echo "Decrypted: $downloaded_file" - echo -e "↳ Path: $(atfile.util.get_realpath "$downloaded_file")" - fi + # shellcheck disable=SC2154 + if [[ $_output_json == 1 ]]; then + is_decrypted="false" + [[ $decrypt == 1 ]] && is_decrypted="true" + echo -e "{ \"decrypted\": $is_decrypted, \"name\": \"$(basename "${downloaded_file}")\", \"path\": \"$(atfile.util.get_realpath "${downloaded_file}")\" }" | jq else - [[ -f "$downloaded_file" ]] && rm -f "$downloaded_file" - atfile.die "Unable to download '$key'" + echo -e "Downloaded: $key" + [[ $decrypt == 1 ]] && echo "Decrypted: $downloaded_file" + echo -e "↳ Path: $(atfile.util.get_realpath "$downloaded_file")" fi } diff --git a/src/commands/get.sh b/src/commands/get.sh index 6794dba..04fc741 100644 --- a/src/commands/get.sh +++ b/src/commands/get.sh @@ -2,14 +2,16 @@ function atfile.get() { key="$1" - success=1 + unset error # shellcheck disable=SC2154 atfile.say.debug "Getting record...\n↳ NSID: $_nsid_upload\n↳ Repo: $_username\n↳ Key: $key" record="$(com.atproto.repo.getRecord "$_username" "$_nsid_upload" "$key")" - [[ $? != 0 || -z "$record" || "$record" == "{}" || "$record" == *"\"error\":"* ]] && success=0 + error="$(atfile.util.get_xrpc_error $? "$record")" + + [[ -n "$error" ]] && atfile.die.xrpc_error "Unable to get '$key'" "$error" - if [[ $success == 1 ]]; then + if [[ -z "$error" ]]; then file_type="$(echo "$record" | jq -r '.value.file.mimeType')" did="$(echo "$record" | jq -r ".uri" | cut -d "/" -f 3)" key="$(atfile.util.get_rkey_from_at_uri "$(echo "$record" | jq -r ".uri")")" @@ -105,8 +107,6 @@ function atfile.get() { esac fi fi - else - atfile.die "Unable to get '$key'" fi } diff --git a/src/commands/lock.sh b/src/commands/lock.sh index 6bb2597..aec8642 100644 --- a/src/commands/lock.sh +++ b/src/commands/lock.sh @@ -8,7 +8,7 @@ function atfile.lock() { # shellcheck disable=SC2154 atfile.say.debug "Getting record...\n↳ NSID: $_nsid_upload\n↳ Repo: $_username\n↳ Key: $key" upload_record="$(com.atproto.repo.getRecord "$_username" "$_nsid_upload" "$key")" - error=$(atfile.util.get_xrpc_error $? "$upload_record") + error="$(atfile.util.get_xrpc_error $? "$upload_record")" if [[ -z "$error" ]]; then if [[ $locked == 1 ]]; then @@ -22,7 +22,7 @@ function atfile.lock() { # shellcheck disable=SC2154 atfile.say.debug "Updating record...\n↳ NSID: $_nsid_lock\n↳ Repo: $_username\n↳ Key: $key" record="$(com.atproto.repo.putRecord "$_username" "$_nsid_lock" "$key" "$lock_record")" - error=$(atfile.util.get_xrpc_error $? "$record") + error="$(atfile.util.get_xrpc_error $? "$record")" fi if [[ -z "$error" ]]; then @@ -38,9 +38,9 @@ function atfile.lock() { fi else if [[ $locked == true ]]; then - atfile.die "Unable to lock '$key'" "$error" + atfile.die.xrpc_error "Unable to lock '$key'" "$error" else - atfile.die "Unable to unlock '$key'" "$error" + atfile.die.xrpc_error "Unable to unlock '$key'" "$error" fi fi }