blob: a8492a3c93ab224d52cd9475be04863bb894dbf2 [file] [log] [blame]
giolekva892a4e22020-04-27 16:46:22 +04001package client
2
3import (
4 "context"
5 "os"
6
7 "github.com/giolekva/pcloud/pfs/api"
8 "github.com/giolekva/pcloud/pfs/chunk"
9)
10
11type FileUploader struct {
12 client api.MetadataStorageClient
13}
14
15func NewFileUploader(client api.MetadataStorageClient) *FileUploader {
16 return &FileUploader{client}
17}
18
19func (fu *FileUploader) Upload(f *os.File, numReplicas int) {
20 info, err := f.Stat()
21 if err != nil {
22 return
23 }
24 resp, err := fu.client.CreateBlob(
25 context.Background(), &api.CreateBlobRequest{
26 SizeBytes: int32(info.Size()),
27 NumReplicas: int32(numReplicas)})
28 if err != nil {
29 panic(err)
30 }
31 if len(resp.Chunk) != 1 {
32 panic(resp)
33 }
34 lis := &chunk.NonChangingReplicaAssignment{}
35 primaryAddressCh := lis.Primary(
36 resp.Chunk[0].ChunkId,
37 resp.Chunk[0].Server[0])
38 chunk.WriteToPrimary(
39 context.Background(),
40 resp.Chunk[0].ChunkId,
41 chunk.NewReadOnlyFileChunk(f, 0, int(info.Size())),
42 primaryAddressCh)
43}