#!/usr/bin/env bash function atfile.upload() { file="$1" recipient="$2" key="$3" unset error if [[ ! -f "$file" ]]; then atfile.die "File '$file' does not exist" else file="$(atfile.util.get_realpath "$file")" fi if [[ -n $recipient ]]; then file_crypt="$(dirname "$file")/$(basename "$file").gpg" # shellcheck disable=SC2154 [[ $_output_json == 0 ]] && echo -e "Encrypting '$file_crypt'..." gpg --yes --quiet --recipient "$recipient" --output "$file_crypt" --encrypt "$file" # shellcheck disable=SC2181 if [[ $? == 0 ]]; then file="$file_crypt" else rm -f "$file_crypt" atfile.die "Unable to encrypt '$(basename "$file")'" fi fi if [[ -z "$error" ]]; then unset file_date unset file_size unset file_type # shellcheck disable=SC2154 case "$_os" in "bsd"|"macos") file_date="$(atfile.util.get_date "$(stat -f '%Sm' -t "%Y-%m-%dT%H:%M:%SZ" "$file")")" file_size="$(stat -f '%z' "$file")" file_type="$(file -b --mime-type "$file")" ;; "haiku") haiku_file_attr="$(catattr BEOS:TYPE "$file" 2> /dev/null)" # shellcheck disable=SC2181 [[ $? == 0 ]] && file_type="$(echo "$haiku_file_attr" | cut -d ":" -f 3 | xargs)" file_date="$(atfile.util.get_date "$(stat -c '%y' "$file")")" file_size="$(stat -c %s "$file")" ;; *) file_date="$(atfile.util.get_date "$(stat -c '%y' "$file")")" file_size="$(stat -c %s "$file")" file_type="$(file -b --mime-type "$file")" ;; esac file_hash="$(atfile.util.get_md5 "$file")" file_hash_checksum="$(echo "$file_hash" | cut -d "|" -f 1)" file_hash_type="$(echo "$file_hash" | cut -d "|" -f 2)" file_name="$(basename "$file")" if [[ -n $recipient ]]; then file_type="application/prs.atfile.gpg-crypt" elif [[ "$file_type" == "application/"* ]]; then file_extension="${file_name##*.}" case "$file_extension" in "car") file_type="application/prs.atfile.car" ;; "dmg"|"smi") file_type="application/x-apple-diskimage" ;; esac fi file_type_emoji="$(atfile.util.get_file_type_emoji "$file_type")" # shellcheck disable=SC2154 atfile.say.debug "File: $file\n↳ Date: $file_date\n↳ Hash: $file_hash_checksum ($file_hash_type)\n↳ Name: $file_name\n↳ Size: $file_size\n↳ Type: $file_type_emoji $file_type" unset file_finger_record unset file_meta_record file_finger_record="$(atfile.util.get_finger_record)" file_meta_record="$(atfile.util.get_meta_record "$file" "$file_type")" atfile.say.debug "Checking filesize..." # shellcheck disable=SC2154 file_size_surplus="$(atfile.util.get_file_size_surplus_for_pds "$file_size" "$_server")" if [[ $file_size_surplus != 0 ]]; then die_message="File '$file_name' is too large ($(atfile.util.get_file_size_pretty "$file_size_surplus") over)" atfile.die "$die_message" fi # shellcheck disable=SC2154 [[ $_output_json == 0 ]] && echo "Uploading '$file'..." blob="$(com.atproto.sync.uploadBlob "$file")" error="$(atfile.util.get_xrpc_error $? "$blob")" [[ $error == "?" ]] && error="Blob rejected by PDS (too large?)" atfile.say.debug "Uploading blob...\n↳ Ref: $(echo "$blob" | jq -r ".ref.\"\$link\"")" if [[ -z "$error" ]]; then # shellcheck disable=SC2154 file_record="$(blue.zio.atfile.upload "$blob" "$_now" "$file_hash_checksum" "$file_hash_type" "$file_date" "$file_name" "$file_size" "$file_type" "$file_meta_record" "$file_finger_record")" if [[ -n "$key" ]]; then # shellcheck disable=SC2154 atfile.say.debug "Updating record...\n↳ NSID: $_nsid_upload\n↳ Repo: $_username\n↳ Key: $key" record="$(com.atproto.repo.putRecord "$_username" "$_nsid_upload" "$key" "$file_record")" error="$(atfile.util.get_xrpc_error $? "$record")" else # shellcheck disable=SC2154 atfile.say.debug "Creating record...\n↳ NSID: $_nsid_upload\n↳ Repo: $_username" record="$(com.atproto.repo.createRecord "$_username" "$_nsid_upload" "$file_record")" error="$(atfile.util.get_xrpc_error $? "$record")" fi fi fi if [[ -n $recipient ]]; then rm -f "$file" fi if [[ -z "$error" ]]; then unset recipient_key blob_uri="$(atfile.util.build_blob_uri "$(echo "$record" | jq -r ".uri" | cut -d "/" -f 3)" "$(echo "$blob" | jq -r ".ref.\"\$link\"")")" key="$(atfile.util.get_rkey_from_at_uri "$(echo "$record" | jq -r ".uri")")" if [[ -n "$recipient" ]]; then recipient_key="$(gpg --list-keys "$recipient" | sed -n 2p | xargs)" fi # shellcheck disable=SC2154 if [[ $_output_json == 1 ]]; then unset recipient_json if [[ -n "$recipient" ]]; then recipient_json="{ \"id\": \"$recipient\", \"key\": \"$recipient_key\" }" else recipient_json="null" fi echo -e "{ \"blob\": \"$blob_uri\", \"key\": \"$key\", \"upload\": $record, \"recipient\": $recipient_json }" | jq else echo "---" # shellcheck disable=SC2154 if [[ $_os == "haiku" ]]; then # BUG: Haiku Terminal has issues with emojis echo "Uploaded: $file_name" else echo "Uploaded: $file_type_emoji $file_name" fi atfile.util.print_blob_url_output "$blob_uri" echo -e "↳ Key: $key" # shellcheck disable=SC2154 echo -e "↳ URI: atfile://$_username/$key" if [[ -n "$recipient" ]]; then echo -e "↳ Recipient: $recipient ($recipient_key)" fi fi else atfile.die.xrpc_error "Unable to upload '$file'" "$error" fi }