69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"sync"
|
|
|
|
"kra/internal/conf"
|
|
"kra/internal/modules/system/biz"
|
|
)
|
|
|
|
type Reloadable struct {
|
|
mu sync.RWMutex
|
|
current biz.FileStorage
|
|
}
|
|
|
|
func NewFileStorage(runtime *conf.Runtime) (*Reloadable, error) {
|
|
var config *conf.AdminBackend
|
|
if runtime != nil {
|
|
config = runtime.Admin()
|
|
}
|
|
return NewReloadable(config)
|
|
}
|
|
|
|
func NewReloadable(config *conf.AdminBackend) (*Reloadable, error) {
|
|
current, err := New(config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Reloadable{current: current}, nil
|
|
}
|
|
|
|
func (s *Reloadable) Replace(current biz.FileStorage) {
|
|
s.mu.Lock()
|
|
s.current = current
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
func (s *Reloadable) Put(ctx context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.current.Put(ctx, name, reader)
|
|
}
|
|
func (s *Reloadable) Open(ctx context.Context, name string) (io.ReadCloser, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.current.Open(ctx, name)
|
|
}
|
|
func (s *Reloadable) Delete(ctx context.Context, name string) error {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.current.Delete(ctx, name)
|
|
}
|
|
func (s *Reloadable) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.current.Compose(ctx, names, destination)
|
|
}
|
|
func (s *Reloadable) DeletePrefix(ctx context.Context, prefix string) error {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.current.DeletePrefix(ctx, prefix)
|
|
}
|
|
func (s *Reloadable) List(ctx context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.current.List(ctx, prefix, cursor, limit)
|
|
}
|