From 103efa8b1c0e1b2191f183f077e8618e2562223d Mon Sep 17 00:00:00 2001 From: pablu Date: Thu, 6 Aug 2026 19:47:26 +0200 Subject: feat: update client to new v0.1.1 --- client.go | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) (limited to 'client.go') diff --git a/client.go b/client.go index f799f07..bc155ee 100644 --- a/client.go +++ b/client.go @@ -154,6 +154,36 @@ func (c *Client) DeleteFile(ctx context.Context, logicalPath string) error { return nil } +func (c *Client) MoveFile( + ctx context.Context, + sourcePath string, + destinationPath string, +) error { + _, err := c.rpc.MoveFile(ctx, &zwdaemonv1.MoveFileRequest{ + SourcePath: sourcePath, + DestinationPath: destinationPath, + }) + if err != nil { + return convertRPCError(err) + } + return nil +} + +func (c *Client) CopyFile( + ctx context.Context, + sourcePath string, + destinationPath string, +) error { + _, err := c.rpc.CopyFile(ctx, &zwdaemonv1.CopyFileRequest{ + SourcePath: sourcePath, + DestinationPath: destinationPath, + }) + if err != nil { + return convertRPCError(err) + } + return nil +} + func (c *Client) Compact(ctx context.Context) error { _, err := c.rpc.Compact(ctx, &zwdaemonv1.CompactRequest{}) if err != nil { @@ -291,3 +321,77 @@ func (c *Client) AddFile( } } } + +func (c *Client) ReplaceFile( + ctx context.Context, + logicalPath string, + source io.Reader, + sourceLen uint64, +) error { + uploadCtx, cancel := context.WithCancel(ctx) + defer cancel() + + stream, err := c.rpc.ReplaceFile(uploadCtx) + if err != nil { + return convertRPCError(err) + } + + send := func(request *zwdaemonv1.ReplaceFileRequest) error { + err := stream.Send(request) + if err == nil { + return nil + } + + if errors.Is(err, io.EOF) { + _, receiveErr := stream.CloseAndRecv() + if receiveErr != nil { + return convertRPCError(receiveErr) + } + } + + return convertRPCError(err) + } + + err = send(&zwdaemonv1.ReplaceFileRequest{ + Payload: &zwdaemonv1.ReplaceFileRequest_Header{ + Header: &zwdaemonv1.ReplaceFileHeader{ + LogicalPath: logicalPath, + SourceLen: sourceLen, + }, + }, + }) + if err != nil { + return err + } + + const chunkSize = 512 * 1024 + buffer := make([]byte, chunkSize) + + for { + n, readErr := source.Read(buffer) + + if n > 0 { + err := send(&zwdaemonv1.ReplaceFileRequest{ + Payload: &zwdaemonv1.ReplaceFileRequest_Chunk{ + Chunk: buffer[:n], + }, + }) + if err != nil { + return err + } + } + + switch { + case readErr == nil: + continue + case errors.Is(readErr, io.EOF): + _, err := stream.CloseAndRecv() + if err != nil { + return convertRPCError(err) + } + return nil + default: + return &UploadReadError{Err: readErr} + } + } +} -- cgit 1.3.1