-
Notifications
You must be signed in to change notification settings - Fork 41
/
server.go
62 lines (56 loc) · 1.41 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Package server implement a file upload server.
package server
import (
"io"
"sync"
"github.com/pkg/errors"
pb "go.viam.com/utils/proto/rpc/examples/fileupload/v1"
)
// Server implements a simple file upload service.
type Server struct {
mu sync.Mutex
pb.UnimplementedFileUploadServiceServer
fail bool
}
// SetFail instructs the server to fail at certain points in its execution.
func (srv *Server) SetFail(fail bool) {
srv.mu.Lock()
srv.fail = fail
srv.mu.Unlock()
}
// UploadFile receives a file over a series of chunks.
func (srv *Server) UploadFile(server pb.FileUploadService_UploadFileServer) error {
var haveName bool
var fileName string
var data []byte
for {
select {
case <-server.Context().Done():
return server.Context().Err()
default:
}
req, err := server.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
// at this point, you can do something with the file
return server.Send(&pb.UploadFileResponse{Name: fileName, Size: int64(len(data))})
}
return err
}
switch d := req.Data.(type) {
case (*pb.UploadFileRequest_Name):
if haveName {
return errors.New("received name more than once")
}
haveName = true
fileName = d.Name
case (*pb.UploadFileRequest_ChunkData):
if !haveName {
return errors.New("first provide file name")
}
data = append(data, d.ChunkData...)
default:
return errors.Errorf("unknown data type %T", req.Data)
}
}
}