š¦āš¦ Store and retrieve files on the Atmosphere
1#!/usr/bin/env bash
2
3function atfile.download() {
4 key="$1"
5 decrypt=$2
6 downloaded_file=""
7
8 # shellcheck disable=SC2154
9 atfile.say.debug "Getting record...\nā³ NSID: $_nsid_upload\nā³ Repo: $_username\nā³ Key: $key"
10 record="$(com.atproto.repo.getRecord "$_username" "$_nsid_upload" "$key")"
11 error="$(atfile.util.get_xrpc_error $? "$record")"
12
13 [[ -n "$error" ]] && atfile.die.xrpc_error "Unable to download '$key'" "$error"
14
15 blob_uri="$(atfile.util.build_blob_uri "$(echo "$record" | jq -r ".uri" | cut -d "/" -f 3)" "$(echo "$record" | jq -r ".value.blob.ref.\"\$link\"")")"
16 file_name="$(echo "$record" | jq -r '.value.file.name')"
17 key="$(atfile.util.get_rkey_from_at_uri "$(echo "$record" | jq -r ".uri")")"
18 downloaded_file="$(atfile.util.build_out_filename "$key" "$file_name")"
19
20 curl -H "User-Agent: $(atfile.util.get_uas)" --silent "$blob_uri" -o "$downloaded_file"
21 # shellcheck disable=SC2181
22 if [[ $? != 0 ]]; then
23 [[ -f "$downloaded_file" ]] && rm -f "$downloaded_file"
24 atfile.die "Unable to download '$key'"
25 fi
26
27 if [[ $decrypt == 1 ]]; then
28 new_downloaded_file="$(echo "$downloaded_file" | sed -s s/.gpg//)"
29
30 gpg --quiet --output "$new_downloaded_file" --decrypt "$downloaded_file"
31
32 # shellcheck disable=SC2181
33 if [[ $? != 0 ]]; then
34 [[ -f "$downloaded_file" ]] && rm -f "$downloaded_file"
35 atfile.die "Unable to decrypt '$key'"
36 else
37 rm -f "$downloaded_file"
38 downloaded_file="$new_downloaded_file"
39 fi
40 fi
41
42 # shellcheck disable=SC2154
43 if [[ $_output_json == 1 ]]; then
44 is_decrypted="false"
45 [[ $decrypt == 1 ]] && is_decrypted="true"
46 echo -e "{ \"decrypted\": $is_decrypted, \"name\": \"$(basename "${downloaded_file}")\", \"path\": \"$(atfile.util.get_realpath "${downloaded_file}")\" }" | jq
47 else
48 echo -e "Downloaded: $key"
49 [[ $decrypt == 1 ]] && echo "Decrypted: $downloaded_file"
50 echo -e "ā³ Path: $(atfile.util.get_realpath "$downloaded_file")"
51 fi
52}