📦➔🦋 Store and retrieve files on the Atmosphere
1#!/usr/bin/env bash
2
3function atfile.get() {
4 key="$1"
5 unset error
6
7 # shellcheck disable=SC2154
8 atfile.say.debug "Getting record...\n↳ NSID: $_nsid_upload\n↳ Repo: $_username\n↳ Key: $key"
9 record="$(com.atproto.repo.getRecord "$_username" "$_nsid_upload" "$key")"
10 error="$(atfile.util.get_xrpc_error $? "$record")"
11
12 [[ -n "$error" ]] && atfile.die.xrpc_error "Unable to get '$key'" "$error"
13
14 if [[ -z "$error" ]]; then
15 file_type="$(echo "$record" | jq -r '.value.file.mimeType')"
16 did="$(echo "$record" | jq -r ".uri" | cut -d "/" -f 3)"
17 key="$(atfile.util.get_rkey_from_at_uri "$(echo "$record" | jq -r ".uri")")"
18 blob_uri="$(atfile.util.build_blob_uri "$did" "$(echo "$record" | jq -r ".value.blob.ref.\"\$link\"")")"
19 cdn_uri="$(atfile.util.get_cdn_uri "$did" "$(echo "$record" | jq -r ".value.blob.ref.\"\$link\"")" "$file_type")"
20
21 unset locked
22 unset encrypted
23
24 # shellcheck disable=SC2154
25 atfile.say.debug "Getting record...\n↳ NSID: $_nsid_lock\n↳ Repo: $_username\n↳ Key: $key"
26 locked_record="$(com.atproto.repo.getRecord "$_username" "$_nsid_lock" "$key")"
27 # shellcheck disable=SC2181
28 if [[ $? == 0 ]] && [[ -n "$locked_record" ]]; then
29 if [[ $(echo "$locked_record" | jq -r ".value.lock") == true ]]; then
30 locked="$(atfile.util.get_yn 1)"
31 else
32 locked="$(atfile.util.get_yn 0)"
33 fi
34 fi
35
36 if [[ "$file_type" == "application/prs.atfile.gpg-crypt" ]]; then
37 encrypted="$(atfile.util.get_yn 1)"
38 else
39 encrypted="$(atfile.util.get_yn 0)"
40 fi
41
42 # shellcheck disable=SC2154
43 if [[ $_output_json == 1 ]]; then
44 browser_accessible=$(atfile.util.get_yn "$(atfile.util.is_url_accessible_in_browser "$blob_uri")")
45
46 echo "{ \"encrypted\": $encrypted, \"locked\": $locked, \"upload\": $(echo "$record" | jq -r ".value"), \"url\": { \"blob\": \"$blob_uri\", \"browser_accessible\": $browser_accessible, \"cdn\": { \"bsky\": \"$cdn_uri\" } } }" | jq
47 else
48 file_date="$(echo "$record" | jq -r '.value.file.modifiedAt')"
49 file_hash="$(echo "$record" | jq -r '.value.checksum.hash')"
50 file_hash_type="$(echo "$record" | jq -r '.value.checksum.algo')"
51 [[ "$file_hash_type" == "null" ]] && file_hash_type="$(echo "$record" | jq -r '.value.checksum.type')"
52 file_hash_pretty="$file_hash ($file_hash_type)"
53 file_name="$(echo "$record" | jq -r '.value.file.name')"
54 file_name_pretty="$(atfile.util.get_file_name_pretty "$(echo "$record" | jq -r '.value')")"
55 file_size="$(echo "$record" | jq -r '.value.file.size')"
56 file_size_pretty="$(atfile.util.get_file_size_pretty "$file_size")"
57
58 unset finger_type
59 header="$file_name_pretty"
60
61 if [[ $(atfile.util.is_null_or_empty "$file_hash_type") == 1 ]] || [[ "$file_hash_type" == "md5" && ${#file_hash} != 32 ]] || [[ "$file_hash_type" == "none" ]]; then
62 file_hash_pretty="(None)"
63 fi
64
65 if [[ "$(echo "$record" | jq -r ".value.finger")" != "null" ]]; then
66 finger_type="$(echo "$record" | jq -r ".value.finger.\"\$type\"" | cut -d "#" -f 2)"
67 fi
68
69 echo "$header"
70 atfile.util.print_blob_url_output "$blob_uri"
71 [[ -n "$cdn_uri" ]] && echo -e " ↳ CDN: $cdn_uri"
72 # shellcheck disable=SC2154
73 echo -e "↳ URI: atfile://$_username/$key"
74 echo -e "↳ File: $key"
75 echo -e " ↳ Name: $file_name"
76 echo -e " ↳ Type: $file_type"
77 echo -e " ↳ Size: $file_size_pretty"
78 echo -e " ↳ Date: $(atfile.util.get_date "$file_date" "%Y-%m-%d %H:%M:%S %Z")"
79 echo -e " ↳ Hash: $file_hash_pretty"
80 echo -e "↳ Locked: $locked"
81 echo -e "↳ Encrypted: $encrypted"
82 if [[ -z "$finger_type" ]]; then
83 echo -e "↳ Source: (Unknown)"
84 else
85 case $finger_type in
86 "browser")
87 finger_browser_uas="$(echo "$record" | jq -r ".value.finger.userAgent")"
88
89 [[ -z $finger_browser_uas || $finger_browser_uas == "null" ]] && finger_browser_uas="(Unknown)"
90
91 echo -e "↳ Source: $finger_browser_uas"
92 ;;
93 "machine")
94 finger_machine_app="$(echo "$record" | jq -r ".value.finger.app")"
95 finger_machine_host="$(echo "$record" | jq -r ".value.finger.host")"
96 finger_machine_os="$(echo "$record" | jq -r ".value.finger.os")"
97
98 [[ -z $finger_machine_app || $finger_machine_app == "null" ]] && finger_machine_app="(Unknown)"
99
100 echo -e "↳ Source: $finger_machine_app"
101 [[ -n $finger_machine_host && $finger_machine_host != "null" ]] && echo -e " ↳ Host: $finger_machine_host"
102 [[ -n $finger_machine_os && $finger_machine_os != "null" ]] && echo -e " ↳ OS: $finger_machine_os"
103 ;;
104 *)
105 echo -e "↳ Source: (Unknown)"
106 ;;
107 esac
108 fi
109 fi
110 fi
111}
112
113function atfile.get_url() {
114 key="$1"
115 unset error
116
117 # shellcheck disable=SC2154
118 atfile.say.debug "Getting record...\n↳ NSID: $_nsid_upload\n↳ Repo: $_username\n↳ Key: $key"
119 record="$(com.atproto.repo.getRecord "$_username" "$_nsid_upload" "$key")"
120 error="$(atfile.util.get_xrpc_error $? "$record")"
121
122 if [[ -z "$error" ]]; then
123 blob_url="$(atfile.util.build_blob_uri "$(echo "$record" | jq -r ".uri" | cut -d "/" -f 3)" "$(echo "$record" | jq -r ".value.blob.ref.\"\$link\"")")"
124
125 # shellcheck disable=SC2154
126 if [[ $_output_json == 1 ]]; then
127 echo -e "{\"url\": \"$blob_url\" }" | jq
128 else
129 echo "$blob_url"
130 fi
131 else
132 atfile.die.xrpc_error "Unable to get '$key'" "$error"
133 fi
134}