133 lines
3.4 KiB
Go
133 lines
3.4 KiB
Go
package objectstorage
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"sync"
|
|
)
|
|
|
|
type ReloadableClient struct {
|
|
mu sync.RWMutex
|
|
current Client
|
|
}
|
|
|
|
func NewReloadableClient(current Client) *ReloadableClient {
|
|
return &ReloadableClient{current: current}
|
|
}
|
|
|
|
func (c *ReloadableClient) Update(next Client) {
|
|
if c == nil || next == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.current = next
|
|
}
|
|
|
|
func (c *ReloadableClient) Validate() error {
|
|
return c.snapshot().Validate()
|
|
}
|
|
|
|
func (c *ReloadableClient) PutBytes(ctx context.Context, objectKey string, content []byte, contentType string) error {
|
|
return c.snapshot().PutBytes(ctx, objectKey, content, contentType)
|
|
}
|
|
|
|
func (c *ReloadableClient) PutReader(ctx context.Context, objectKey string, reader io.Reader, size int64, contentType string) error {
|
|
client := c.snapshot()
|
|
readerClient, ok := client.(ReaderClient)
|
|
if !ok {
|
|
content, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return client.PutBytes(ctx, objectKey, content, contentType)
|
|
}
|
|
return readerClient.PutReader(ctx, objectKey, reader, size, contentType)
|
|
}
|
|
|
|
func (c *ReloadableClient) GetBytes(ctx context.Context, objectKey string) ([]byte, error) {
|
|
return c.snapshot().GetBytes(ctx, objectKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) GetReader(ctx context.Context, objectKey string) (io.ReadCloser, error) {
|
|
client := c.snapshot()
|
|
readerClient, ok := client.(ObjectReaderClient)
|
|
if !ok {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
return readerClient.GetReader(ctx, objectKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) Exists(ctx context.Context, objectKey string) (bool, error) {
|
|
return c.snapshot().Exists(ctx, objectKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) Delete(ctx context.Context, objectKey string) error {
|
|
return c.snapshot().Delete(ctx, objectKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) PublicURL(objectKey string) (string, error) {
|
|
return c.snapshot().PublicURL(objectKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) DownloadURL(objectKey string) (string, error) {
|
|
return c.snapshot().DownloadURL(objectKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) List(ctx context.Context, in ListInput) (*ListResult, error) {
|
|
client := c.snapshot()
|
|
management, ok := client.(ManagementClient)
|
|
if !ok {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
return management.List(ctx, in)
|
|
}
|
|
|
|
func (c *ReloadableClient) Stat(ctx context.Context, objectKey string) (*ObjectInfo, error) {
|
|
client := c.snapshot()
|
|
management, ok := client.(ManagementClient)
|
|
if !ok {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
return management.Stat(ctx, objectKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) Copy(ctx context.Context, sourceKey, destinationKey string) error {
|
|
client := c.snapshot()
|
|
management, ok := client.(ManagementClient)
|
|
if !ok {
|
|
return ErrNotConfigured
|
|
}
|
|
return management.Copy(ctx, sourceKey, destinationKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) Usage(ctx context.Context) (*UsageInfo, error) {
|
|
client := c.snapshot()
|
|
usageClient, ok := client.(UsageClient)
|
|
if !ok {
|
|
return nil, ErrUsageUnsupported
|
|
}
|
|
return usageClient.Usage(ctx)
|
|
}
|
|
|
|
func (c *ReloadableClient) PublicReadableURL(objectKey string) (string, error) {
|
|
client := c.snapshot()
|
|
publicClient, ok := client.(PublicReadableClient)
|
|
if !ok {
|
|
return "", ErrObjectNotPublic
|
|
}
|
|
return publicClient.PublicReadableURL(objectKey)
|
|
}
|
|
|
|
func (c *ReloadableClient) snapshot() Client {
|
|
if c == nil {
|
|
return disabledClient{reason: "object storage client is nil"}
|
|
}
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
if c.current == nil {
|
|
return disabledClient{reason: "object storage client is not initialized"}
|
|
}
|
|
return c.current
|
|
}
|