mirror of
https://github.com/ipfs/kubo.git
synced 2025-10-15 02:35:03 +08:00
122 lines
3.2 KiB
Go
122 lines
3.2 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
|
|
filestore "github.com/ipfs/go-ipfs/filestore"
|
|
|
|
balanced "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/importer/balanced"
|
|
ihelper "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/importer/helpers"
|
|
trickle "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/importer/trickle"
|
|
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
|
|
cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
|
|
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
|
|
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
|
|
chunk "gx/ipfs/QmdSeG9s4EQ9TGruJJS9Us38TQDZtMmFGwzTYUDVqNTURm/go-ipfs-chunker"
|
|
)
|
|
|
|
var urlStoreCmd = &cmds.Command{
|
|
Subcommands: map[string]*cmds.Command{
|
|
"add": urlAdd,
|
|
},
|
|
}
|
|
|
|
var urlAdd = &cmds.Command{
|
|
Helptext: cmdkit.HelpText{
|
|
Tagline: "Add URL via urlstore.",
|
|
LongDescription: `
|
|
Add URLs to ipfs without storing the data locally.
|
|
|
|
The URL provided must be stable and ideally on a web server under your
|
|
control.
|
|
|
|
The file is added using raw-leaves but otherwise using the default
|
|
settings for 'ipfs add'.
|
|
|
|
The file is not pinned, so this command should be followed by an 'ipfs
|
|
pin add'.
|
|
|
|
This command is considered temporary until a better solution can be
|
|
found. It may disappear or the semantics can change at any
|
|
time.
|
|
`,
|
|
},
|
|
Options: []cmdkit.Option{
|
|
cmdkit.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
|
|
},
|
|
Arguments: []cmdkit.Argument{
|
|
cmdkit.StringArg("url", true, false, "URL to add to IPFS"),
|
|
},
|
|
Type: &BlockStat{},
|
|
|
|
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
|
url := req.Arguments[0]
|
|
n, err := cmdenv.GetNode(env)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !filestore.IsURL(url) {
|
|
return fmt.Errorf("unsupported url syntax: %s", url)
|
|
}
|
|
|
|
cfg, err := n.Repo.Config()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !cfg.Experimental.UrlstoreEnabled {
|
|
return filestore.ErrUrlstoreNotEnabled
|
|
}
|
|
|
|
useTrickledag, _ := req.Options[trickleOptionName].(bool)
|
|
|
|
hreq, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
hres, err := http.DefaultClient.Do(hreq)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if hres.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("expected code 200, got: %d", hres.StatusCode)
|
|
}
|
|
|
|
chk := chunk.NewSizeSplitter(hres.Body, chunk.DefaultBlockSize)
|
|
prefix := cid.NewPrefixV1(cid.DagProtobuf, mh.SHA2_256)
|
|
dbp := &ihelper.DagBuilderParams{
|
|
Dagserv: n.DAG,
|
|
RawLeaves: true,
|
|
Maxlinks: ihelper.DefaultLinksPerBlock,
|
|
NoCopy: true,
|
|
CidBuilder: &prefix,
|
|
URL: url,
|
|
}
|
|
|
|
layout := balanced.Layout
|
|
if useTrickledag {
|
|
layout = trickle.Layout
|
|
}
|
|
root, err := layout(dbp.New(chk))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return cmds.EmitOnce(res, &BlockStat{
|
|
Key: root.Cid().String(),
|
|
Size: int(hres.ContentLength),
|
|
})
|
|
},
|
|
Encoders: cmds.EncoderMap{
|
|
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
|
|
_, err := fmt.Fprintln(w, bs.Key)
|
|
return err
|
|
}),
|
|
},
|
|
}
|