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:
Harshavardhana
2017-04-26 23:27:48 -07:00
committed by GitHub
parent cf1fc45142
commit 57c5c75611
4 changed files with 100 additions and 75 deletions

View File

@@ -17,6 +17,7 @@
package cmd
import (
"net"
"net/http"
"strings"
"time"
@@ -224,3 +225,36 @@ func canonicalizeETag(etag string) string {
func isETagEqual(left, right string) bool {
return canonicalizeETag(left) == canonicalizeETag(right)
}
// deleteObject is a convenient wrapper to delete an object, this
// is a common function to be called from object handlers and
// web handlers.
func deleteObject(obj ObjectLayer, bucket, object string, r *http.Request) (err error) {
// Acquire a write lock before deleting the object.
objectLock := globalNSMutex.NewNSLock(bucket, object)
objectLock.Lock()
defer objectLock.Unlock()
// Proceed to delete the object.
if err = obj.DeleteObject(bucket, object); err != nil {
return err
}
// Get host and port from Request.RemoteAddr.
host, port, _ := net.SplitHostPort(r.RemoteAddr)
// Notify object deleted event.
eventNotify(eventData{
Type: ObjectRemovedDelete,
Bucket: bucket,
ObjInfo: ObjectInfo{
Name: object,
},
ReqParams: extractReqParams(r),
UserAgent: r.UserAgent(),
Host: host,
Port: port,
})
return nil
}