Skip to content

Commit

Permalink
add utils pkg for dealing with spaces ids
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Feb 9, 2024
1 parent 9a016c0 commit e4ec202
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/spaces/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package utils

import (
"encoding/base32"
"fmt"
"strings"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
)

func DecodeSpaceID(raw string) (storageID, path string, ok bool) {
// The input is expected to be in the form of <storage_id>$<base32(<path>)
s := strings.SplitN(raw, "$", 2)
if len(s) != 2 {
return
}

storageID = s[0]
encodedPath := s[1]
p, err := base32.StdEncoding.DecodeString(encodedPath)
if err != nil {
return
}

path = string(p)
ok = true
return
}

func DecodeResourceID(raw string) (storageID, path, itemID string, ok bool) {
// The input is expected to be in the form of <storage_id>$<base32(<path>)!<item_id>
s := strings.SplitN(raw, "!", 2)
if len(s) != 2 {
return
}
itemID = s[1]
storageID, path, ok = DecodeSpaceID(s[0])
return
}

func EncodeResourceID(r *provider.ResourceId) string {
return fmt.Sprintf("%s$%s!%s", r.StorageId, r.SpaceId, r.OpaqueId)
}

func EncodeSpaceID(path string) string {
return base32.StdEncoding.EncodeToString([]byte(path))
}

0 comments on commit e4ec202

Please sign in to comment.