itd/api/resources.go

52 lines
1.2 KiB
Go
Raw Normal View History

2022-08-30 19:13:22 +00:00
package api
import (
"context"
"go.arsenm.dev/infinitime"
2023-01-03 06:30:17 +00:00
"go.arsenm.dev/itd/internal/rpc"
2022-08-30 19:13:22 +00:00
)
2023-01-03 06:30:17 +00:00
type ResourceOperation uint8
const (
ResourceOperationRemoveObsolete = infinitime.ResourceOperationRemoveObsolete
ResourceOperationUpload = infinitime.ResourceOperationUpload
)
type ResourceLoadProgress struct {
Operation ResourceOperation
Name string
Total int64
Sent int64
Err error
}
2022-08-30 19:13:22 +00:00
// LoadResources loads resources onto the watch from the given
// file path to the resources zip
2023-01-03 06:30:17 +00:00
func (c *FSClient) LoadResources(ctx context.Context, path string) (<-chan ResourceLoadProgress, error) {
progCh := make(chan ResourceLoadProgress, 2)
rc, err := c.client.LoadResources(ctx, &rpc.PathRequest{Path: path})
2022-08-30 19:13:22 +00:00
if err != nil {
return nil, err
}
2023-01-03 06:30:17 +00:00
go fsRecvToChannel[rpc.ResourceLoadProgress](rc, progCh, func(evt *rpc.ResourceLoadProgress, err error) ResourceLoadProgress {
return ResourceLoadProgress{
Operation: ResourceOperation(evt.Operation),
Name: evt.Name,
Sent: evt.Sent,
Total: evt.Total,
Err: err,
}
})
2022-08-30 19:13:22 +00:00
return progCh, nil
}
2023-01-03 06:30:17 +00:00
type StreamClient[T any] interface {
Recv() (*T, error)
Context() context.Context
}