2020-07-03 13:52:04 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2021-07-28 13:27:06 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-03 11:48:33 +00:00
|
|
|
"os"
|
2020-10-24 13:09:22 +00:00
|
|
|
"strconv"
|
2020-07-03 13:52:04 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-08-10 10:03:09 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-10-19 01:04:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-07-28 13:27:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-08-18 13:48:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/cache"
|
2021-07-28 13:27:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
2020-07-03 13:52:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-05-13 20:25:31 +00:00
|
|
|
// ObjectInfo holds S3 object data.
|
2020-08-03 11:48:33 +00:00
|
|
|
ObjectInfo struct {
|
2021-08-13 14:13:14 +00:00
|
|
|
id *object.ID
|
|
|
|
bucketID *cid.ID
|
|
|
|
isDir bool
|
|
|
|
|
|
|
|
Bucket string
|
|
|
|
Name string
|
|
|
|
Size int64
|
|
|
|
ContentType string
|
|
|
|
Created time.Time
|
|
|
|
CreationEpoch uint64
|
|
|
|
HashSum string
|
|
|
|
Owner *owner.ID
|
|
|
|
Headers map[string]string
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
// ListObjectsInfo contains common fields of data for ListObjectsV1 and ListObjectsV2.
|
2020-08-03 11:48:33 +00:00
|
|
|
ListObjectsInfo struct {
|
2021-07-18 13:40:19 +00:00
|
|
|
Prefixes []string
|
|
|
|
Objects []*ObjectInfo
|
2020-08-03 11:48:33 +00:00
|
|
|
IsTruncated bool
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
// ListObjectsInfoV1 holds data which ListObjectsV1 returns.
|
|
|
|
ListObjectsInfoV1 struct {
|
|
|
|
ListObjectsInfo
|
2021-06-25 12:54:25 +00:00
|
|
|
NextMarker string
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
2021-06-25 12:54:25 +00:00
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
// ListObjectsInfoV2 holds data which ListObjectsV2 returns.
|
|
|
|
ListObjectsInfoV2 struct {
|
|
|
|
ListObjectsInfo
|
|
|
|
NextContinuationToken string
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2021-07-05 19:18:58 +00:00
|
|
|
|
|
|
|
// ObjectVersionInfo stores info about objects versions.
|
|
|
|
ObjectVersionInfo struct {
|
2021-08-13 14:13:14 +00:00
|
|
|
Object *ObjectInfo
|
|
|
|
IsLatest bool
|
2021-07-05 19:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectVersionsInfo stores info and list of objects' versions.
|
|
|
|
ListObjectVersionsInfo struct {
|
2021-08-18 13:48:58 +00:00
|
|
|
CommonPrefixes []string
|
2021-07-05 19:18:58 +00:00
|
|
|
IsTruncated bool
|
|
|
|
KeyMarker string
|
|
|
|
NextKeyMarker string
|
|
|
|
NextVersionIDMarker string
|
|
|
|
Version []*ObjectVersionInfo
|
2021-08-13 14:13:14 +00:00
|
|
|
DeleteMarker []*ObjectVersionInfo
|
2021-07-05 19:18:58 +00:00
|
|
|
VersionIDMarker string
|
|
|
|
}
|
2020-07-03 13:52:04 +00:00
|
|
|
)
|
|
|
|
|
2021-06-29 09:59:33 +00:00
|
|
|
// PathSeparator is a path components separator string.
|
|
|
|
const PathSeparator = string(os.PathSeparator)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
func userHeaders(attrs []*object.Attribute) map[string]string {
|
|
|
|
result := make(map[string]string, len(attrs))
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
for _, attr := range attrs {
|
2020-11-24 07:01:38 +00:00
|
|
|
result[attr.Key()] = attr.Value()
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
func objInfoFromMeta(bkt *cache.BucketInfo, meta *object.Object) *ObjectInfo {
|
|
|
|
return objectInfoFromMeta(bkt, meta, "", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func objectInfoFromMeta(bkt *cache.BucketInfo, meta *object.Object, prefix, delimiter string) *ObjectInfo {
|
2020-10-24 13:09:22 +00:00
|
|
|
var (
|
2021-06-29 09:59:33 +00:00
|
|
|
isDir bool
|
|
|
|
size int64
|
|
|
|
mimeType string
|
|
|
|
creation time.Time
|
|
|
|
filename = filenameFromObject(meta)
|
2020-10-24 13:09:22 +00:00
|
|
|
)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2021-06-29 09:59:33 +00:00
|
|
|
if !strings.HasPrefix(filename, prefix) {
|
2021-01-14 17:39:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:01:38 +00:00
|
|
|
userHeaders := userHeaders(meta.Attributes())
|
2021-06-29 09:59:33 +00:00
|
|
|
delete(userHeaders, object.AttributeFileName)
|
|
|
|
if contentType, ok := userHeaders[object.AttributeContentType]; ok {
|
|
|
|
mimeType = contentType
|
|
|
|
delete(userHeaders, object.AttributeContentType)
|
2020-10-24 13:09:22 +00:00
|
|
|
}
|
|
|
|
if val, ok := userHeaders[object.AttributeTimestamp]; !ok {
|
|
|
|
// ignore empty value
|
|
|
|
} else if dt, err := strconv.ParseInt(val, 10, 64); err == nil {
|
|
|
|
creation = time.Unix(dt, 0)
|
|
|
|
delete(userHeaders, object.AttributeTimestamp)
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 09:59:33 +00:00
|
|
|
if len(delimiter) > 0 {
|
|
|
|
tail := strings.TrimPrefix(filename, prefix)
|
|
|
|
index := strings.Index(tail, delimiter)
|
|
|
|
if index >= 0 {
|
|
|
|
isDir = true
|
2021-07-07 15:49:59 +00:00
|
|
|
mimeType = ""
|
2021-06-29 09:59:33 +00:00
|
|
|
filename = prefix + tail[:index+1]
|
|
|
|
userHeaders = nil
|
|
|
|
} else {
|
2021-06-30 12:29:01 +00:00
|
|
|
size = int64(meta.PayloadSize())
|
2021-06-29 09:59:33 +00:00
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
} else {
|
2021-06-30 12:29:01 +00:00
|
|
|
size = int64(meta.PayloadSize())
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
|
|
|
return &ObjectInfo{
|
2021-08-13 14:13:14 +00:00
|
|
|
id: meta.ID(),
|
|
|
|
bucketID: bkt.CID,
|
|
|
|
isDir: isDir,
|
|
|
|
|
|
|
|
Bucket: bkt.Name,
|
|
|
|
Name: filename,
|
|
|
|
Created: creation,
|
|
|
|
CreationEpoch: meta.CreationEpoch(),
|
|
|
|
ContentType: mimeType,
|
|
|
|
Headers: userHeaders,
|
|
|
|
Owner: meta.OwnerID(),
|
|
|
|
Size: size,
|
|
|
|
HashSum: meta.PayloadChecksum().String(),
|
2021-07-05 19:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 09:59:33 +00:00
|
|
|
func filenameFromObject(o *object.Object) string {
|
|
|
|
var name = o.ID().String()
|
2020-11-24 07:01:38 +00:00
|
|
|
for _, attr := range o.Attributes() {
|
|
|
|
if attr.Key() == object.AttributeFileName {
|
2021-06-29 09:59:33 +00:00
|
|
|
return attr.Value()
|
2020-10-19 01:04:37 +00:00
|
|
|
}
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2021-06-29 09:59:33 +00:00
|
|
|
return name
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// NameFromString splits name into base file name and directory path.
|
2021-01-14 17:39:48 +00:00
|
|
|
func NameFromString(name string) (string, string) {
|
|
|
|
ind := strings.LastIndex(name, PathSeparator)
|
2020-07-03 13:52:04 +00:00
|
|
|
return name[ind+1:], name[:ind+1]
|
|
|
|
}
|
2020-10-24 13:09:22 +00:00
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// ID returns object ID from ObjectInfo.
|
2020-10-24 13:09:22 +00:00
|
|
|
func (o *ObjectInfo) ID() *object.ID { return o.id }
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
// Version returns object version from ObjectInfo.
|
|
|
|
func (o *ObjectInfo) Version() string { return o.id.String() }
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
// NiceName returns object name for cache.
|
|
|
|
func (o *ObjectInfo) NiceName() string { return o.Bucket + "/" + o.Name }
|
|
|
|
|
|
|
|
// Address returns object address.
|
|
|
|
func (o *ObjectInfo) Address() *object.Address { return newAddress(o.bucketID, o.id) }
|
|
|
|
|
2021-08-17 08:04:42 +00:00
|
|
|
// TagsObject returns name of system object for tags.
|
|
|
|
func (o *ObjectInfo) TagsObject() string { return ".tagset." + o.Name + "." + o.Version() }
|
|
|
|
|
2021-08-10 10:03:09 +00:00
|
|
|
// CID returns bucket ID from ObjectInfo.
|
|
|
|
func (o *ObjectInfo) CID() *cid.ID { return o.bucketID }
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// IsDir allows to check if object is a directory.
|
2021-01-14 17:39:48 +00:00
|
|
|
func (o *ObjectInfo) IsDir() bool { return o.isDir }
|
2021-07-28 13:27:06 +00:00
|
|
|
|
|
|
|
// GetBoxData extracts accessbox.Box from context.
|
|
|
|
func GetBoxData(ctx context.Context) (*accessbox.Box, error) {
|
|
|
|
var boxData *accessbox.Box
|
|
|
|
data, ok := ctx.Value(api.BoxData).(*accessbox.Box)
|
|
|
|
if !ok || data == nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get box data from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
boxData = data
|
|
|
|
if boxData.Gate == nil {
|
|
|
|
boxData.Gate = &accessbox.GateData{}
|
|
|
|
}
|
|
|
|
return boxData, nil
|
|
|
|
}
|
2021-08-17 11:57:24 +00:00
|
|
|
|
|
|
|
func formBucketTagObjectName(name string) string {
|
|
|
|
return ".tagset." + name
|
|
|
|
}
|