Merge pull request #218 from harshavardhana/pr_out_implement_proper_delimiter_and_prefix_handling
This commit is contained in:
commit
20dc738b67
5 changed files with 84 additions and 37 deletions
|
@ -45,7 +45,7 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ
|
|||
switch err := err.(type) {
|
||||
case nil: // success
|
||||
{
|
||||
response := generateObjectsListResult(bucket, objects, resources.IsTruncated)
|
||||
response := generateObjectsListResult(bucket, objects, resources)
|
||||
w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType))
|
||||
}
|
||||
case mstorage.BucketNotFound:
|
||||
|
|
|
@ -34,10 +34,8 @@ type ObjectListResponse struct {
|
|||
MaxKeys int
|
||||
Delimiter string
|
||||
IsTruncated bool
|
||||
Contents []*Item `xml:,innerxml`
|
||||
CommonPrefixes struct {
|
||||
Prefix string
|
||||
} `xml:,innerxml`
|
||||
Contents []*Item `xml:,innerxml`
|
||||
CommonPrefixes []*Prefix `xml:,innerxml`
|
||||
}
|
||||
|
||||
// Bucket list response format
|
||||
|
@ -49,6 +47,10 @@ type BucketListResponse struct {
|
|||
} `xml:,innerxml` // Buckets are nested
|
||||
}
|
||||
|
||||
type Prefix struct {
|
||||
Prefix string
|
||||
}
|
||||
|
||||
// Bucket struct
|
||||
type Bucket struct {
|
||||
Name string
|
||||
|
|
|
@ -68,8 +68,9 @@ func (b ItemKey) Less(i, j int) bool { return b[i].Key < b[j].Key }
|
|||
//
|
||||
// output:
|
||||
// populated struct that can be serialized to match xml and json api spec output
|
||||
func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata, isTruncated bool) ObjectListResponse {
|
||||
func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata, bucketResources mstorage.BucketResourcesMetadata) ObjectListResponse {
|
||||
var contents []*Item
|
||||
var prefixes []*Prefix
|
||||
var owner = Owner{}
|
||||
var data = ObjectListResponse{}
|
||||
|
||||
|
@ -78,6 +79,9 @@ func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata,
|
|||
|
||||
for _, object := range objects {
|
||||
var content = &Item{}
|
||||
if object.Key == "" {
|
||||
continue
|
||||
}
|
||||
content.Key = object.Key
|
||||
content.LastModified = object.Created.Format(dateFormat)
|
||||
content.ETag = object.ETag
|
||||
|
@ -89,7 +93,16 @@ func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata,
|
|||
sort.Sort(ItemKey(contents))
|
||||
data.Name = bucket
|
||||
data.Contents = contents
|
||||
data.MaxKeys = MAX_OBJECT_LIST
|
||||
data.IsTruncated = isTruncated
|
||||
data.MaxKeys = bucketResources.Maxkeys
|
||||
data.Prefix = bucketResources.Prefix
|
||||
data.Delimiter = bucketResources.Delimiter
|
||||
data.Marker = bucketResources.Marker
|
||||
data.IsTruncated = bucketResources.IsTruncated
|
||||
for _, prefix := range bucketResources.CommonPrefixes {
|
||||
var prefixItem = &Prefix{}
|
||||
prefixItem.Prefix = prefix
|
||||
prefixes = append(prefixes, prefixItem)
|
||||
}
|
||||
data.CommonPrefixes = prefixes
|
||||
return data
|
||||
}
|
||||
|
|
|
@ -314,6 +314,29 @@ func (p *Path) getAllFiles(path string, fl os.FileInfo, err error) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func delimiter(path, delimiter string) string {
|
||||
delimited := ""
|
||||
if !strings.Contains(path, delimiter) {
|
||||
return delimited
|
||||
}
|
||||
index := strings.Index(path, delimiter)
|
||||
if index == -1 {
|
||||
return delimited
|
||||
}
|
||||
delimitedIndex := index + len(delimiter)
|
||||
delimited = path[:delimitedIndex]
|
||||
return delimited
|
||||
}
|
||||
|
||||
func appendU(slice []string, i string) []string {
|
||||
for _, ele := range slice {
|
||||
if ele == i {
|
||||
return slice
|
||||
}
|
||||
}
|
||||
return append(slice, i)
|
||||
}
|
||||
|
||||
type ByObjectKey []mstorage.ObjectMetadata
|
||||
|
||||
func (b ByObjectKey) Len() int { return len(b) }
|
||||
|
@ -350,31 +373,44 @@ func (storage *storage) ListObjects(bucket string, resources mstorage.BucketReso
|
|||
goto ret
|
||||
}
|
||||
// TODO handle resources.Marker
|
||||
if resources.Delimiter != "" {
|
||||
metadata := mstorage.ObjectMetadata{
|
||||
Bucket: bucket,
|
||||
Maxkeys: resources.Maxkeys,
|
||||
Prefix: resources.Prefix,
|
||||
Marker: resources.Marker,
|
||||
Delimiter: resources.Delimiter,
|
||||
switch true {
|
||||
case resources.Delimiter != "" && resources.Prefix == "":
|
||||
delimited := delimiter(name, resources.Delimiter)
|
||||
switch true {
|
||||
case delimited == "":
|
||||
metadata := mstorage.ObjectMetadata{
|
||||
Bucket: bucket,
|
||||
Key: name,
|
||||
Created: file.ModTime(),
|
||||
Size: file.Size(),
|
||||
ETag: bucket + "#" + name,
|
||||
}
|
||||
metadataList = append(metadataList, metadata)
|
||||
case delimited != "":
|
||||
resources.CommonPrefixes = appendU(resources.CommonPrefixes, delimited)
|
||||
}
|
||||
metadataList = append(metadataList, metadata)
|
||||
}
|
||||
if resources.Delimiter != "" && strings.HasPrefix(name, resources.Prefix) {
|
||||
metadata := mstorage.ObjectMetadata{}
|
||||
metadataList = append(metadataList, metadata)
|
||||
}
|
||||
if strings.HasPrefix(name, resources.Prefix) {
|
||||
case resources.Delimiter != "" && strings.HasPrefix(name, resources.Prefix):
|
||||
delimited := delimiter(name, resources.Delimiter)
|
||||
switch true {
|
||||
case delimited == "":
|
||||
metadata := mstorage.ObjectMetadata{
|
||||
Bucket: bucket,
|
||||
Key: name,
|
||||
Created: file.ModTime(),
|
||||
Size: file.Size(),
|
||||
ETag: bucket + "#" + name,
|
||||
}
|
||||
metadataList = append(metadataList, metadata)
|
||||
case delimited != "":
|
||||
resources.CommonPrefixes = appendU(resources.CommonPrefixes, delimited)
|
||||
}
|
||||
case strings.HasPrefix(name, resources.Prefix):
|
||||
metadata := mstorage.ObjectMetadata{
|
||||
Bucket: bucket,
|
||||
Maxkeys: resources.Maxkeys,
|
||||
Prefix: resources.Prefix,
|
||||
Marker: resources.Marker,
|
||||
Delimiter: resources.Delimiter,
|
||||
Key: name,
|
||||
Created: file.ModTime(),
|
||||
Size: file.Size(),
|
||||
ETag: bucket + "#" + name,
|
||||
Bucket: bucket,
|
||||
Key: name,
|
||||
Created: file.ModTime(),
|
||||
Size: file.Size(),
|
||||
ETag: bucket + "#" + name,
|
||||
}
|
||||
metadataList = append(metadataList, metadata)
|
||||
}
|
||||
|
|
|
@ -43,12 +43,8 @@ type BucketMetadata struct {
|
|||
}
|
||||
|
||||
type ObjectMetadata struct {
|
||||
Bucket string
|
||||
Key string
|
||||
Maxkeys int
|
||||
Prefix string
|
||||
Marker string
|
||||
Delimiter string
|
||||
Bucket string
|
||||
Key string
|
||||
|
||||
ContentType string
|
||||
Created time.Time
|
||||
|
|
Loading…
Reference in a new issue