debian-mirror-gitlab/workhorse/internal/gitaly/blob.go

42 lines
861 B
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package gitaly
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
2021-09-04 01:27:46 +05:30
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/v14/streamio"
2021-02-22 17:27:13 +05:30
)
type BlobClient struct {
gitalypb.BlobServiceClient
}
func (client *BlobClient) SendBlob(ctx context.Context, w http.ResponseWriter, request *gitalypb.GetBlobRequest) error {
c, err := client.GetBlob(ctx, request)
if err != nil {
return fmt.Errorf("rpc failed: %v", err)
}
firstResponseReceived := false
rr := streamio.NewReader(func() ([]byte, error) {
resp, err := c.Recv()
if !firstResponseReceived && err == nil {
firstResponseReceived = true
w.Header().Set("Content-Length", strconv.FormatInt(resp.GetSize(), 10))
}
return resp.GetData(), err
})
if _, err := io.Copy(w, rr); err != nil {
return fmt.Errorf("copy rpc data: %v", err)
}
return nil
}