2021-10-14 17:00:11 +00:00
|
|
|
package loader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.kapelle.org/niklas/s3browser/internal/helper"
|
|
|
|
types "git.kapelle.org/niklas/s3browser/internal/types"
|
|
|
|
"github.com/graph-gophers/dataloader"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Loader struct {
|
|
|
|
listObjectsLoader *dataloader.Loader
|
|
|
|
listObjectsRecursiveLoader *dataloader.Loader
|
|
|
|
statObjectLoader *dataloader.Loader
|
|
|
|
listBucketsLoader *dataloader.Loader
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLoader(config types.AppConfig) *Loader {
|
|
|
|
return &Loader{
|
|
|
|
listObjectsLoader: dataloader.NewBatchedLoader(
|
|
|
|
listObjectsBatch,
|
2021-10-20 21:38:09 +00:00
|
|
|
dataloader.WithCache(&dataloader.NoCache{}),
|
2021-10-14 17:00:11 +00:00
|
|
|
),
|
|
|
|
listObjectsRecursiveLoader: dataloader.NewBatchedLoader(
|
|
|
|
listObjectsRecursiveBatch,
|
2021-10-20 21:38:09 +00:00
|
|
|
dataloader.WithCache(&dataloader.NoCache{}),
|
2021-10-14 17:00:11 +00:00
|
|
|
),
|
|
|
|
statObjectLoader: dataloader.NewBatchedLoader(
|
|
|
|
statObjectBatch,
|
2021-10-20 21:38:09 +00:00
|
|
|
dataloader.WithCache(&dataloader.NoCache{}),
|
2021-10-14 17:00:11 +00:00
|
|
|
),
|
|
|
|
listBucketsLoader: dataloader.NewBatchedLoader(
|
|
|
|
listBucketsBatch,
|
2021-10-20 21:38:09 +00:00
|
|
|
dataloader.WithCache(&dataloader.NoCache{}),
|
2021-10-14 17:00:11 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Loader) GetFiles(ctx context.Context, path types.ID) ([]types.File, error) {
|
|
|
|
thunk := l.listObjectsLoader.Load(ctx, path)
|
|
|
|
objects, err := thunk()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var files []types.File
|
|
|
|
|
|
|
|
for _, obj := range objects.([]minio.ObjectInfo) {
|
|
|
|
if obj.Err != nil {
|
|
|
|
return nil, obj.Err
|
|
|
|
} else if !strings.HasSuffix(obj.Key, "/") {
|
|
|
|
files = append(files, *helper.ObjInfoToFile(obj, path.Bucket))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Loader) GetFile(ctx context.Context, id types.ID) (*types.File, error) {
|
|
|
|
thunk := l.statObjectLoader.Load(ctx, id)
|
|
|
|
|
|
|
|
result, err := thunk()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
objInfo, ok := result.(minio.ObjectInfo)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to stats object")
|
|
|
|
}
|
|
|
|
|
|
|
|
return helper.ObjInfoToFile(objInfo, id.Bucket), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Loader) GetDirs(ctx context.Context, path types.ID) ([]types.Directory, error) {
|
|
|
|
thunk := l.listObjectsLoader.Load(ctx, path)
|
|
|
|
|
|
|
|
result, err := thunk()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var dirs []types.Directory
|
|
|
|
for _, obj := range result.([]minio.ObjectInfo) {
|
|
|
|
if obj.Err != nil {
|
|
|
|
return nil, obj.Err
|
|
|
|
} else if strings.HasSuffix(obj.Key, "/") {
|
|
|
|
resultID := types.ID{
|
|
|
|
Bucket: path.Bucket,
|
|
|
|
Key: obj.Key,
|
|
|
|
}
|
|
|
|
|
|
|
|
resultID.Normalize()
|
|
|
|
|
|
|
|
dirs = append(dirs, types.Directory{
|
|
|
|
ID: resultID,
|
|
|
|
Name: filepath.Base(obj.Key),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dirs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Loader) GetBuckets(ctx context.Context) ([]string, error) {
|
|
|
|
thunk := l.listBucketsLoader.Load(ctx, dataloader.StringKey(""))
|
|
|
|
|
|
|
|
result, err := thunk()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketsInfo := result.([]minio.BucketInfo)
|
|
|
|
var buckets []string
|
|
|
|
for _, i := range bucketsInfo {
|
|
|
|
buckets = append(buckets, i.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buckets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Loader) GetFilesRecursive(ctx context.Context, path types.ID) ([]types.File, error) {
|
|
|
|
thunk := l.listObjectsRecursiveLoader.Load(ctx, path)
|
|
|
|
result, err := thunk()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objects := result.([]minio.ObjectInfo)
|
|
|
|
|
|
|
|
var files []types.File
|
|
|
|
for _, obj := range objects {
|
|
|
|
files = append(files, *helper.ObjInfoToFile(obj, path.Bucket))
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Loader) InvalidateCacheForFile(ctx context.Context, id types.ID) {
|
2021-10-14 21:46:48 +00:00
|
|
|
log.Debug("Clear cache for file: ", id.String())
|
2021-10-14 17:00:11 +00:00
|
|
|
parent := id.Parent()
|
|
|
|
|
2021-10-14 21:46:48 +00:00
|
|
|
l.statObjectLoader.Clear(ctx, id)
|
|
|
|
l.listObjectsLoader.Clear(ctx, id).Clear(ctx, parent)
|
2021-10-14 17:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Loader) InvalidateCacheForDir(ctx context.Context, path types.ID) {
|
2021-10-14 21:46:48 +00:00
|
|
|
log.Debug("Clear cache for dir: ", path.String())
|
2021-10-14 17:00:11 +00:00
|
|
|
parent := helper.GetParentDir(path)
|
|
|
|
|
2021-10-14 21:46:48 +00:00
|
|
|
l.listObjectsLoader.Clear(ctx, path).Clear(ctx, parent)
|
2021-10-14 17:00:11 +00:00
|
|
|
l.listObjectsRecursiveLoader.Clear(ctx, path).Clear(ctx, parent)
|
|
|
|
}
|