mirror of
https://github.com/minio/minio.git
synced 2024-12-26 07:05:55 -05:00
Merge pull request #218 from harshavardhana/pr_out_implement_proper_delimiter_and_prefix_handling
This commit is contained in:
commit
20dc738b67
@ -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:
|
||||
|
@ -35,9 +35,7 @@ type ObjectListResponse struct {
|
||||
Delimiter string
|
||||
IsTruncated bool
|
||||
Contents []*Item `xml:,innerxml`
|
||||
CommonPrefixes struct {
|
||||
Prefix string
|
||||
} `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,27 +373,40 @@ func (storage *storage) ListObjects(bucket string, resources mstorage.BucketReso
|
||||
goto ret
|
||||
}
|
||||
// TODO handle resources.Marker
|
||||
if resources.Delimiter != "" {
|
||||
switch true {
|
||||
case resources.Delimiter != "" && resources.Prefix == "":
|
||||
delimited := delimiter(name, resources.Delimiter)
|
||||
switch true {
|
||||
case delimited == "":
|
||||
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,
|
||||
}
|
||||
metadataList = append(metadataList, metadata)
|
||||
case delimited != "":
|
||||
resources.CommonPrefixes = appendU(resources.CommonPrefixes, delimited)
|
||||
}
|
||||
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(),
|
||||
|
@ -45,10 +45,6 @@ type BucketMetadata struct {
|
||||
type ObjectMetadata struct {
|
||||
Bucket string
|
||||
Key string
|
||||
Maxkeys int
|
||||
Prefix string
|
||||
Marker string
|
||||
Delimiter string
|
||||
|
||||
ContentType string
|
||||
Created time.Time
|
||||
|
Loading…
Reference in New Issue
Block a user