feat: add file saving

This commit is contained in:
Vyacheslav1557 2024-08-21 03:01:06 +05:00
parent bebc7f3076
commit eb1a2e6df7

View file

@ -9,6 +9,7 @@ import (
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/emptypb"
"io" "io"
"os"
) )
func (s *TesterServer) CreateProblem(server problemv1.ProblemService_CreateProblemServer) error { 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) 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 { if err != nil {
return status.Errorf(codes.Unknown, "") // FIXME return status.Errorf(codes.Unknown, "") // FIXME
} }
@ -51,6 +57,28 @@ func (s *TesterServer) CreateProblem(server problemv1.ProblemService_CreateProbl
return nil 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 { func readChunks(ctx context.Context, server problemv1.ProblemService_CreateProblemServer) <-chan []byte {
ch := make(chan []byte) ch := make(chan []byte)