Exemplo n.º 1
0
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))
		}
	})
}
Exemplo n.º 2
0
// Stream opens a reader that yields the raw data of the file,
// already transparently decompressed and decrypted.
func (f *File) Stream(ipfs *ipfsutil.Node) (ipfsutil.Reader, error) {
	log.Debugf(
		"Stream `%s` (hash: %s) (key: %x)",
		NodePath(f),
		f.hash.B58String(),
		f.key,
	)

	ipfsStream, err := ipfsutil.Cat(ipfs, f.hash.Multihash)
	if err != nil {
		return nil, err
	}

	return NewIpfsReader(f.key, ipfsStream)
}