2
# eget - simply shell on wget for loading directories over http (wget does not support wildcard for http)
4
# eget http://ftp.altlinux.ru/pub/security/ssl/*
6
# Copyright (C) 2014-2014, 2016, 2020, 2022 Etersoft
7
# Copyright (C) 2014 Daniil Mikhailov <danil@etersoft.ru>
8
# Copyright (C) 2016-2017, 2020, 2022 Vitaly Lipatov <lav@etersoft.ru>
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU Affero General Public License as published by
12
# the Free Software Foundation, either version 3 of the License, or
13
# (at your option) any later version.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU Affero General Public License for more details.
20
# You should have received a copy of the GNU Affero General Public License
21
# along with this program. If not, see <http://www.gnu.org/licenses/>.
26
PROGDIR=$(dirname "$0")
27
PROGNAME=$(basename "$0")
29
[ "$PROGDIR" = "." ] && PROGDIR="$(pwd)"
30
if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
46
[ -n "$quiet" ] && return
52
if [ -n "$EPMMODE" ] ; then
54
(unset EGET_IPFS_GATEWAY; unset EGET_IPFS_API ; unset EGET_IPFS_DB ; EGET_BACKEND=$ORIG_EGET_BACKEND internal_tools_eget "$@" )
58
[ -n "$PROGNAME" ] || fatal "pipe mode is not supported"
61
#[ -n "$verbose" ] && bashopt='-x'
63
(unset EGET_IPFS_GATEWAY; unset EGET_IPFS_API ; unset EGET_IPFS_DB ; EGET_BACKEND=$ORIG_EGET_BACKEND $CMDSHELL $bashopt $PROGDIR/$PROGNAME "$@" )
69
# copied from eepm project
71
# copied from /etc/init.d/outformat (ALT Linux)
74
# Set a sane TERM required for tput
75
[ -n "$TERM" ] || TERM=dumb
90
is_command tput >/dev/null 2>/dev/null || return
91
# FreeBSD does not support tput -S
92
echo | a= tput -S >/dev/null 2>/dev/null || return
93
export USETTY="tput -S"
96
: ${BLACK:=0} ${RED:=1} ${GREEN:=2} ${YELLOW:=3} ${BLUE:=4} ${MAGENTA:=5} ${CYAN:=6} ${WHITE:=7}
100
[ -n "$USETTY" ] || return
109
[ -n "$USETTY" ] || return
117
[ -n "$USETTY" ] || return
119
echo op; # set Original color Pair.
120
echo sgr0; # turn off all special graphics mode (bold in our case).
127
[ -n "$verbose" ] || return
131
# Print command line and run command line
134
if [ -z "$quiet" ] ; then
137
[ "$UID" = 0 ] && PROMTSIG="#"
143
# Print command line and run command line
152
[ -n "$verbose" ] && showcmd "$@"
158
# print a path to the command if exists in $PATH
159
if a= which which 2>/dev/null >/dev/null ; then
160
# the best case if we have which command (other ways needs checking)
161
# TODO: don't use which at all, it is binary, not builtin shell command
164
a= which -- "$1" 2>/dev/null
166
elif a= type -a type 2>/dev/null >/dev/null ; then
169
a= type -fpP -- "$1" 2>/dev/null
174
a= type "$1" 2>/dev/null | sed -e 's|.* /|/|'
178
# check if <arg> is a real command
181
print_command_path "$1" >/dev/null
184
# add realpath if missed
185
if ! is_command realpath ; then
188
[ -n "$*" ] || return
197
[ -z "$1" ] && cat && return
198
# translate glob to regexp
199
grep "$(echo "$1" | sed -e 's|\.|\\.|g' -e 's|\*|.*|g' -e 's|\?|.|g' )$"
204
if [ -n "$SECONDLATEST" ] ; then
205
sort -V | tail -n2 | head -n1
208
[ -z "$LATEST" ] && cat && return
215
echo "$1" | grep -q "^/" && return
216
echo "$1" | grep -q "^file:/"
221
echo "$1" | sed -e 's|^file://*|/|'
226
echo "$1" | grep -q "^[filehtps]*:/"
232
is_url "$URL" || return
233
echo "$URL" | grep -q "[?&]"
238
# If a CID is 46 characters starting with "Qm", it's a CIDv0
239
echo "$1" | grep -q -E "^Qm[[:alnum:]]{44}$" && return
240
# TODO: CIDv1 support, see https://github.com/multiformats/cid
246
is_ipfs_hash "$1" && return
247
echo "$1" | grep -q "^ipfs://"
253
echo "$1" | grep -q "^https://" & return
254
echo "$1" | grep -q "^http://" & return
259
echo "$1" | sed -e 's|^ipfs://*||' -e 's|\?.*||'
263
# args: cmd <URL> <options>
264
# will run cmd <options> <URL>
265
download_with_mirroring()
273
$CMD "$@" "$URL" && return
275
[ -n "$CHECKMIRRORS" ] || return $res
277
MIRROR="https://mirror.eterfund.ru"
278
SECONDURL="$(echo "$URL" | sed -e "s|^.*://|$MIRROR/|")"
279
$CMD "$@" "$SECONDURL" && URL="$SECONDURL" && return
281
MIRROR="https://mirror.eterfund.org"
282
SECONDURL="$(echo "$URL" | sed -e "s|^.*://|$MIRROR/|")"
283
$CMD "$@" "$SECONDURL" && URL="$SECONDURL" && return
308
# TODO: wget --trust-server-names
310
WGETNAMEOPTIONS='--content-disposition'
311
CURLNAMEOPTIONS='--remote-name --remote-time --remote-header-name'
341
eget - wget like downloader wrapper with wildcard support in filename part of URL
342
Usage: eget [options] http://somesite.ru/dir/na*.log
345
-q|--quiet - quiet mode
346
--verbose - verbose mode
347
-k|--no-check-certificate - skip SSL certificate chain support
348
-H|--header - use <header> (X-Cache:1 for example)
349
-U|-A|--user-agent - send browser like UserAgent
350
--compressed - request a compressed response and automatically decompress the content
351
-4|--ipv4|--inet4-only - use only IPV4
352
-6|--ipv6|--inet6-only - use only IPV6
353
-O-|-O - - output downloaded file to stdout
354
-O file - download to this file
355
--latest - print only latest version of a file
356
--second-latest - print only second to latest version of a file
357
--allow-mirrors - check mirrors if url is not accessible
359
--list|--list-only - print only URLs
360
--check URL - check if the URL is accessible (returns HTTP 200 OK)
361
--get-response URL - get response with all headers (ever if HEAD is not acceptable)
362
--get-filename URL - print filename for the URL (via Content-Disposition if applicable)
363
--get-real-url URL - print URL after all redirects
364
--get-ipfs-cid URL - print CID for URL (after all redirects)
367
ftp:// http:// https:// file:/ ipfs://
369
Supported backends (set like EGET_BACKEND=curl)
370
wget curl (todo: aria2c)
373
$ eget http://ftp.somesite.ru/package-*.x64.tar
374
$ eget http://ftp.somesite.ru/package *.tar
375
$ eget https://github.com/owner/project package*.ext
376
$ eget -O myname ipfs://QmVRUjnsnxHWkjq91KreCpUk4D9oZEbMwNQ3rzdjwND5dR
377
$ eget --list http://ftp.somesite.ru/package-*.tar
378
$ eget --check http://ftp.somesite.ru/test
379
$ eget --list http://download.somesite.ru 'package-*.tar.xz'
380
$ eget --list --latest https://github.com/telegramdesktop/tdesktop/releases 'tsetup.*.tar.xz'
387
echo "eget - wget like downloader wrapper with wildcard support, uses wget or curl as backend" >&2
388
echo "Run $0 --help to get help" >&2
393
while [ -n "$1" ] ; do
406
-k|--no-check-certificate)
407
WGETNOSSLCHECK='--no-check-certificate'
409
AXELNOSSLCHECK='--insecure'
413
WGETHEADER="--header=$1"
414
CURLHEADER="--header $1"
415
AXELHEADER="--header=$1"
418
user_agent="Mozilla/5.0 (X11; Linux $arch) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
419
WGETUSERAGENT="-U '$user_agent'"
420
CURLUSERAGENT="-A '$user_agent'"
421
AXELUSERAGENT="--user-agent='$user_agent'"
424
CURLCOMPRESSED='--compressed'
425
WGETCOMPRESSED='--compression=auto'
427
-4|--ipv4|--inet4-only)
430
-6|--ipv6|--inet6-only)
470
fatal "Unknown option '$1', check eget --help."
480
#############################3
483
# https://github.com/ipfs/kubo/issues/5541
484
ipfs_diag_timeout='--timeout 60s'
486
ipfs_api_local="/ip4/127.0.0.1/tcp/5001"
487
[ -n "$EGET_IPFS_API" ] && ipfs_api_local="$EGET_IPFS_API"
489
ipfs_api_brave="/ip4/127.0.0.1/tcp/45005"
491
ipfs_gateway="https://cloudflare-ipfs.com/ipfs"
492
[ -n "$EGET_IPFS_GATEWAY" ] && ipfs_gateway="$EGET_IPFS_GATEWAY"
493
IPFS_GATEWAY="$ipfs_gateway"
495
# Test data: https://etersoft.ru/templates/etersoft/images/logo.png
496
ipfs_checkQm="QmYwf2GAMvHxfFiUFL2Mr6KUG6QrDiupqGc8ms785ktaYw"
500
local ipfs_brave="$(ls ~/.config/BraveSoftware/Brave-Browser/*/*/go-ipfs_* | sort | tail -n1 2>/dev/null)"
501
[ -n "$ipfs_brave" ] && [ -x "$ipfs_brave" ] || return
507
[ -n "$IPFS_CMD" ] || fatal "IPFS is disabled"
508
if [ -n "$verbose" ] ; then
509
verdocmd $IPFS_CMD --api $IPFS_API $ipfs_diag_timeout diag sys >/dev/null
511
verdocmd $IPFS_CMD --api $IPFS_API $ipfs_diag_timeout diag sys >/dev/null 2>/dev/null
517
[ -n "$IPFS_CMD" ] || fatal "IPFS is disabled"
518
verdocmd $IPFS_CMD --api $IPFS_API $ipfs_diag_timeout cat "$1" >/dev/null
525
IPFS_CMD="$(print_command_path ipfs)"
526
if [ -n "$IPFS_CMD" ] ; then
527
IPFS_API="$ipfs_api_local"
528
if ipfs_api_access ; then
529
ipfs_mode="local" && return
530
#if ipfs_check "$ipfs_checkQm" ; then
531
# ipfs_mode="local" && return
533
# info "Skipped local: it is accessible via $IPFS_CMD --api $IPFS_API, but can't return shared $ipfs_checkQm"
538
IPFS_CMD="$(get_ipfs_brave)"
539
# if no EGET_IPFS_API, check brave
540
if [ -z "$EGET_IPFS_API" ] && [ -n "$IPFS_CMD" ] ; then
541
IPFS_API="$ipfs_api_brave"
542
if ipfs_api_access ; then
543
ipfs_mode="brave" && return
544
#if ipfs_check "$ipfs_checkQm" ; then
545
# ipfs_mode="brave" && return
547
# info "Skipped Brave: it is accessible via $IPFS_CMD --api $IPFS_API, but can't return shared $ipfs_checkQm"
552
# TODO: check checksum
553
if docmd eget --check "$ipfs_gateway/$ipfs_checkQm" ; then
559
if docmd eget --check "$(dirname $ipfs_gateway)" ; then
560
info "IPFS gateway $ipfs_gateway is accessible, but can't return shared $ipfs_checkQm"
562
info "IPFS gateway $(dirname $ipfs_gateway) is not accessible"
569
# Functions for work with eget ipfs db
573
[ -r "$EGET_IPFS_DB" ] || return
574
is_fileurl "$URL" && return 1
575
grep -F "$URL Qm" "$EGET_IPFS_DB" | cut -f2 -d" " | grep -E "Qm[[:alnum:]]{44}" | head -n1
583
[ -w "$EGET_IPFS_DB" ] || return
585
is_fileurl "$URL" && return
587
echo "$URL $CID $FN" >> "$EGET_IPFS_DB"
588
info "Placed in $EGET_IPFS_DB: $URL $CID $FN"
594
[ -z "$EGET_IPFS_DB" ] && basename "$CID" && return
595
grep -F " $CID " "$EGET_IPFS_DB" | head -n1 | cut -f3 -d" "
601
[ -z "$EGET_IPFS_DB" ] && echo "$CID" && return
602
grep -F " $CID " "$EGET_IPFS_DB" | head -n1 | cut -f1 -d" "
608
ipfs_mode="$EGET_IPFS"
610
# enable auto mode when set $EGET_IPFS_DB
611
[ -z "$ipfs_mode" ] && [ -n "$EGET_IPFS_DB" ] && ipfs_mode="auto"
613
if [ -n "$LISTONLY$CHECKURL" ] ; then
619
if [ -n "$ipfs_mode" ] && [ -n "$EGET_IPFS_DB" ] ; then
620
ddb="$(dirname "$EGET_IPFS_DB")"
621
if [ -d "$ddb" ] ; then
622
info "Using eget IPFS db $EGET_IPFS_DB"
623
[ -r "$EGET_IPFS_DB" ] || touch "$EGET_IPFS_DB"
630
# detect if we run with ipfs:// or with auto
631
if is_ipfsurl "$1" && [ -z "$ipfs_mode" ] || [ "$ipfs_mode" = "auto" ] ; then
632
info "Autodetecting available IPFS relay..."
634
info "Auto selected IPFS mode: $ipfs_mode"
636
[ -n "$ipfs_mode" ] && info "IPFS mode: $ipfs_mode"
641
if [ "$ipfs_mode" = "disabled" ] ; then
645
fatal "IPFS is disabled"
650
fatal "IPFS is disabled"
655
fatal "IPFS is disabled"
659
elif [ "$ipfs_mode" = "brave" ] ; then
660
IPFS_CMD="$(get_ipfs_brave)" || fatal "Can't find ipfs command in Brave"
661
IPFS_PRETTY_CMD="~Brave-Browser/$(basename $IPFS_CMD)"
662
IPFS_API="$ipfs_api_brave"
663
ipfs_api_access || fatal "Can't access to Brave IPFS API (Brave browser is not running and IPFS is not activated?)"
664
info "Will use $IPFS_PRETTY_CMD --api $IPFS_API"
666
elif [ "$ipfs_mode" = "local" ] ; then
667
IPFS_CMD="$(print_command_path ipfs)" || fatal "Can't find ipfs command"
668
IPFS_PRETTY_CMD="$IPFS_CMD"
669
IPFS_API="$ipfs_api_local"
670
ipfs_api_access || fatal "Can't access to IPFS API (ipfs daemon is not running?)"
671
info "Will use $IPFS_PRETTY_CMD --api $IPFS_API"
673
elif [ "$ipfs_mode" = "gateway" ] ; then
674
info "Will use eget $IPFS_GATEWAY/HASH"
678
[ -n "$IPFS_GATEWAY" ] || fatal "ipfs http gateway is not set"
679
echo "$IPFS_GATEWAY/$1"
684
if [ -n "$2" ] ; then
685
docmd eget -O "$2" "$(ipfs_get_real_url "$1")"
687
docmd eget "$(ipfs_get_real_url "$1")"
699
info "IPFS put skipped when a gateway is used"
702
elif [ -z "$ipfs_mode" ] ; then
705
fatal "Unsupported eget ipfs mode $ipfs_mode"
708
if [ -n "$IPFS_CMD" ] ; then
717
[ -n "$IPFS_CMD" ] || fatal "ipfs api is not usable"
718
if [ -n "$2" ] ; then
719
showcmd $IPFS_PRETTY_CMD --api $IPFS_API get -o "$2" "$1"
720
$IPFS_CMD --api $IPFS_API get -o "$2" "$1"
722
showcmd $IPFS_PRETTY_CMD --api $IPFS_API get "$1"
723
$IPFS_CMD --api $IPFS_API get "$1"
729
[ -n "$IPFS_CMD" ] || fatal "ipfs api is not usable"
731
# detect if -q is used (will output Qm instead of addded Qm)
733
[ "$qu" = "-q" ] || qu=''
735
showcmd $IPFS_PRETTY_CMD --api $IPFS_API add "$@"
738
res="$($IPFS_CMD --api $IPFS_API add "$@")" || return
740
if [ -z "$qu" ] ; then
741
res="$(echo "$res" | grep "^added Qm")" || return
742
res="$(echo "$res" | cut -f2 -d" ")"
745
is_ipfs_hash "$res" && echo "$res" && return
746
fatal "Can't recognize $res IPFS hash"
751
[ -n "$IPFS_CMD" ] || fatal "ipfs api is not usable"
752
showcmd $IPFS_PRETTY_CMD --api $IPFS_API cat "$1"
753
$IPFS_CMD --api $IPFS_API cat "$1"
757
###############################
761
WGET="$(print_command_path wget)"
762
CURL="$(print_command_path curl)"
764
ORIG_EGET_BACKEND="$EGET_BACKEND"
766
if is_fileurl "$1" ; then
768
elif is_ipfsurl "$1" ; then
773
case "$EGET_BACKEND" in
777
[ -n "$WGET" ] || fatal "There are no wget in the system but you forced using it via EGET_BACKEND. Install it with $ epm install wget"
780
[ -n "$CURL" ] || fatal "There are no curl in the system but you forced using it via EGET_BACKEND. Install it with $ epm install curl"
783
[ -n "$WGET" ] && EGET_BACKEND="wget"
784
[ -z "$EGET_BACKEND" ] && [ -n "$CURL" ] && EGET_BACKEND="curl"
785
[ -n "$EGET_BACKEND" ] || fatal "There are no wget nor curl in the system. Install something with $ epm install wget"
788
fatal "Uknown EGET_BACKEND $EGET_BACKEND"
794
if [ "$EGET_BACKEND" = "file" ] ; then
796
# put remote content to stdout
800
cat "$(path_from_url "$URL")"
802
# download to default name of to $2
806
if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
809
elif [ -n "$2" ] ; then
810
cp -av "$(path_from_url "$URL")" "$2"
813
cp -av "$(path_from_url "$URL")" .
819
test -f "$(path_from_url "$URL")"
832
elif [ "$EGET_BACKEND" = "ipfs" ] ; then
834
# put remote content to stdout
838
ipfs_cat "$(cid_from_url "$URL")"
840
# download to default name of to $2
844
if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
847
elif [ -n "$2" ] ; then
848
ipfs_get "$(cid_from_url "$URL")" "$2"
852
local fn="$(url_print_filename_from_url "$URL")"
853
if [ -n "$fn" ] ; then
854
ipfs_get "$(cid_from_url "$URL")" "$fn"
858
ipfs_get "$(cid_from_url "$URL")"
865
scat "$URL" >/dev/null
868
url_print_filename_from_url()
871
local fn="$(echo "$URL" | sed -e 's|ipfs://.*\?filename=||')"
872
[ "$URL" != "$fn" ] && echo "$fn" && return
878
url_print_filename_from_url "$URL" && return
879
local CID="$(cid_from_url "$URL")"
880
get_filename_by_cid "$CID"
886
local CID="$(cid_from_url "$URL")"
887
# if we use gateway, return URL with gateway
888
ipfs_get_real_url "$URL" && return
889
get_url_by_cid "$CID"
893
elif [ "$EGET_BACKEND" = "wget" ] ; then
896
if [ -n "$WGETUSERAGENT" ] ; then
897
docmd $WGET $FORCEIPV $WGETQ $WGETCOMPRESSED $WGETHEADER $WGETNOSSLCHECK "$WGETUSERAGENT" "$@"
899
docmd $WGET $FORCEIPV $WGETQ $WGETCOMPRESSED $WGETHEADER $WGETNOSSLCHECK "$@"
903
# put remote content to stdout
907
download_with_mirroring __wget "$URL" -O-
909
# download to default name of to $2
913
if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
916
elif [ -n "$2" ] ; then
917
download_with_mirroring __wget "$URL" -O "$2"
920
# TODO: поддержка rsync для известных хостов?
921
# Не качать, если одинаковый размер и дата
923
# TODO: overwrite always
924
download_with_mirroring __wget "$URL" $WGETNAMEOPTIONS
931
answer="$(quiet=1 __wget --spider -S "$URL" 2>&1)"
932
# HTTP/1.1 405 Method Not Allowed
933
if echo "$answer" | grep -q "^ *HTTP/[12.]* 405" ; then
934
(quiet=1 __wget --start-pos=5000G -S "$URL" 2>&1)
941
elif [ "$EGET_BACKEND" = "curl" ] ; then
945
if [ -n "$CURLUSERAGENT" ] ; then
946
docmd $CURL $FORCEIPV --fail -L $CURLQ $CURLCOMPRESSED $CURLHEADER "$CURLUSERAGENT" $CURLNOSSLCHECK "$@"
948
docmd $CURL $FORCEIPV --fail -L $CURLQ $CURLCOMPRESSED $CURLHEADER $CURLNOSSLCHECK "$@"
951
# put remote content to stdout
955
download_with_mirroring __curl "$URL" --output -
957
# download to default name of to $2
962
if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
965
elif [ -n "$2" ] ; then
966
download_with_mirroring __curl "$URL" --output "$2"
970
download_with_mirroring __curl "$URL" $CURLNAMEOPTIONS
977
answer="$(quiet=1 __curl -LI "$URL" 2>&1)"
978
# HTTP/1.1 405 Method Not Allowed
979
if echo "$answer" | grep -q "^ *HTTP/[12.]* 405" ; then
980
(quiet=1 __curl -L -i -r0-0 "$URL" 2>&1)
987
fatal "Unknown EGET_BACKEND '$EGET_BACKEND', logical error."
991
# Common code for both wget and curl (http related)
992
if [ "$EGET_BACKEND" = "wget" ] || [ "$EGET_BACKEND" = "curl" ] ; then
997
url_get_response "$URL" | grep -i "^ *[[:alpha:]].*: " | sed -e 's|^ *||' -e 's|\r$||'
1003
url_get_response "$URL" | grep "HTTP/" | tail -n1 | grep -q -w "200\|404"
1010
url_get_headers "$URL" | grep -i "^ *$HEADER: " | sed -e "s|^ *$HEADER: ||i"
1017
! is_httpurl "$URL" && echo "$URL" && return
1019
# don't check location if we have made form of the URL
1020
[ -n "$MADEURL" ] && [ "$MADEURL" = "$URL" ] && echo "$URL" && return
1023
for loc in $(url_get_header "$URL" "Location" | tac | sed -e 's| .*||') ; do
1024
# hack for construct full url from related Location
1025
if echo "$loc" | grep -q "^/" ; then
1026
loc="$(concatenate_url_and_filename "$(get_host_only "$URL")" "$loc")"
1028
if ! is_strange_url "$loc" ; then
1041
! is_httpurl "$URL" && basename "$URL" && return
1043
# See https://www.cpcwood.com/blog/5-aws-s3-utf-8-content-disposition
1044
# https://www.rfc-editor.org/rfc/rfc6266
1045
local cd="$(url_get_header "$URL" "Content-Disposition")"
1046
if echo "$cd" | grep -qi "filename\*= *UTF-8" ; then
1047
#Content-Disposition: attachment; filename="unityhub-amd64-3.3.0.deb"; filename*=UTF-8''"unityhub-amd64-3.3.0.deb"
1048
echo "$cd" | sed -e "s|.*filename\*= *UTF-8''||i" -e 's|^"||' -e 's|";$||' -e 's|"$||'
1051
if echo "$cd" | grep -qi "filename=" ; then
1052
#Content-Disposition: attachment; filename=postman-linux-x64.tar.gz
1053
#content-disposition: attachment; filename="code-1.77.1-1680651749.el7.x86_64.rpm"
1054
echo "$cd" | sed -e 's|.*filename= *||i' -e 's|^"||' -e 's|";.*||' -e 's|"$||'
1058
basename "$(url_get_real_url "$URL")"
1064
if [ -n "$ipfs_mode" ] && [ -n "$EGET_IPFS_DB" ] && ! is_ipfsurl "$1" ; then
1070
#res="$(url_scat "$URL" | ipfs_put )" || return
1071
#res="$(echo "$res" | grep "^added Qm")" || return 1
1072
#CID="$(echo "$res" | cut -f2 -d" ")"
1073
# with -q to disable progress (mixed with download progress)
1074
res="$(url_scat "$URL" | ipfs_put -q)" || return
1075
is_ipfs_hash "$res" || return 1
1079
# put remote content to stdout
1085
# It is list only function. Don't save to IPFS
1090
local CID="$(get_cid_by_url "$URL")"
1091
if [ -n "$CID" ] ; then
1097
CID="$(download_to_ipfs "$URL")" || return
1099
ipfs_cat "$CID" || return
1101
local FN="$(url_get_filename "$URL")" || return
1103
put_cid_and_url "$URL" "$CID" "$FN"
1106
# download to default name of to $2
1112
if [ -n "$GETFILENAME" ] ; then
1117
local REALURL="$(get_real_url "$URL")" || return
1119
if [ -n "$GETREALURL" ] ; then
1125
if [ "$TARGET" = "/dev/stdout" ] || [ "$TARGET" = "-" ] ; then
1131
#if is_strange_url "$REALURL" ; then
1132
# info "Just download strange URL $REALURL, skipping IPFS"
1133
# url_sget "$REALURL" "$TARGET"
1137
local CID="$(get_cid_by_url "$REALURL")"
1138
if [ -n "$CID" ] ; then
1140
if [ -n "$GETIPFSCID" ] ; then
1145
if [ -n "$GETFILENAME" ] ; then
1146
get_filename_by_cid "$CID"
1150
if [ -n "$GETREALURL" ] ; then
1151
get_url_by_cid "$CID"
1155
if [ -z "$TARGET" ] ; then
1156
# TODO: in some cases we can get name from URL...
1157
TARGET="$(get_filename_by_cid "$CID")"
1158
if [ -z "$TARGET" ] ; then
1162
[ "$URL" = "$REALURL" ] && info "$URL -> $CID -> $TARGET" || info "$URL -> $REALURL -> $CID -> $TARGET"
1163
ipfs_get "$CID" "$TARGET" && return
1165
# fail get from IPFS, fallback
1166
url_sget "$REALURL" "$TARGET"
1171
# download and put to IPFS
1172
local FN="$(url_get_filename "$REALURL")" || return
1173
if [ -z "$TARGET" ] ; then
1177
if [ -n "$GETIPFSCID" ] ; then
1178
# add to IPFS and print out CID
1179
CID="$(ipfs_put --progress "$REALURL")" || return
1184
# download file and add to IPFS
1185
url_sget "$REALURL" "$TARGET" || return
1187
# don't do ipfs put when gateway is using
1188
[ "$ipfs_mode" = "gateway" ] && return
1190
CID="$(ipfs_put --progress "$TARGET")" || return
1192
put_cid_and_url "$REALURL" "$CID" "$FN"
1195
check_url_is_accessible()
1198
local REALURL="$(get_real_url "$URL")" || return
1199
local CID="$(get_cid_by_url "$REALURL")"
1200
if [ -n "$CID" ] ; then
1201
[ "$URL" = "$REALURL" ] && info "$URL -> $CID" || info "$URL -> $REALURL -> $CID"
1206
CID="$(download_to_ipfs "$REALURL")" || return
1208
local FN="$(url_get_filename "$REALURL")" || return
1209
ipfs_cat "$CID" >/dev/null || return
1210
put_cid_and_url "$REALURL" "$CID" "$FN"
1215
url_get_filename "$1"
1220
url_get_real_url "$1"
1231
if [ -n "$GETFILENAME" ] ; then
1236
if [ -n "$GETREALURL" ] ; then
1244
check_url_is_accessible()
1251
url_get_filename "$1"
1256
url_get_real_url "$1"
1264
# https://github.com/OWNER/PROJECT
1265
local owner="$(echo "$1" | sed -e "s|^https://github.com/||" -e "s|/.*||")" #"
1266
local project="$(echo "$1" | sed -e "s|^https://github.com/$owner/||" -e "s|/.*||")" #"
1267
[ -n "$owner" ] || fatal "Can't get owner from $1"
1268
[ -n "$project" ] || fatal "Can't get project from $1"
1269
local URL="https://api.github.com/repos/$owner/$project/releases"
1270
# api sometime returns unformatted json
1271
scat $URL | sed -e 's|,\(["{]\)|,\n\1|g' | \
1272
grep -i -o -E '"browser_download_url": *"https://.*"' | cut -d'"' -f4
1275
# drop file path from URL
1278
echo "$1/" | grep -Eo '(.*://[^/]+)'
1281
concatenate_url_and_filename()
1283
local url="$(echo "$1" | sed -e 's|/*$||' )"
1284
local fn="$(echo "$2" | sed -e 's|^/*||' )"
1288
# MADEURL filled with latest made URL as flag it is end form of URL
1297
fn="$(echo "$fn" | sed -e 's|^./||' -e 's|^/+||')"
1299
if is_fileurl "$url" ; then
1302
elif echo "$fn" | grep -q "^/" ; then
1303
# if there is file path from the root of the site
1304
url="$(get_host_only "$url")"
1305
elif echo "$url" | grep -q -v "/$" ; then
1306
# if there is no slash in the end of URL
1307
url="$(dirname "$url")"
1310
MADEURL="$(concatenate_url_and_filename "$url" "$fn")"
1316
if is_fileurl "$URL" ; then
1317
ls -1 "$(path_from_url "$URL")"
1321
# cat html, divide to lines by tags and cut off hrefs only
1322
scat $URL | sed -e 's|<|<\n|g' -e 's|data-file=|href=|g' -e "s|href=http|href=\"http|g" -e "s|>|\">|g" -e "s|'|\"|g" | \
1323
grep -i -o -E 'href="(.+)"' | cut -d'"' -f2
1327
if [ -n "$CHECKURL" ] ; then
1330
check_url_is_accessible "$URL"
1332
if [ -n "$verbose" ] ; then
1333
[ "$res" = "0" ] && echo "$URL is accessible via network" || echo "$URL is NOT accessible via network"
1338
if [ -n "$GETRESPONSE" ] ; then
1339
url_get_response "$1"
1344
# separate part for github downloads
1345
if echo "$1" | grep -q "^https://github.com/" && \
1346
echo "$1" | grep -q -v "/download/" && [ -n "$2" ] ; then
1349
if [ -n "$LISTONLY" ] ; then
1350
get_github_urls "$1" | filter_glob "$MASK" | filter_order
1355
for fn in $(get_github_urls "$1" | filter_glob "$MASK" | filter_order) ; do
1356
MADEURL="$fn" # mark it is the end form of the URL
1357
sget "$fn" "$TARGETFILE" || ERROR=1
1358
[ -n "$TARGETFILE" ] && [ "$ERROR" = "0" ] && break
1363
if is_ipfsurl "$1" ; then
1364
[ -n "$2" ] && fatal "too many args when ipfs://Qm... used: extra '$2' arg"
1365
sget "$1" "$TARGETFILE"
1369
# if mask is the second arg
1370
if [ -n "$2" ] ; then
1374
# do not support / at the end without separately specified mask
1375
if echo "$1" | grep -q "/$" ; then
1376
#fatal "Use http://example.com/e/* to download all files in dir"
1381
URL="$(dirname "$1")/"
1382
# wildcards allowed only in the last part of path
1383
MASK=$(basename "$1")
1388
# https://www.freeoffice.com/download.php?filename=freeoffice-2021-1062.x86_64.rpm
1389
if echo "$URL" | grep -q "[*]" ; then
1390
fatal "Error: there are globbing symbol (*) in $URL. It is allowed only for mask part"
1393
is_url "$MASK" && fatal "eget supports only one URL as argument"
1394
[ -n "$3" ] && fatal "too many args: extra '$3'. May be you need use quotes for arg with wildcards."
1397
# If ftp protocol, just download
1398
if echo "$URL" | grep -q "^ftp://" ; then
1399
[ -n "$LISTONLY" ] && fatal "TODO: list files for ftp:// is not supported yet"
1400
sget "$1" "$TARGETFILE"
1405
if [ -n "$LISTONLY" ] ; then
1406
for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
1407
is_url "$fn" && echo "$fn" && continue
1408
make_fileurl "$URL" "$fn"
1413
# If there is no wildcard symbol like asterisk, just download
1414
if echo "$MASK" | grep -qv "[*?]" || echo "$MASK" | grep -q "[?].*="; then
1415
sget "$1" "$TARGETFILE"
1420
for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
1421
is_url "$fn" || fn="$(make_fileurl "$URL" "$fn" )" #"
1422
sget "$fn" "$TARGETFILE" || ERROR=1
1423
[ -n "$TARGETFILE" ] && [ "$ERROR" = "0" ] && break