diff --git a/helper_functions.go b/helper_functions.go index 23ded9a..bbdd62e 100644 --- a/helper_functions.go +++ b/helper_functions.go @@ -71,6 +71,34 @@ func searchTags(alias []string, tags map[string]string) (map[string][]string, er return map[string][]string{alias[0]: findings}, nil } +func listMinioPath(alias []string, path string) ([]string, error) { + cmdArgs := []string{"./mc", "ls", alias[1] + "/" + path, "--json"} + cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) + var stdout bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return nil, err + } + scanner := bufio.NewScanner(&stdout) + + var files []string + for scanner.Scan() { + line := scanner.Text() + var data map[string]interface{} + if err := json.Unmarshal([]byte(line), &data); err != nil { + return nil, err + } + + if data["status"].(string) == "success" && data["type"].(string) == "file" { + files = append(files, data["key"].(string)) + } + } + + return files, nil +} + func searchContentType(alias []string, contentType string) (map[string][]string, error) { cmdArgs := []string{"./mc", "find", alias[1], fmt.Sprintf("--metadata=Content-Type=%s", contentType)} diff --git a/instances.go b/instances.go index 62392db..31d7b8f 100644 --- a/instances.go +++ b/instances.go @@ -198,6 +198,46 @@ func (minioInstance *MinIO) Healths() (map[string]string, error) { return health, nil } +func (minioInstance *MinIO) listPath(path string) ([]string, error) { + healthyInstances, err := minioInstance.Healths() + if err != nil { + return nil, err + } + + var findPath []string + + var wg sync.WaitGroup + + wg.Add(len(healthyInstances)) + for k, v := range healthyInstances { + alias := []string{k, v} + + go func(alias []string, datasetPath string) { + defer wg.Done() + + finding, err := search(alias, datasetPath) + + if err != nil { + fmt.Println("Is not present here!") + } + + if finding != "" { + findPath = []string{alias[0], alias[1]} + } + }(alias, path) + } + + wg.Wait() + + files, err := listMinioPath(findPath, path) + + if err != nil { + return nil, err + } + + return files, nil +} + func (minioInstance *MinIO) findObject(datasetPath string) (string, error) { healthyInstances, err := minioInstance.Healths() diff --git a/main.go b/main.go index bb846d4..4763f8b 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "io" "mime/multipart" @@ -22,12 +21,7 @@ func main() { r := gin.Default() r.MaxMultipartMemory = 100 << 20 - config := cors.DefaultConfig() - config.AllowOrigins = []string{"https://localhost:3000"} - config.AllowHeaders = []string{"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With"} - config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"} - - r.Use(cors.New(config)) + r.Use(CORSMiddleware()) r.GET("/get_all_objects", func(c *gin.Context) { extension, okExtension := c.GetQuery("extension") @@ -302,7 +296,7 @@ func main() { if !exists { c.JSON(400, gin.H{ - "message": "Body is incorrect!", + "message": "dataset_path parameter is required!", }) } else { data, err := minio.findObject(datasetPath) @@ -320,6 +314,28 @@ func main() { } }) + r.GET("/list_location", func(c *gin.Context) { + path, exists := c.GetQuery("path") + + if !exists { + c.JSON(400, gin.H{ + "message": "path parameter is required!", + }) + } else { + files, err := minio.listPath(path) + + if err != nil { + c.JSON(404, gin.H{ + "message": "Path not found!", + }) + } + + c.JSON(200, gin.H{ + "files": files, + }) + } + }) + r.PUT("/put_object", func(c *gin.Context) { authorization := c.Request.Header["Authorization"] @@ -444,7 +460,9 @@ func main() { for k, v := range tagData { mapTags[k] = v.(string) } + result, err := minio.uploadFile(reader, mapTags, float64(fileSize), fileName, contentType) + fmt.Println(result) if err != nil { c.JSON(500, gin.H{ @@ -467,7 +485,7 @@ func main() { func verifyToken(tokenString string) bool { client := &http.Client{} - req, _ := http.NewRequest("GET", "http://localhost:8080/auth/realms/react-keycloak/protocol/openid-connect/userinfo", nil) + req, _ := http.NewRequest("GET", "https://62.72.21.79:8442/auth/realms/react-keycloak/protocol/openid-connect/userinfo", nil) req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", tokenString)) response, err := client.Do(req) if err != nil { @@ -478,3 +496,19 @@ func verifyToken(tokenString string) bool { } return true } + +func CORSMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") + c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") + c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") + c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT") + + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(204) + return + } + + c.Next() + } +}