file-store/pkg/storeserver/server.go

108 lines
2.6 KiB
Go
Raw Normal View History

package storeserver
import (
"bytes"
"context"
"fmt"
"io"
"google.golang.org/grpc"
"git.keganmyers.com/terribleplan/file-store/pkg/proto"
)
var _ = (proto.StoreServerServer)((*protoStoreServer)(nil))
// proto.StoreServerServer
func NewProto(s StoreServer, gs *grpc.Server) {
// return
proto.RegisterStoreServerServer(gs, &protoStoreServer{s: s})
return
}
type protoStoreServer struct {
s StoreServer
proto.UnimplementedStoreServerServer
}
// files
func (p *protoStoreServer) DeleteFile(context.Context, *proto.FileIdentifier) (*proto.Empty, error) {
return nil, fmt.Errorf("todo")
}
func (p *protoStoreServer) ListFiles(_ *proto.Empty, lfs proto.StoreServer_ListFilesServer) error {
f := proto.FileIdentifier{}
return p.s.ListFiles(func(fileId string) error {
f.FileId = fileId
return lfs.Send(&f)
})
}
func (p *protoStoreServer) ReadFile(context.Context, *proto.FileIdentifier) (*proto.StoreFileMeta, error) {
return nil, fmt.Errorf("todo")
}
func (p *protoStoreServer) WriteFile(_ context.Context, f *proto.StoreFileMeta) (*proto.Empty, error) {
return &proto.Empty{}, p.s.WriteFile(f.FileIdentifier.FileId, f.MetaJson)
// return nil, fmt.Errorf("todo")
}
// chunks
func (p *protoStoreServer) DeleteChunk(context.Context, *proto.ChunkIdentifier) (*proto.Empty, error) {
return nil, fmt.Errorf("todo")
}
func (p *protoStoreServer) DescribeChunk(context.Context, *proto.ChunkIdentifier) (*proto.StoreChunkMeta, error) {
return nil, fmt.Errorf("todo")
}
func (p *protoStoreServer) ListChunks(*proto.Empty, proto.StoreServer_ListChunksServer) error {
return fmt.Errorf("todo")
}
func (p *protoStoreServer) ReadChunk(context.Context, *proto.ChunkDataIdentifier) (*proto.ChunkData, error) {
return nil, fmt.Errorf("todo")
}
type serverChunkReader struct {
buffer *bytes.Buffer
stream proto.StoreServer_WriteChunkServer
err error
}
// todo
func (r *serverChunkReader) Read(p []byte) (int, error) {
for r.buffer.Len() < len(p) && r.err == nil {
chunk, err := r.stream.Recv()
if chunk != nil {
r.buffer.Write(chunk.Data.Data)
}
if err != nil {
r.err = err
break
}
}
if r.err != nil && r.err != io.EOF {
return 0, r.err
}
return r.buffer.Read(p)
}
// todo
func (r *serverChunkReader) Close() error {
return nil
}
func (p *protoStoreServer) WriteChunk(stream proto.StoreServer_WriteChunkServer) error {
chunk, err := stream.Recv()
if err != nil {
return err
}
buf := &bytes.Buffer{}
buf.Write(chunk.Data.Data)
p.s.WriteChunk(chunk.DataIdentifier.Chunk.FileIdentifier.FileId, uint16(chunk.DataIdentifier.Chunk.ChunkId), &serverChunkReader{buffer: buf, stream: stream})
return fmt.Errorf("todo")
}