func TestAddCat(t *testing.T) { testwith.WithIpfs(t, func(node *ipfsutil.Node) { // Dummy in-memory reader: origData := []byte("Hello World") buf := &bytes.Buffer{} buf.Write(origData) hash, err := ipfsutil.Add(node, buf) if err != nil { t.Errorf("Add of a simple file failed: %v", err) return } reader, err := ipfsutil.Cat(node, hash) if err != nil { t.Errorf("Could not cat simple file: %v", err) return } data, err := ioutil.ReadAll(reader) if err != nil { t.Errorf("Could not read back added data: %v", err) return } if err = reader.Close(); err != nil { t.Errorf("close(cat) failed: %v", err) return } if !bytes.Equal(data, origData) { t.Errorf("Data not equal: %v <- -> %v", string(data), string(origData)) } }) }
// StageFromReader reads data from r, encrypts & compresses it while feeding it to ipfs. // The resulting hash will be committed to the index. func (st *Store) StageFromReader(repoPath string, r io.Reader, compressAlgo compress.AlgorithmType) error { repoPath = prefixSlash(repoPath) st.mu.Lock() defer st.mu.Unlock() // Control how many bytes are written to the encryption layer: sizeAcc := &util.SizeAccumulator{} teeR := io.TeeReader(r, sizeAcc) key := make([]byte, 32) if _, err := io.ReadFull(rand.Reader, key); err != nil { return err } // TODO: Make algo configurable/add heuristic too choose // a suitable algorithm stream, err := NewFileReader(key, teeR, compressAlgo) if err != nil { return err } mhash, err := ipfsutil.Add(st.IPFS, stream) if err != nil { return err } if err := st.IPFS.Pin(mhash); err != nil { return err } owner, err := st.Owner() if err != nil { return err } if _, err := stageFile(st.fs, repoPath, &Hash{mhash}, key, sizeAcc.Size(), owner.ID()); err != nil { return err } return nil }