Exemplo n.º 1
0
func (c *Conn) StoreBlob(file *os.File) (string, error) {
	reader := hashed.NewSha1FileReader(file)

	info, err := file.Stat()
	if err != nil {
		return "", err
	}
	size := info.Size()

	if err := c.WriteJSONFrame(request{"store", "", size, false}); err != nil {
		return "", err
	}

	if err := c.WriteBlobFrameFrom(reader, size); err != nil {
		return "", err
	}

	key := util.HexHash(reader)
	if err := c.WriteJSONFrame(request{Key: key}); err != nil {
		return "", err
	}

	var res response
	if err := c.ReadJSONFrame(&res); err != nil {
		return "", err
	}

	if res.Val != "ok" {
		return "", responseError(res)
	}

	return key, nil
}
Exemplo n.º 2
0
func (c *Conn) handleStore(req request) error {
	log.Info.Printf("Store request from %s", c.sock.RemoteAddr())

	var (
		blob *store.Blob
		err  error
	)

	if req.Size == 0 {
		blob, err = c.store.NewBlob()
	} else {
		blob, err = c.store.NewBlobSize(req.Size)
	}
	if err != nil {
		return err
	}

	abort := true
	defer func() {
		if abort {
			blob.Abort()
		}
	}()

	if err := c.ReadBlobFrameTo(blob); err != nil {
		return err
	}

	var key string
	if req.Key != "" {
		key = req.Key
	} else {
		var keyReq request
		if err := c.ReadJSONFrame(&keyReq); err != nil {
			return err
		}
		key = keyReq.Key
	}

	blobKey := util.HexHash(blob)
	if key != blobKey {
		c.respondIncorrectKey()
		return nil
	}

	if _, err := blob.Store(req.Sync); err != nil {
		return err
	}
	log.Info.Printf("Blob %s stored by %s", blobKey, c.sock.RemoteAddr())

	abort = false
	c.respondOK()
	return nil
}