Skip to content

Commit

Permalink
Unifining some things
Browse files Browse the repository at this point in the history
  • Loading branch information
JarcauCristian committed Jan 9, 2024
1 parent df72a1d commit 714b2bb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
39 changes: 35 additions & 4 deletions instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,24 +653,55 @@ func (minioInstance *MinIO) uploadFile(reader io.Reader, tags map[string]string,
return nil, err
}

result := map[string]string{"location": object.Bucket + "/" + fileName, "size": strconv.Itoa(int(object.Size))}
result := map[string]string{"location": targetSite + "/" + object.Bucket + "/" + fileName, "size": strconv.Itoa(int(object.Size))}

return result, nil
}

func (minioInstance *MinIO) getDirectObject(datasetPath string) (*minio.Object, error) {
healthyInstances, err := minioInstance.Healths()

if err != nil {
return &minio.Object{}, err
}

targetSite := strings.Split(datasetPath, "/")[0] + "//" + strings.Split(datasetPath, "/")[2]
fmt.Println(targetSite)
find := false
for k, _ := range healthyInstances {
if k == targetSite {
find = true
}
}

if !find {
return nil, errors.New("could not find the target minio instance inside the healthy ones")
}

bucketName := strings.Split(datasetPath, "/")[3]
objectPath := strings.Join(strings.Split(datasetPath, "/")[4:], "/")

reader, err := minioInstance.clients[targetSite].GetObject(
context.Background(),
bucketName,
objectPath,
minio.GetObjectOptions{},
)

return reader, nil
}

func (minioInstance *MinIO) getObject(url string, datasetPath string, forever bool) (string, error) {
path := fmt.Sprintf("%s/%s", minioInstance.aliases[url], datasetPath)

var expirationTime string

if forever {
expirationTime = "366d"
expirationTime = "7d"
} else {
expirationTime = "10m"
}

fmt.Println(expirationTime)

cmdArgs := []string{"./mc", "share", "download", "--expire", expirationTime, "--json", path}

cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
Expand Down
31 changes: 28 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,33 @@ func main() {
}
})

r.GET("/get/object", func(c *gin.Context) {

datasetPath, exists := c.GetQuery("path")

if !exists {
c.JSON(400, gin.H{
"message": "dataset_path parameter is required!",
})
} else {
data, err := minio.getDirectObject(datasetPath)

if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer data.Close()

c.Header("Content-Disposition", "attachment; filename=downloaded_file.csv")

_, err = io.Copy(c.Writer, data)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
}
})

r.GET("/list_location", func(c *gin.Context) {
path, exists := c.GetQuery("path")

Expand Down Expand Up @@ -509,9 +536,7 @@ func main() {
"message": "Something happened when trying to upload the file!",
})
} else {
c.JSON(201, gin.H{
"message": result,
})
c.JSON(201, result)
}
})

Expand Down

0 comments on commit 714b2bb

Please sign in to comment.