Commit eb92eee1 authored by Vitaly Lipatov's avatar Vitaly Lipatov

eget: rewrite download for IPFS

parent a0aff9ad
...@@ -622,8 +622,23 @@ ipfs_get() ...@@ -622,8 +622,23 @@ ipfs_get()
ipfs_put() ipfs_put()
{ {
[ -n "$IPFS_CMD" ] || fatal "ipfs api is not usable" [ -n "$IPFS_CMD" ] || fatal "ipfs api is not usable"
# detect if -q is used (will output Qm instead of addded Qm)
local qu="$1"
[ "$qu" = "-q" ] || qu=''
showcmd $IPFS_PRETTY_CMD --api $IPFS_API add "$@" showcmd $IPFS_PRETTY_CMD --api $IPFS_API add "$@"
$IPFS_CMD --api $IPFS_API add "$@"
local res
res="$($IPFS_CMD --api $IPFS_API add "$@")" || return
if [ -z "$qu" ] ; then
res="$(echo "$res" | grep "^added Qm")" || return
res="$(echo "$res" | cut -f2 -d" ")"
fi
is_ipfs_hash "$res" && echo "$res" && return
fatal "Can't recognize $res IPFS hash"
} }
ipfs_cat() ipfs_cat()
...@@ -996,30 +1011,39 @@ sget() ...@@ -996,30 +1011,39 @@ sget()
fi fi
if [ -z "$TARGET" ] ; then if [ -z "$TARGET" ] ; then
# TODO: in some cases we can get name from URL...
TARGET="$(get_filename_by_cid "$CID")" TARGET="$(get_filename_by_cid "$CID")"
if [ -z "$TARGET" ] ; then if [ -z "$TARGET" ] ; then
TARGET="$CID" TARGET="$CID"
fi fi
fi fi
[ "$URL" = "$REALURL" ] && info "$URL -> $CID -> $TARGET" || info "$URL -> $REALURL -> $CID -> $TARGET" [ "$URL" = "$REALURL" ] && info "$URL -> $CID -> $TARGET" || info "$URL -> $REALURL -> $CID -> $TARGET"
ipfs_get "$CID" "$TARGET" ipfs_get "$CID" "$TARGET" && return
# fail get from IPFS, fallback
url_sget "$REALURL" "$TARGET"
return return
fi fi
# download and put to IPFS
local FN="$(url_get_filename "$REALURL")" || return local FN="$(url_get_filename "$REALURL")" || return
if [ -z "$TARGET" ] ; then if [ -z "$TARGET" ] ; then
TARGET="$FN" TARGET="$FN"
fi fi
CID="$(download_to_ipfs "$REALURL")" || return
if [ -n "$GETIPFSCID" ] ; then if [ -n "$GETIPFSCID" ] ; then
put_cid_and_url "$REALURL" "$CID" "$FN" # add to IPFS and print out CID
CID="$(ipfs_put "$REALURL")" || return
echo "$CID" echo "$CID"
exit exit
fi fi
ipfs_get "$CID" "$TARGET" || return # download file and add to IPFS
url_sget "$REALURL" "$TARGET" || return
CID="$(ipfs_put "$TARGET")" || return
is_ipfs_hash "$CID" || return
put_cid_and_url "$REALURL" "$CID" "$FN" put_cid_and_url "$REALURL" "$CID" "$FN"
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment