Skip to content

Commit

Permalink
initial quota support
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Jan 18, 2021
1 parent 47119be commit 7851535
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Handler struct {
func (h *Handler) Init(c *config.Config) {
h.UserHandler = new(user.Handler)
h.UsersHandler = new(users.Handler)
h.UsersHandler.Init(c)
h.CapabilitiesHandler = new(capabilities.Handler)
h.CapabilitiesHandler.Init(c)
}
Expand Down
85 changes: 71 additions & 14 deletions internal/http/services/owncloud/ocs/handlers/cloud/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@ import (
"fmt"
"net/http"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/internal/http/services/owncloud/ocdav"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/config"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/response"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/pkg/rhttp/router"
ctxuser "github.com/cs3org/reva/pkg/user"
)

// The UsersHandler renders user data for the user id given in the url path
// Handler renders user data for the user id given in the url path
type Handler struct {
gatewayAddr string
}

// Init initializes this and any contained handlers
func (h *Handler) Init(c *config.Config) error {
h.gatewayAddr = c.GatewaySvc
return nil
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -53,19 +68,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
head, r.URL.Path = router.ShiftPath(r.URL.Path)
switch head {
case "":
response.WriteOCSSuccess(w, r, &Users{
// FIXME query storages? cache a summary?
// TODO use list of storages to allow clients to resolve quota status
Quota: &Quota{
Free: 2840756224000,
Used: 5059416668,
Total: 2845815640668,
Relative: 0.18,
Definition: "default",
},
DisplayName: u.DisplayName,
Email: u.Mail,
})
h.handleUsers(w, r, u)
return
case "groups":
response.WriteOCSSuccess(w, r, &Groups{})
Expand Down Expand Up @@ -100,3 +103,57 @@ type Users struct {
type Groups struct {
Groups []string `json:"groups" xml:"groups>element"`
}

func (h *Handler) handleUsers(w http.ResponseWriter, r *http.Request, u *userpb.User) {
ctx := r.Context()
sublog := appctx.GetLogger(r.Context())

gc, err := pool.GetGatewayServiceClient(h.gatewayAddr)
if err != nil {
sublog.Error().Err(err).Msg("error getting gateway client")
w.WriteHeader(http.StatusInternalServerError)
return
}

getHomeRes, err := gc.GetHome(ctx, &provider.GetHomeRequest{})
if err != nil {
sublog.Error().Err(err).Msg("error calling GetHome")
w.WriteHeader(http.StatusInternalServerError)
return
}
if getHomeRes.Status.Code != rpc.Code_CODE_OK {
ocdav.HandleErrorStatus(sublog, w, getHomeRes.Status)
return
}

getQuotaRes, err := gc.GetQuota(ctx, &gateway.GetQuotaRequest{
Ref: &provider.Reference{
Spec: &provider.Reference_Path{
Path: getHomeRes.Path,
},
},
})
if err != nil {
sublog.Error().Err(err).Msg("error calling GetQuota")
w.WriteHeader(http.StatusInternalServerError)
return
}

if getQuotaRes.Status.Code != rpc.Code_CODE_OK {
ocdav.HandleErrorStatus(sublog, w, getQuotaRes.Status)
return
}

response.WriteOCSSuccess(w, r, &Users{
// ocs can only return the home storage quota
Quota: &Quota{
Free: int64(getQuotaRes.TotalBytes - getQuotaRes.UsedBytes),
Used: int64(getQuotaRes.UsedBytes),
Total: int64(getQuotaRes.TotalBytes), // -1, -2 have special meaning?
Relative: float32(float64(getQuotaRes.UsedBytes) / float64(getQuotaRes.TotalBytes)),
Definition: "default",
},
DisplayName: u.DisplayName,
Email: u.Mail,
})
}

0 comments on commit 7851535

Please sign in to comment.