// Add a file to ipfs from the given reader, returns the hash of the added file func (s *Shell) Add(r io.Reader) (string, error) { var rc io.ReadCloser if rclose, ok := r.(io.ReadCloser); ok { rc = rclose } else { rc = ioutil.NopCloser(r) } // handler expects an array of files fr := files.NewReaderFile("", "", rc, nil) slf := files.NewSliceFile("", "", []files.File{fr}) fileReader := files.NewMultiFileReader(slf, true) req := NewRequest(s.url, "add") req.Body = fileReader resp, err := req.Send(s.httpcli) if err != nil { return "", err } defer resp.Close() if resp.Error != nil { return "", resp.Error } var out object err = json.NewDecoder(resp.Output).Decode(&out) if err != nil { return "", err } return out.Hash, nil }
func (s *Shell) AddLink(target string) (string, error) { link := files.NewLinkFile("", "", target, nil) slf := files.NewSliceFile("", "", []files.File{link}) reader := files.NewMultiFileReader(slf, true) req := s.newRequest("add") req.Body = reader resp, err := req.Send(s.httpcli) if err != nil { return "", err } defer resp.Close() if resp.Error != nil { return "", resp.Error } var out object err = json.NewDecoder(resp.Output).Decode(&out) if err != nil { return "", err } return out.Hash, nil }
// AddDir adds a directory recursively with all of the files under it func (s *Shell) AddDir(dir string) (string, error) { stat, err := os.Lstat(dir) if err != nil { return "", err } sf, err := files.NewSerialFile("", dir, stat) if err != nil { return "", err } slf := files.NewSliceFile("", dir, []files.File{sf}) reader := files.NewMultiFileReader(slf, true) req := NewRequest(s.url, "add") req.Opts["r"] = "true" req.Body = reader resp, err := req.Send(s.httpcli) if err != nil { return "", err } defer resp.Close() if resp.Error != nil { return "", resp.Error } dec := json.NewDecoder(resp.Output) var final string for { var out object err = dec.Decode(&out) if err != nil { if err == io.EOF { break } return "", err } final = out.Hash } if final == "" { return "", errors.New("no results received") } return final, nil }