+105
src/commands/handle.sh
+105
src/commands/handle.sh
···
1
+
#!/usr/bin/env bash
2
+
3
+
function atfile.handle() {
4
+
uri="$1"
5
+
handler="$2"
6
+
7
+
function atfile.handle.is_temp_file_needed() {
8
+
handler="${1//.desktop/}"
9
+
type="$2"
10
+
11
+
handlers=(
12
+
"app.drey.EarTag"
13
+
"com.github.neithern.g4music"
14
+
)
15
+
16
+
if [[ ${handlers[*]} =~ $handler ]]; then
17
+
echo 1
18
+
elif [[ $type == "text/"* ]]; then
19
+
echo 1
20
+
else
21
+
echo 0
22
+
fi
23
+
}
24
+
25
+
[[ $_output_json == 1 ]] && atfile.die "Command not available as JSON"
26
+
27
+
actor="$(echo "$uri" | cut -d "/" -f 3)"
28
+
key="$(echo "$uri" | cut -d "/" -f 4)"
29
+
30
+
atfile.util.create_dir "$_path_blobs_tmp"
31
+
32
+
if [[ -n "$actor" && -n "$key" ]]; then
33
+
atfile.util.override_actor "$actor"
34
+
35
+
atfile.say.debug "Getting record...\nโณ NSID: $_nsid_upload\nโณ Repo: $_username\nโณ Key: $key"
36
+
record="$(com.atproto.repo.getRecord "$_username" "$_nsid_upload" "$key")"
37
+
error="$(atfile.util.get_xrpc_error $? "$record")"
38
+
[[ -n "$error" ]] && atfile.die.gui.xrpc_error "Unable to get '$key'" "$error"
39
+
40
+
blob_cid="$(echo "$record" | jq -r ".value.blob.ref.\"\$link\"")"
41
+
blob_uri="$(atfile.util.build_blob_uri "$_username" "$blob_cid")"
42
+
file_type="$(echo "$record" | jq -r '.value.file.mimeType')"
43
+
44
+
if [[ $_os == "linux"* ]] && \
45
+
[ -x "$(command -v xdg-mime)" ] && \
46
+
[ -x "$(command -v xdg-open)" ] && \
47
+
[ -x "$(command -v gtk-launch)" ]; then
48
+
49
+
# HACK: Open with browser if $file_type isn't set
50
+
[[ -z $file_type ]] && file_type="text/html"
51
+
52
+
if [[ -z $handler ]]; then
53
+
atfile.say.debug "Querying for handler '$file_type'..."
54
+
handler="$(xdg-mime query default "$file_type")"
55
+
else
56
+
handler="$handler.desktop"
57
+
atfile.say.debug "Handler manually set to '$handler'"
58
+
fi
59
+
60
+
# shellcheck disable=SC2319
61
+
# shellcheck disable=SC2181
62
+
if [[ -n $handler ]] || [[ $? != 0 ]]; then
63
+
atfile.say.debug "Opening '$key' ($file_type) with '${handler//.desktop/}'..."
64
+
65
+
# HACK: Some apps don't like http(s)://; we'll need to handle these
66
+
if [[ $(atfile.handle.is_temp_file_needed "$handler" "$file_type") == 1 ]]; then
67
+
atfile.say.debug "Unsupported for streaming"
68
+
69
+
download_success=1
70
+
# shellcheck disable=SC2154
71
+
tmp_path="$_path_blobs_tmp/$blob_cid"
72
+
73
+
if ! [[ -f "$tmp_path" ]]; then
74
+
atfile.say.debug "Downloading '$blob_cid'..."
75
+
atfile.http.download "$blob_uri" "$tmp_path"
76
+
[[ $? != 0 ]] && download_success=0
77
+
else
78
+
atfile.say.debug "Blob '$blob_cid' already exists"
79
+
fi
80
+
81
+
if [[ $download_success == 1 ]]; then
82
+
atfile.say.debug "Launching '$handler'..."
83
+
gtk-launch "$handler" "$tmp_path" </dev/null &>/dev/null &
84
+
else
85
+
atfile.die.gui \
86
+
"Unable to download '$key'"
87
+
fi
88
+
else
89
+
atfile.say.debug "Launching '$handler'..."
90
+
gtk-launch "$handler" "$blob_uri" </dev/null &>/dev/null &
91
+
fi
92
+
else
93
+
atfile.say.debug "No handler for '$file_type'. Launching URI..."
94
+
atfile.util.launch_uri "$blob_uri"
95
+
fi
96
+
else
97
+
atfile.say.debug "Relevant tools not installed. Launching URI..."
98
+
atfile.util.launch_uri "$blob_uri"
99
+
fi
100
+
else
101
+
atfile.die.gui \
102
+
"Invalid ATFile URI\nโณ Must be 'atfile://<actor>/<key>'" \
103
+
"Invalid ATFile URI"
104
+
fi
105
+
}
+1
-2
src/commands/help.sh
+1
-2
src/commands/help.sh
···
75
75
bsky [<actor>]
76
76
Get Bluesky profile for <actor>\n
77
77
handle <at-uri>
78
-
Open at:// URI with relevant App\n
78
+
Open at:// URI with PDSls\n
79
79
handle <atfile-uri> [<handler>]
80
80
Open atfile:// URI with relevant App
81
81
โน๏ธ Set <handler> to a .desktop entry (with '.desktop') to force the
···
191
191
192
192
Usage
193
193
$_prog <command> [<arguments>]
194
-
$_prog at://<actor>[/<collection>/<rkey>]
195
194
$_prog atfile://<actor>/<key>\n\n"
196
195
197
196
[[ $ATFILE_DEVEL == 1 ]] && usage+="$usage_commands_devel\n\n"
-128
src/commands/old_cmds.sh
-128
src/commands/old_cmds.sh
···
289
289
fi
290
290
}
291
291
292
-
function atfile.invoke.handle_atfile() {
293
-
uri="$1"
294
-
handler="$2"
295
-
296
-
function atfile.invoke.handle_atfile.is_temp_file_needed() {
297
-
handler="${1//.desktop/}"
298
-
type="$2"
299
-
300
-
handlers=(
301
-
"app.drey.EarTag"
302
-
"com.github.neithern.g4music"
303
-
)
304
-
305
-
if [[ ${handlers[*]} =~ $handler ]]; then
306
-
echo 1
307
-
elif [[ $type == "text/"* ]]; then
308
-
echo 1
309
-
else
310
-
echo 0
311
-
fi
312
-
}
313
-
314
-
[[ $_output_json == 1 ]] && atfile.die "Command not available as JSON"
315
-
316
-
actor="$(echo "$uri" | cut -d "/" -f 3)"
317
-
key="$(echo "$uri" | cut -d "/" -f 4)"
318
-
319
-
atfile.util.create_dir "$_path_blobs_tmp"
320
-
321
-
if [[ -n "$actor" && -n "$key" ]]; then
322
-
atfile.util.override_actor "$actor"
323
-
324
-
atfile.say.debug "Getting record...\nโณ NSID: $_nsid_upload\nโณ Repo: $_username\nโณ Key: $key"
325
-
record="$(com.atproto.repo.getRecord "$_username" "$_nsid_upload" "$key")"
326
-
error="$(atfile.util.get_xrpc_error $? "$record")"
327
-
[[ -n "$error" ]] && atfile.die.gui.xrpc_error "Unable to get '$key'" "$error"
328
-
329
-
blob_cid="$(echo "$record" | jq -r ".value.blob.ref.\"\$link\"")"
330
-
blob_uri="$(atfile.util.build_blob_uri "$_username" "$blob_cid")"
331
-
file_type="$(echo "$record" | jq -r '.value.file.mimeType')"
332
-
333
-
if [[ $_os == "linux"* ]] && \
334
-
[ -x "$(command -v xdg-mime)" ] && \
335
-
[ -x "$(command -v xdg-open)" ] && \
336
-
[ -x "$(command -v gtk-launch)" ]; then
337
-
338
-
# HACK: Open with browser if $file_type isn't set
339
-
[[ -z $file_type ]] && file_type="text/html"
340
-
341
-
if [[ -z $handler ]]; then
342
-
atfile.say.debug "Querying for handler '$file_type'..."
343
-
handler="$(xdg-mime query default "$file_type")"
344
-
else
345
-
handler="$handler.desktop"
346
-
atfile.say.debug "Handler manually set to '$handler'"
347
-
fi
348
-
349
-
# shellcheck disable=SC2319
350
-
# shellcheck disable=SC2181
351
-
if [[ -n $handler ]] || [[ $? != 0 ]]; then
352
-
atfile.say.debug "Opening '$key' ($file_type) with '${handler//.desktop/}'..."
353
-
354
-
# HACK: Some apps don't like http(s)://; we'll need to handle these
355
-
if [[ $(atfile.invoke.handle_atfile.is_temp_file_needed "$handler" "$file_type") == 1 ]]; then
356
-
atfile.say.debug "Unsupported for streaming"
357
-
358
-
download_success=1
359
-
# shellcheck disable=SC2154
360
-
tmp_path="$_path_blobs_tmp/$blob_cid"
361
-
362
-
if ! [[ -f "$tmp_path" ]]; then
363
-
atfile.say.debug "Downloading '$blob_cid'..."
364
-
atfile.http.download "$blob_uri" "$tmp_path"
365
-
[[ $? != 0 ]] && download_success=0
366
-
else
367
-
atfile.say.debug "Blob '$blob_cid' already exists"
368
-
fi
369
-
370
-
if [[ $download_success == 1 ]]; then
371
-
atfile.say.debug "Launching '$handler'..."
372
-
gtk-launch "$handler" "$tmp_path" </dev/null &>/dev/null &
373
-
else
374
-
atfile.die.gui \
375
-
"Unable to download '$key'"
376
-
fi
377
-
else
378
-
atfile.say.debug "Launching '$handler'..."
379
-
gtk-launch "$handler" "$blob_uri" </dev/null &>/dev/null &
380
-
fi
381
-
else
382
-
atfile.say.debug "No handler for '$file_type'. Launching URI..."
383
-
atfile.util.launch_uri "$blob_uri"
384
-
fi
385
-
else
386
-
atfile.say.debug "Relevant tools not installed. Launching URI..."
387
-
atfile.util.launch_uri "$blob_uri"
388
-
fi
389
-
else
390
-
atfile.die.gui \
391
-
"Invalid ATFile URI\nโณ Must be 'atfile://<actor>/<key>'" \
392
-
"Invalid ATFile URI"
393
-
fi
394
-
}
395
-
396
-
function atfile.invoke.handle_aturi() {
397
-
uri="$1"
398
-
399
-
[[ $_output_json == 1 ]] && atfile.die "Command not available as JSON"
400
-
[[ "$uri" != "at://"* ]] && atfile.die.gui \
401
-
"Invalid AT URI\nโณ Must be 'at://<actor>[/<collection>/<rkey>]'" \
402
-
"Invalid AT URI"
403
-
404
-
atfile.say.debug "Resolving '$uri'..."
405
-
app_uri="$(atfile.util.get_app_url_for_at_uri "$uri")"
406
-
[[ -z "$app_uri" ]] && atfile.die.gui \
407
-
"Unable to resolve AT URI to App"
408
-
409
-
app_proto="$(echo "$app_uri" | cut -d ":" -f 1)"
410
-
411
-
atfile.say.debug "Opening '$app_uri' ($app_proto)..."
412
-
413
-
if [[ $app_proto == "atfile" ]]; then
414
-
atfile.invoke.handle_atfile "$app_uri"
415
-
else
416
-
atfile.util.launch_uri "$app_uri"
417
-
fi
418
-
}
419
-
420
292
function atfile.invoke.list() {
421
293
cursor="$1"
422
294
unset error