corso/src/internal/kopia/path_encoder.go
Abin Simon 1cad17bfc7
Use filepath instead of path (#1247)
## Description

`path` is not cross platform as it hardcodes / as the separator. Also add a lint rule for this.

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🐹 Trivial/Minor

## Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* fixes https://github.com/alcionai/corso/issues/1236

## Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
2022-10-20 22:57:30 +00:00

37 lines
1.0 KiB
Go

package kopia
import (
"encoding/base64"
"path"
)
var encoder = base64.URLEncoding
// encodeElements takes a set of strings and returns a slice of the strings
// after encoding them to a file system-safe format. Elements are returned in
// the same order they were passed in.
func encodeElements(elements ...string) []string {
encoded := make([]string, 0, len(elements))
for _, e := range elements {
encoded = append(encoded, encoder.EncodeToString([]byte(e)))
}
return encoded
}
// encodeAsPath takes a set of elements and returns the concatenated elements as
// if they were a path. The elements are joined with the separator in the golang
// path package.
func encodeAsPath(elements ...string) string {
// Needs `/` to be used a separator here
//nolint:forbidigo
return path.Join(encodeElements(elements...)...)
}
// decodeElement takes an encoded element and decodes it if possible.
func decodeElement(element string) (string, error) {
r, err := encoder.DecodeString(element)
return string(r), err
}