From ecef80a30928a544a1481586fcd2594818427fcd Mon Sep 17 00:00:00 2001 From: JarcauCristian Date: Tue, 16 Jan 2024 14:33:28 +0200 Subject: [PATCH] Added path deleteion --- instances.go | 41 ++++++++++++++++++++++++++++++++++++++++- main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/instances.go b/instances.go index d97b21c..f976244 100644 --- a/instances.go +++ b/instances.go @@ -548,6 +548,46 @@ func (minioInstance *MinIO) putObject(content []byte, fileName string, tags map[ return targetSite + "=" + object.Bucket + "=" + fileName, nil } +func (minioInstance *MinIO) deleteFile(datasetPath string) error { + var mp map[string]interface{} + + healthyInstances, err := minioInstance.Healths() + if err != nil { + return err + } + + errorCounter := 0 + + for _, v := range healthyInstances { + cmdArgs := []string{"./mc", "rm", "--recursive", v + "/" + datasetPath, "--json"} + + cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) + var stdout bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + err := cmd.Run() + if err != nil { + return err + } + + err = json.Unmarshal(stdout.Bytes(), &mp) + if err != nil { + return err + } + + if mp["status"].(string) == "error" { + errorCounter++ + } + } + + if errorCounter > 0 { + return errors.New("could not delete all the found paths") + } + + return nil +} + func (minioInstance *MinIO) uploadFile(reader io.Reader, tags map[string]string, fileSize float64, fileName string, contentType string, temporary bool) (map[string]string, error) { if minioInstance.robinIndex == minioInstance.currentIndex-1 { minioInstance.robinIndex = 0 @@ -668,7 +708,6 @@ func (minioInstance *MinIO) getDirectObject(datasetPath string) (*minio.Object, } targetSite := strings.Split(datasetPath, "/")[0] + "//" + strings.Split(datasetPath, "/")[2] - fmt.Println(targetSite) find := false for k, _ := range healthyInstances { if k == targetSite { diff --git a/main.go b/main.go index 183717d..fc0b71c 100644 --- a/main.go +++ b/main.go @@ -542,6 +542,52 @@ func main() { } }) + r.DELETE("/delete_path", func(c *gin.Context) { + authorization := c.Request.Header["Authorization"] + + if len(authorization) == 0 { + c.JSON(400, gin.H{ + "message": "You need to pass the authorization header!", + }) + } else { + tokenString := strings.Split(c.Request.Header["Authorization"][0], " ")[1] + + if tokenString == "" { + c.JSON(401, gin.H{ + "message": "You are unauthorized!", + }) + } + + verification := verifyToken(tokenString) + + if !verification { + c.JSON(401, gin.H{ + "message": "You are unauthorized!", + }) + } else { + path, exists := c.GetQuery("path") + + if !exists { + c.JSON(400, gin.H{ + "message": "Required parameter path not provided!", + }) + } else { + err := minio.deleteFile(path) + + if err != nil { + c.JSON(500, gin.H{ + "message": "Something happened when deleting the path!", + }) + } else { + c.JSON(200, gin.H{ + "message": "Path deleted successfully!", + }) + } + } + } + } + }) + err = r.Run(":9000") if err != nil { return