2019-06-24 20:23:07 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
2019-06-25 00:36:10 +01:00
|
|
|
"strings"
|
2019-06-24 20:23:07 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/paths"
|
2019-06-25 00:36:10 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-06-24 20:23:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Path is a representation of an object path within a bucket
|
|
|
|
type Path struct {
|
|
|
|
bucket string
|
|
|
|
unencPath paths.Unencrypted
|
|
|
|
raw []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bucket returns the bucket part of the path.
|
|
|
|
func (p Path) Bucket() string { return p.bucket }
|
|
|
|
|
|
|
|
// UnencryptedPath returns the unencrypted path part of the path.
|
|
|
|
func (p Path) UnencryptedPath() paths.Unencrypted { return p.unencPath }
|
|
|
|
|
|
|
|
// Raw returns the raw data in the path.
|
|
|
|
func (p Path) Raw() []byte { return append([]byte(nil), p.raw...) }
|
|
|
|
|
|
|
|
// String returns the string form of the raw data in the path.
|
|
|
|
func (p Path) String() string { return string(p.raw) }
|
|
|
|
|
|
|
|
// ParsePath returns a new Path with the given raw bytes.
|
2019-06-25 00:36:10 +01:00
|
|
|
func ParsePath(raw storj.Path) (path Path) {
|
|
|
|
// A path may contain a bucket and an unencrypted path.
|
|
|
|
parts := strings.SplitN(raw, "/", 2)
|
|
|
|
path.bucket = parts[0]
|
2019-06-24 20:23:07 +01:00
|
|
|
if len(parts) > 1 {
|
2019-06-25 00:36:10 +01:00
|
|
|
path.unencPath = paths.NewUnencrypted(parts[1])
|
2019-06-24 20:23:07 +01:00
|
|
|
}
|
2019-06-25 00:36:10 +01:00
|
|
|
path.raw = []byte(raw)
|
|
|
|
return path
|
2019-06-24 20:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePath will create a Path for the provided information.
|
|
|
|
func CreatePath(bucket string, unencPath paths.Unencrypted) (path Path) {
|
|
|
|
path.bucket = bucket
|
|
|
|
path.unencPath = unencPath
|
|
|
|
|
|
|
|
path.raw = append(path.raw, bucket...)
|
|
|
|
if unencPath.Valid() {
|
|
|
|
path.raw = append(path.raw, '/')
|
|
|
|
path.raw = append(path.raw, unencPath.Raw()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|