mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 03:05:14 +00:00
82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/micro/micro/v3/service/auth"
|
|
log "github.com/micro/micro/v3/service/logger"
|
|
"github.com/micro/micro/v3/service/model"
|
|
files "github.com/micro/services/files/proto"
|
|
)
|
|
|
|
type Files struct {
|
|
db model.Model
|
|
}
|
|
|
|
func NewFiles() *Files {
|
|
i := model.ByEquality("project")
|
|
i.Order.Type = model.OrderTypeUnordered
|
|
|
|
db := model.New(
|
|
files.File{},
|
|
&model.Options{
|
|
Key: "Id",
|
|
Indexes: []model.Index{i},
|
|
},
|
|
)
|
|
|
|
return &Files{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (e *Files) Save(ctx context.Context, req *files.SaveRequest, rsp *files.SaveResponse) error {
|
|
// @todo return proper micro errors
|
|
acc, ok := auth.AccountFromContext(ctx)
|
|
if !ok {
|
|
return errors.New("Files.Save requires authentication")
|
|
}
|
|
|
|
log.Info("Received Files.Save request")
|
|
for _, file := range req.Files {
|
|
f := files.File{}
|
|
err := e.db.Read(model.QueryEquals("Id", file.Id), &f)
|
|
if err != nil && err != model.ErrorNotFound {
|
|
return err
|
|
}
|
|
// if file exists check ownership
|
|
if f.Id != "" && f.Owner != acc.ID {
|
|
return errors.New("Not authorized")
|
|
}
|
|
err = e.db.Create(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Files) List(ctx context.Context, req *files.ListRequest, rsp *files.ListResponse) error {
|
|
log.Info("Received Files.List request")
|
|
rsp.Files = []*files.File{}
|
|
err := e.db.Read(model.QueryEquals("project", req.GetProject()), &rsp.Files)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// @todo funnily while this is the archetypical
|
|
// query for the KV store interface, it's not supported by the model
|
|
// so we do client side filtering here
|
|
if req.Path != "" {
|
|
filtered := []*files.File{}
|
|
for _, file := range rsp.Files {
|
|
if strings.HasPrefix(file.Path, req.Path) {
|
|
filtered = append(filtered, file)
|
|
}
|
|
}
|
|
rsp.Files = filtered
|
|
}
|
|
return nil
|
|
}
|