diff --git a/services/graph/pkg/service/v0/personaldata.go b/services/graph/pkg/service/v0/personaldata.go index 5bb0f8c2927..dfb579e44fb 100644 --- a/services/graph/pkg/service/v0/personaldata.go +++ b/services/graph/pkg/service/v0/personaldata.go @@ -2,14 +2,15 @@ package svc import ( "bytes" + "context" "encoding/json" "fmt" "io" "net/http" "path/filepath" "strconv" - "time" + gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" user "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" @@ -59,13 +60,8 @@ func (g Graph) ExportPersonalData(w http.ResponseWriter, r *http.Request) { } // touch file - gwc := g.GetGatewayClient() - resp, err := gwc.TouchFile(ctx, &provider.TouchFileRequest{ - Opaque: utils.AppendPlainToOpaque(nil, "markprocessing", "true"), - Ref: ref, - }) - if err != nil || resp.GetStatus().GetCode() != rpc.Code_CODE_OK { - g.logger.Error().Err(err).Str("status", resp.GetStatus().GetMessage()).Msg("error touching file") + if err := mustTouchFile(ctx, ref, g.GetGatewayClient()); err != nil { + g.logger.Error().Err(err).Msg("error touching file") w.WriteHeader(http.StatusInternalServerError) return } @@ -78,9 +74,6 @@ func (g Graph) ExportPersonalData(w http.ResponseWriter, r *http.Request) { // GatherPersonalData will all gather all personal data of the user and save it to a file in the users personal space func (g Graph) GatherPersonalData(usr *user.User, ref *provider.Reference, token string, marsh Marshaller) { - // TMP - Delay processing - comment if you read this on PR - time.Sleep(10 * time.Second) - // create data data := make(map[string]interface{}) @@ -146,6 +139,58 @@ func (g Graph) upload(u *user.User, data []byte, ref *provider.Reference, th str return nil } +// touches the file, creating folders if neccessary +func mustTouchFile(ctx context.Context, ref *provider.Reference, gwc gateway.GatewayAPIClient) error { + if err := touchFile(ctx, ref, gwc); err == nil { + return nil + } + + if err := createFolders(ctx, ref, gwc); err != nil { + return err + } + + return touchFile(ctx, ref, gwc) +} + +func touchFile(ctx context.Context, ref *provider.Reference, gwc gateway.GatewayAPIClient) error { + resp, err := gwc.TouchFile(ctx, &provider.TouchFileRequest{ + Opaque: utils.AppendPlainToOpaque(nil, "markprocessing", "true"), + Ref: ref, + }) + if err != nil { + return err + } + if resp.GetStatus().GetCode() != rpc.Code_CODE_OK { + return fmt.Errorf("unexpected statuscode while touching file: %d %s", resp.GetStatus().GetCode(), resp.GetStatus().GetMessage()) + } + return nil +} + +func createFolders(ctx context.Context, ref *provider.Reference, gwc gateway.GatewayAPIClient) error { + var pathes []string + p := filepath.Dir(ref.GetPath()) + for p != "." { + pathes = append([]string{p}, pathes...) + fmt.Println(pathes) + p = filepath.Dir(p) + } + + for _, p := range pathes { + r := &provider.Reference{ResourceId: ref.GetResourceId(), Path: p} + resp, err := gwc.CreateContainer(ctx, &provider.CreateContainerRequest{Ref: r}) + if err != nil { + return err + } + + code := resp.GetStatus().GetCode() + if code != rpc.Code_CODE_OK && code != rpc.Code_CODE_ALREADY_EXISTS { + return fmt.Errorf("unexpected statuscode while creating folder: %d %s", code, resp.GetStatus().GetMessage()) + } + } + return nil + +} + func getLocation(r *http.Request) string { // from body var req ExportPersonalDataRequest