mirror of
https://github.com/minio/minio.git
synced 2025-11-23 11:07:50 -05:00
web: Simplify and converge common functions in web/obj API. (#4179)
RemoveObject() in webAPI currently re-implements some part of the code to remove objects combine them for simplicity and code convergence.
This commit is contained in:
@@ -255,16 +255,22 @@ func (web *webAPIHandlers) ListObjects(r *http.Request, args *ListObjectsArgs, r
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveObjectArgs - args to remove an object
|
||||
// JSON will look like:
|
||||
// '{"bucketname":"testbucket","objects":["photos/hawaii/","photos/maldives/","photos/sanjose.jpg"]}'
|
||||
// RemoveObjectArgs - args to remove an object, JSON will look like.
|
||||
//
|
||||
// {
|
||||
// "bucketname": "testbucket",
|
||||
// "objects": [
|
||||
// "photos/hawaii/",
|
||||
// "photos/maldives/",
|
||||
// "photos/sanjose.jpg"
|
||||
// ]
|
||||
// }
|
||||
type RemoveObjectArgs struct {
|
||||
Objects []string `json:"objects"` // can be files or sub-directories
|
||||
Prefix string `json:"prefix"` // current directory in the browser-ui
|
||||
BucketName string `json:"bucketname"` // bucket name.
|
||||
Objects []string `json:"objects"` // Contains objects, prefixes.
|
||||
BucketName string `json:"bucketname"` // Contains bucket name.
|
||||
}
|
||||
|
||||
// RemoveObject - removes an object.
|
||||
// RemoveObject - removes an object, or all the objects at a given prefix.
|
||||
func (web *webAPIHandlers) RemoveObject(r *http.Request, args *RemoveObjectArgs, reply *WebGenericRep) error {
|
||||
objectAPI := web.ObjectAPI()
|
||||
if objectAPI == nil {
|
||||
@@ -273,51 +279,35 @@ func (web *webAPIHandlers) RemoveObject(r *http.Request, args *RemoveObjectArgs,
|
||||
if !isHTTPRequestValid(r) {
|
||||
return toJSONError(errAuthentication)
|
||||
}
|
||||
|
||||
if args.BucketName == "" || len(args.Objects) == 0 {
|
||||
return toJSONError(errUnexpected)
|
||||
return toJSONError(errInvalidArgument)
|
||||
}
|
||||
|
||||
var err error
|
||||
objectLoop:
|
||||
for _, object := range args.Objects {
|
||||
remove := func(objectName string) error {
|
||||
objectLock := globalNSMutex.NewNSLock(args.BucketName, objectName)
|
||||
objectLock.Lock()
|
||||
defer objectLock.Unlock()
|
||||
err = objectAPI.DeleteObject(args.BucketName, objectName)
|
||||
if err == nil {
|
||||
// Notify object deleted event.
|
||||
eventNotify(eventData{
|
||||
Type: ObjectRemovedDelete,
|
||||
Bucket: args.BucketName,
|
||||
ObjInfo: ObjectInfo{
|
||||
Name: objectName,
|
||||
},
|
||||
ReqParams: extractReqParams(r),
|
||||
})
|
||||
}
|
||||
return err
|
||||
}
|
||||
if !hasSuffix(object, slashSeparator) {
|
||||
// If not a directory, remove the object.
|
||||
err = remove(object)
|
||||
if err != nil {
|
||||
break objectLoop
|
||||
next:
|
||||
for _, objectName := range args.Objects {
|
||||
// If not a directory, remove the object.
|
||||
if !hasSuffix(objectName, slashSeparator) && objectName != "" {
|
||||
if err = deleteObject(objectAPI, args.BucketName, objectName, r); err != nil {
|
||||
break next
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// For directories, list the contents recursively and remove.
|
||||
marker := ""
|
||||
for {
|
||||
var lo ListObjectsInfo
|
||||
lo, err = objectAPI.ListObjects(args.BucketName, object, marker, "", 1000)
|
||||
lo, err = objectAPI.ListObjects(args.BucketName, objectName, marker, "", 1000)
|
||||
if err != nil {
|
||||
break objectLoop
|
||||
break next
|
||||
}
|
||||
marker = lo.NextMarker
|
||||
for _, obj := range lo.Objects {
|
||||
err = remove(obj.Name)
|
||||
err = deleteObject(objectAPI, args.BucketName, obj.Name, r)
|
||||
if err != nil {
|
||||
break objectLoop
|
||||
break next
|
||||
}
|
||||
}
|
||||
if !lo.IsTruncated {
|
||||
@@ -972,6 +962,12 @@ func toWebAPIError(err error) APIError {
|
||||
HTTPStatusCode: http.StatusForbidden,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == errInvalidArgument {
|
||||
return APIError{
|
||||
Code: "InvalidArgument",
|
||||
HTTPStatusCode: http.StatusBadRequest,
|
||||
Description: err.Error(),
|
||||
}
|
||||
}
|
||||
// Convert error type to api error code.
|
||||
var apiErrCode APIErrorCode
|
||||
|
||||
Reference in New Issue
Block a user