2020-07-03 13:52:04 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2020-08-03 11:48:33 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
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 {
|
|
|
|
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-08-03 11:48:33 +00:00
|
|
|
func objectInfoFromMeta(meta *object.Object) *ObjectInfo {
|
2020-10-19 01:04:37 +00:00
|
|
|
aws3name := 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-22 00:19:16 +00:00
|
|
|
if name, ok := userHeaders[ObjectName]; ok {
|
2020-07-03 13:52:04 +00:00
|
|
|
aws3name = name
|
|
|
|
delete(userHeaders, name)
|
|
|
|
}
|
|
|
|
|
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-19 01:04:37 +00:00
|
|
|
Bucket: meta.GetContainerID().String(),
|
2020-07-03 13:52:04 +00:00
|
|
|
Name: aws3name,
|
2020-08-03 11:48:33 +00:00
|
|
|
ContentType: mimeType,
|
|
|
|
Headers: userHeaders,
|
2020-10-19 01:04:37 +00:00
|
|
|
Size: int64(meta.GetPayloadSize()),
|
|
|
|
Created: time.Now(), // time.Unix(meta.GetCreationEpoch(), 0),
|
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-22 00:19:16 +00:00
|
|
|
if attr.GetKey() == ObjectName {
|
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]
|
|
|
|
}
|