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 (
|
2020-08-03 11:48:33 +00:00
|
|
|
ObjectInfo struct {
|
2020-10-24 13:09:22 +00:00
|
|
|
id *object.ID
|
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
Bucket string
|
|
|
|
Name string
|
|
|
|
Size int64
|
|
|
|
ContentType string
|
|
|
|
Created time.Time
|
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
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
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 {
|
|
|
|
result[attr.GetKey()] = attr.GetValue()
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:09:22 +00:00
|
|
|
func objectInfoFromMeta(bkt *BucketInfo, meta *object.Object) *ObjectInfo {
|
|
|
|
var (
|
|
|
|
creation time.Time
|
|
|
|
filename = meta.GetID().String()
|
|
|
|
)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
userHeaders := userHeaders(meta.GetAttributes())
|
2020-10-24 13:09:22 +00:00
|
|
|
if val, ok := userHeaders[object.AttributeFileName]; ok {
|
|
|
|
filename = val
|
|
|
|
delete(userHeaders, object.AttributeFileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
mimeType := http.DetectContentType(meta.GetPayload())
|
2020-08-03 11:48:33 +00:00
|
|
|
|
|
|
|
return &ObjectInfo{
|
2020-10-24 13:09:22 +00:00
|
|
|
id: meta.GetID(),
|
|
|
|
|
|
|
|
Bucket: bkt.Name,
|
|
|
|
Name: filename,
|
|
|
|
Created: creation,
|
2020-08-03 11:48:33 +00:00
|
|
|
ContentType: mimeType,
|
|
|
|
Headers: userHeaders,
|
2020-10-19 01:04:37 +00:00
|
|
|
Size: int64(meta.GetPayloadSize()),
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
func nameFromObject(o *object.Object) (string, string) {
|
|
|
|
var name = o.GetID().String()
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
for _, attr := range o.GetAttributes() {
|
2020-10-23 00:12:37 +00:00
|
|
|
if attr.GetKey() == object.AttributeFileName {
|
2020-10-19 01:04:37 +00:00
|
|
|
name = attr.GetValue()
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
break
|
|
|
|
}
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
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
|
|
|
|
|
|
|
func (o *ObjectInfo) ID() *object.ID { return o.id }
|