2020-07-03 13:52:04 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2020-08-03 11:48:33 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-10-24 13:09:22 +00:00
|
|
|
"strconv"
|
2020-07-03 13:52:04 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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"
|
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-01-14 17:39:48 +00:00
|
|
|
id *object.ID
|
|
|
|
isDir bool
|
2020-10-24 13:09:22 +00:00
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
Bucket string
|
|
|
|
Name string
|
|
|
|
Size int64
|
|
|
|
ContentType string
|
|
|
|
Created time.Time
|
2021-06-29 13:40:26 +00:00
|
|
|
HashSum string
|
2020-10-19 01:04:37 +00:00
|
|
|
Owner *owner.ID
|
2020-08-03 11:48:33 +00:00
|
|
|
Headers map[string]string
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
// ListObjectsInfo - container for list objects.
|
|
|
|
ListObjectsInfo struct {
|
|
|
|
// Indicates whether the returned list objects response is truncated. A
|
|
|
|
// value of true indicates that the list was truncated. The list can be truncated
|
|
|
|
// if the number of objects exceeds the limit allowed or specified
|
|
|
|
// by max keys.
|
|
|
|
IsTruncated bool
|
|
|
|
|
|
|
|
// When response is truncated (the IsTruncated element value in the response
|
|
|
|
// is true), you can use the key name in this field as marker in the subsequent
|
|
|
|
// request to get next set of objects.
|
|
|
|
//
|
|
|
|
// NOTE: This element is returned only if you have delimiter request parameter
|
|
|
|
// specified.
|
|
|
|
ContinuationToken string
|
|
|
|
NextContinuationToken string
|
|
|
|
|
2021-06-25 12:54:25 +00:00
|
|
|
// When response is truncated (the IsTruncated element value in the response is true),
|
|
|
|
// you can use the key name in this field as marker in the subsequent request to get next set of objects.
|
|
|
|
NextMarker string
|
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
// List of objects info for this request.
|
2020-10-19 01:04:37 +00:00
|
|
|
Objects []*ObjectInfo
|
2020-08-03 11:48:33 +00:00
|
|
|
|
|
|
|
// List of prefixes for this request.
|
|
|
|
Prefixes []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-06-29 09:59:33 +00:00
|
|
|
func objectInfoFromMeta(bkt *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
|
|
|
|
filename = prefix + tail[:index+1]
|
|
|
|
userHeaders = nil
|
|
|
|
} else {
|
|
|
|
size, mimeType = getSizeAndMimeType(meta, mimeType)
|
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
} else {
|
2021-06-29 09:59:33 +00:00
|
|
|
size, mimeType = getSizeAndMimeType(meta, mimeType)
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
|
|
|
return &ObjectInfo{
|
2021-01-14 17:39:48 +00:00
|
|
|
id: meta.ID(),
|
|
|
|
isDir: isDir,
|
2020-10-24 13:09:22 +00:00
|
|
|
|
|
|
|
Bucket: bkt.Name,
|
|
|
|
Name: filename,
|
|
|
|
Created: creation,
|
2020-08-03 11:48:33 +00:00
|
|
|
ContentType: mimeType,
|
|
|
|
Headers: userHeaders,
|
2021-01-14 17:39:48 +00:00
|
|
|
Owner: meta.OwnerID(),
|
|
|
|
Size: size,
|
2021-06-29 13:40:26 +00:00
|
|
|
HashSum: meta.PayloadChecksum().String(),
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 09:59:33 +00:00
|
|
|
func getSizeAndMimeType(meta *object.Object, contentType string) (size int64, mimeType string) {
|
|
|
|
size = int64(meta.PayloadSize())
|
|
|
|
mimeType = contentType
|
|
|
|
if len(mimeType) == 0 {
|
|
|
|
mimeType = http.DetectContentType(meta.Payload())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-07-03 13:52:04 +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-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 }
|