Skip to content

Commit

Permalink
create folders if they are not existing
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Mar 29, 2023
1 parent d74129b commit 0f95409
Showing 1 changed file with 56 additions and 11 deletions.
67 changes: 56 additions & 11 deletions services/graph/pkg/service/v0/personaldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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{})

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0f95409

Please sign in to comment.