mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-07 21:00:30 -06:00
proper url parsing using godata
This commit is contained in:
@@ -7,8 +7,10 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/CiscoM31/godata"
|
||||
cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
|
||||
v1beta11 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
|
||||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
@@ -209,6 +211,69 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
|
||||
// wildcards however addressed here is not yet supported. We want to address drives by their unique
|
||||
// identifiers. Any open queries need to be implemented. Same applies for sub-entities.
|
||||
// For further reading: http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_AddressingaSubsetofaCollection
|
||||
|
||||
// strip "/graph/v1.0/" out and parse the rest. This is how godata input is expected.
|
||||
//https://github.com/CiscoM31/godata/blob/d70e191d2908191623be84401fecc40d6af4afde/url_parser_test.go#L10
|
||||
sanitized := strings.TrimLeft(r.URL.Path, "/graph/v1.0/")
|
||||
|
||||
req, err := godata.ParseRequest(sanitized, r.URL.Query(), true)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if req.FirstSegment.Identifier.Get() == "" {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("identifier cannot be empty").Error())
|
||||
return
|
||||
}
|
||||
|
||||
drive := msgraph.Drive{}
|
||||
if err = json.NewDecoder(r.Body).Decode(&drive); err != nil {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("invalid request body: %v", r.Body).Error())
|
||||
return
|
||||
}
|
||||
|
||||
identifierParts := strings.Split(req.FirstSegment.Identifier.Get(), "!")
|
||||
if len(identifierParts) != 2 {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("invalid resource id: %v", req.FirstSegment.Identifier.Get()).Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
storageID, opaqueID := identifierParts[0], identifierParts[1]
|
||||
|
||||
client, err := g.GetClient()
|
||||
if err != nil {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
updateSpaceRequest := &provider.UpdateStorageSpaceRequest{
|
||||
// Prepare the object to apply the diff from. The properties on StorageSpace will overwrite
|
||||
// the original storage space.
|
||||
StorageSpace: &provider.StorageSpace{
|
||||
Root: &provider.ResourceId{
|
||||
StorageId: storageID,
|
||||
OpaqueId: opaqueID,
|
||||
},
|
||||
Name: *drive.Name,
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := client.UpdateStorageSpace(r.Context(), updateSpaceRequest)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if resp.GetStatus().GetCode() != v1beta11.Code_CODE_OK {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, fmt.Errorf("").Error())
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func cs3TimestampToTime(t *types.Timestamp) time.Time {
|
||||
return time.Unix(int64(t.Seconds), int64(t.Nanos))
|
||||
}
|
||||
|
||||
@@ -1,23 +1,12 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
v1beta11 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
|
||||
|
||||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
|
||||
"github.com/owncloud/ocis/graph/pkg/service/v0/errorcode"
|
||||
msgraph "github.com/owncloud/open-graph-api-go"
|
||||
|
||||
"github.com/owncloud/ocis/ocis-pkg/account"
|
||||
opkgm "github.com/owncloud/ocis/ocis-pkg/middleware"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/owncloud/ocis/ocis-pkg/account"
|
||||
opkgm "github.com/owncloud/ocis/ocis-pkg/middleware"
|
||||
)
|
||||
|
||||
// Service defines the extension handlers.
|
||||
@@ -63,80 +52,16 @@ func NewService(opts ...Option) Service {
|
||||
r.Get("/", svc.GetGroup)
|
||||
})
|
||||
})
|
||||
r.Route("/drives", func(r chi.Router) {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(opkgm.ExtractAccountUUID(
|
||||
account.Logger(options.Logger),
|
||||
account.JWTSecret(options.Config.TokenManager.JWTSecret)),
|
||||
)
|
||||
r.Post("/", svc.CreateDrive)
|
||||
})
|
||||
r.Route("/Drive({id})", func(r chi.Router) {
|
||||
r.Use(opkgm.ExtractAccountUUID(
|
||||
account.Logger(options.Logger),
|
||||
account.JWTSecret(options.Config.TokenManager.JWTSecret)),
|
||||
)
|
||||
r.Patch("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
drive := msgraph.Drive{}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&drive); err != nil {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("invalid schema definition").Error())
|
||||
return
|
||||
}
|
||||
|
||||
d := strings.ReplaceAll(chi.URLParam(r, "id"), `"`, "")
|
||||
|
||||
idParts := strings.Split(d, "!")
|
||||
if len(idParts) != 2 {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("invalid resource id").Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
storageID := idParts[0]
|
||||
opaqueID := idParts[1]
|
||||
|
||||
client, err := svc.GetClient()
|
||||
if err != nil {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
updateSpaceRequest := &provider.UpdateStorageSpaceRequest{
|
||||
StorageSpace: &provider.StorageSpace{
|
||||
Root: &provider.ResourceId{
|
||||
StorageId: storageID,
|
||||
OpaqueId: opaqueID,
|
||||
},
|
||||
Name: *drive.Name,
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := client.UpdateStorageSpace(r.Context(), updateSpaceRequest)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if resp.GetStatus().GetCode() != v1beta11.Code_CODE_OK {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, fmt.Errorf("").Error())
|
||||
}
|
||||
/*
|
||||
1. get storage space by id
|
||||
2. prepare UpdateStorageSpaceRequest
|
||||
3. get a reva client
|
||||
4. do UpdateStorageSpace request
|
||||
|
||||
Known loose ends:
|
||||
1. Reva's FS interface does not yet contain UpdateStorageSpace. Needs to be added.
|
||||
2. There are many ways to select a resource on OData. Because spaces names are not unique, we will support
|
||||
unique updates and not bulk updates. Supported URLs look like:
|
||||
|
||||
https://localhost:9200/graph/v1.0/DriveById(id=1284d238-aa92-42ce-bdc4-0b0000009157!cdf8d353-dd02-46ed-b06a-3bb66f29743c)
|
||||
|
||||
3. How are uploading images to the space being handled? Since an image is not a property of the Drive (speaking OData)
|
||||
it can be handled directly by doing an upload to the storage itself.
|
||||
4. Ditto for descriptions. We want to persist a space's description on a file inside the `.space` reserved folder.
|
||||
*/
|
||||
_, _ = w.Write([]byte(d))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
r.Route("/drives", func(r chi.Router) {
|
||||
r.Post("/", svc.CreateDrive)
|
||||
})
|
||||
r.Route("/Drive({firstSegmentIdentifier})", func(r chi.Router) {
|
||||
r.Patch("/*", svc.UpdateDrive)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user