Skip to content

Commit

Permalink
get template from request
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Mar 6, 2024
1 parent 45ef453 commit 42d6954
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 80 deletions.
2 changes: 1 addition & 1 deletion services/graph/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Spaces struct {
ExtendedSpacePropertiesCacheTTL int `yaml:"extended_space_properties_cache_ttl" env:"GRAPH_SPACES_EXTENDED_SPACE_PROPERTIES_CACHE_TTL" desc:"Max TTL in seconds for the spaces property cache." introductionVersion:"pre5.0"`
UsersCacheTTL int `yaml:"users_cache_ttl" env:"GRAPH_SPACES_USERS_CACHE_TTL" desc:"Max TTL in seconds for the spaces users cache." introductionVersion:"pre5.0"`
GroupsCacheTTL int `yaml:"groups_cache_ttl" env:"GRAPH_SPACES_GROUPS_CACHE_TTL" desc:"Max TTL in seconds for the spaces groups cache." introductionVersion:"pre5.0"`
TemplatePath string `yaml:"template_path" env:"GRAPH_SPACES_TEMPLATE_PATH" desc:"The path to a folder containg files and folder that should be created on any space." introductionVersion:"5.0"`
StorageUsersAddress string `yaml:"storage_users_address" env:"GRAPH_SPACES_STORAGE_USERS_ADDRESS" desc:"The address of the storage-users service." introductionVersion:"5.0"`
}

type LDAP struct {
Expand Down
7 changes: 4 additions & 3 deletions services/graph/pkg/config/defaults/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ func DefaultConfig() *config.Config {
},
Reva: shared.DefaultRevaConfig(),
Spaces: config.Spaces{
WebDavBase: "https://localhost:9200",
WebDavPath: "/dav/spaces/",
DefaultQuota: "1000000000",
StorageUsersAddress: "com.owncloud.api.storage-users",
WebDavBase: "https://localhost:9200",
WebDavPath: "/dav/spaces/",
DefaultQuota: "1000000000",
// 1 minute
ExtendedSpacePropertiesCacheTTL: 60,
// 1 minute
Expand Down
30 changes: 11 additions & 19 deletions services/graph/pkg/service/v0/drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/fs"
"math"
"net/http"
"net/url"
Expand Down Expand Up @@ -483,31 +482,24 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
return
}

newDrive, err := g.cs3StorageSpaceToDrive(r.Context(), webDavBaseURL, resp.StorageSpace)
if err != nil {
logger.Debug().Err(err).Msg("could not create drive: error parsing drive")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
if driveType == _spaceTypeProject {
opaque, err := g.applySpaceTemplate(gatewayClient, resp.GetStorageSpace().GetRoot(), r.URL.Query().Get("template"))
if err != nil {
logger.Error().Err(err).Msg("could not apply template to space")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}

ctx, cs3, err := g.getCS3Client(gatewayClient, resp.GetStorageSpace().GetRoot())
if err != nil {
logger.Error().Err(err).Msg("could not get cs3 client")
errorcode.ServiceNotAvailable.Render(w, r, http.StatusInternalServerError, "could not get cs3 client, aborting")
return
resp.StorageSpace.Opaque = utils.MergeOpaques(resp.GetStorageSpace().GetOpaque(), opaque)
}

fsys, err := fs.Sub(_spaceTemplateFS, "spacetemplate")
newDrive, err := g.cs3StorageSpaceToDrive(r.Context(), webDavBaseURL, resp.GetStorageSpace())
if err != nil {
logger.Error().Err(err).Msg("could not create drive: error parsing fs")
logger.Debug().Err(err).Msg("could not create drive: error parsing drive")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}

if err := applyTemplate(ctx, cs3, gatewayClient, resp.GetStorageSpace().GetRoot(), fsys.(fs.ReadDirFS)); err != nil {
logger.Error().Err(err).Msg("could not apply template to space")
return
}
newDrive.Special = g.getSpecialDriveItems(r.Context(), webDavBaseURL, resp.GetStorageSpace())

render.Status(r, http.StatusCreated)
render.JSON(w, r, newDrive)
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Space description goes here
Here you can add a description for this Space.
124 changes: 68 additions & 56 deletions services/graph/pkg/service/v0/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package svc
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
Expand Down Expand Up @@ -434,91 +434,103 @@ func cs3ReceivedShareToLibreGraphPermissions(ctx context.Context, logger *log.Lo
return permission, nil
}

func (g Graph) getCS3Client(gwc gateway.GatewayAPIClient, root *storageprovider.ResourceId) (context.Context, *metadata.CS3, error) {
mdc := metadata.NewCS3(g.config.Reva.Address, "com.owncloud.api.storage-users")
func (g Graph) applySpaceTemplate(gwc gateway.GatewayAPIClient, root *storageprovider.ResourceId, template string) (*v1beta1.Opaque, error) {
var fsys fs.ReadDirFS

switch template {
default:
fallthrough
case "none":
return nil, nil
case "default":
f, err := fs.Sub(_spaceTemplateFS, "spacetemplate")
if err != nil {
return nil, err
}
fsys = f.(fs.ReadDirFS)
}

mdc := metadata.NewCS3(g.config.Reva.Address, g.config.Spaces.StorageUsersAddress)
mdc.SpaceRoot = root

ctx, err := utils.GetServiceUserContext(g.config.ServiceAccount.ServiceAccountID, gwc, g.config.ServiceAccount.ServiceAccountSecret)
return ctx, mdc, err
}
if err != nil {
return nil, err
}

func applyTemplate(ctx context.Context, mdc *metadata.CS3, gwc gateway.GatewayAPIClient, root *storageprovider.ResourceId, fsys fs.ReadDirFS) error {
entries, err := fsys.ReadDir(".")
opaque, err := uploadFolder(ctx, mdc, ".", "", nil, fsys)
if err != nil {
return err
return nil, err
}

updateSpaceRequest := &storageprovider.UpdateStorageSpaceRequest{
// Prepare the object to apply the diff from. The properties on StorageSpace will overwrite
// the original storage space.
resp, err := gwc.UpdateStorageSpace(ctx, &storageprovider.UpdateStorageSpaceRequest{
StorageSpace: &storageprovider.StorageSpace{
Id: &storageprovider.StorageSpaceId{
OpaqueId: storagespace.FormatResourceID(*root),
},
Root: root,
Root: root,
Opaque: opaque,
},
}

updateSpaceRequest.StorageSpace.Opaque, err = uploadFolder(ctx, mdc, "", "", updateSpaceRequest.StorageSpace.Opaque, fsys, entries)
if err != nil {
return err
}

if len(updateSpaceRequest.StorageSpace.Opaque.Map) == 0 {
return nil
}

resp, err := gwc.UpdateStorageSpace(ctx, updateSpaceRequest)
})
switch {
case err != nil:
return err
case resp.Status.Code == rpc.Code_CODE_OK:
return nil
return nil, err
case resp.GetStatus().GetCode() != rpc.Code_CODE_OK:
return nil, fmt.Errorf("could not update storage space: %s", resp.GetStatus().GetMessage())
default:
return errors.New(resp.Status.Message)
return opaque, nil
}
}

func uploadFolder(ctx context.Context, mdc *metadata.CS3, pathOnDisc, pathOnSpace string, opaque *v1beta1.Opaque, fsys fs.ReadDirFS, entries []os.DirEntry) (*v1beta1.Opaque, error) {
func uploadFolder(ctx context.Context, mdc *metadata.CS3, pathOnDisc, pathOnSpace string, opaque *v1beta1.Opaque, fsys fs.ReadDirFS) (*v1beta1.Opaque, error) {
entries, err := fsys.ReadDir(pathOnDisc)
if err != nil {
return nil, err
}

for _, entry := range entries {
spacePath := filepath.Join(pathOnSpace, entry.Name())
discPath := filepath.Join(pathOnDisc, entry.Name())
opaque, err = uploadEntry(ctx, mdc, pathOnDisc, pathOnSpace, opaque, fsys, entry)
if err != nil {
return nil, err
}
}

if entry.IsDir() {
err := mdc.MakeDirIfNotExist(ctx, spacePath)
if err != nil {
return opaque, err
}
return opaque, nil
}

entries, err := fsys.ReadDir(discPath)
if err != nil {
return opaque, err
}
func uploadEntry(ctx context.Context, mdc *metadata.CS3, pathOnDisc, pathOnSpace string, opaque *v1beta1.Opaque, fsys fs.ReadDirFS, entry os.DirEntry) (*v1beta1.Opaque, error) {
spacePath := filepath.Join(pathOnSpace, entry.Name())
discPath := filepath.Join(pathOnDisc, entry.Name())

opaque, err = uploadFolder(ctx, mdc, discPath, spacePath, opaque, fsys, entries)
if err != nil {
return opaque, err
}
continue
switch {
case entry.IsDir():
err := mdc.MakeDirIfNotExist(ctx, spacePath)
if err != nil {
return nil, err
}

return uploadFolder(ctx, mdc, discPath, spacePath, opaque, fsys)
default:
b, err := fs.ReadFile(fsys, discPath)
if err != nil {
return opaque, err
}

if err := mdc.SimpleUpload(ctx, spacePath, b); err != nil {
return opaque, err
return nil, err
}

// TODO: use upload to avoid second stat
i, err := mdc.Stat(ctx, spacePath)
res, err := mdc.Upload(ctx, metadata.UploadRequest{
Path: spacePath,
Content: b,
})
if err != nil {
return opaque, err
return nil, err
}

identifier := strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name()))
opaque = utils.AppendPlainToOpaque(opaque, identifier, storagespace.FormatResourceID(*i.Id))
for _, special := range []string{ReadmeSpecialFolderName, SpaceImageSpecialFolderName} {
if special == identifier {
opaque = utils.AppendPlainToOpaque(opaque, identifier, res.FileID)
break
}
}
return opaque, nil
}

return opaque, nil
}

0 comments on commit 42d6954

Please sign in to comment.