feat: add file saving
This commit is contained in:
parent
bebc7f3076
commit
eb1a2e6df7
1 changed files with 29 additions and 1 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func (s *TesterServer) CreateProblem(server problemv1.ProblemService_CreateProblemServer) error {
|
||||
|
@ -36,7 +37,12 @@ func (s *TesterServer) CreateProblem(server problemv1.ProblemService_CreateProbl
|
|||
|
||||
ch := readChunks(ctx, server)
|
||||
|
||||
id, err := s.problemService.CreateProblem(ctx, p, ch)
|
||||
err = writeChunks(ctx, ch) // temp stub
|
||||
if err != nil {
|
||||
return err // FIXME
|
||||
}
|
||||
|
||||
id, err := s.problemService.CreateProblem(ctx, p, nil) // FIXME
|
||||
if err != nil {
|
||||
return status.Errorf(codes.Unknown, "") // FIXME
|
||||
}
|
||||
|
@ -51,6 +57,28 @@ func (s *TesterServer) CreateProblem(server problemv1.ProblemService_CreateProbl
|
|||
return nil
|
||||
}
|
||||
|
||||
func writeChunks(ctx context.Context, chunks <-chan []byte) error {
|
||||
// FIXME: use ctx?
|
||||
f, err := os.Create("out.txt") // FIXME: uuidv4 as initial temp name?
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var off int64 = 0
|
||||
for chunk := range chunks {
|
||||
_, err = f.WriteAt(chunk, off)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
off += int64(len(chunk))
|
||||
}
|
||||
|
||||
// TODO: rename file to its hash
|
||||
return nil
|
||||
}
|
||||
|
||||
func readChunks(ctx context.Context, server problemv1.ProblemService_CreateProblemServer) <-chan []byte {
|
||||
ch := make(chan []byte)
|
||||
|
||||
|
|
Loading…
Reference in a new issue