2023-08-20 16:19:05 -04:00
|
|
|
package api_handlers
|
2023-07-24 14:28:55 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"git.agecem.com/agecem/agecem-org/config"
|
|
|
|
"git.agecem.com/agecem/agecem-org/media"
|
2023-08-19 19:58:09 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/models"
|
2023-07-24 14:28:55 -04:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// API Handlers
|
|
|
|
|
|
|
|
// HandleV1 affiche les routes accessibles.
|
|
|
|
// Les routes sont triées selon .Path, pour les rendre plus facilement navigables.
|
|
|
|
func HandleV1(c echo.Context) error {
|
|
|
|
routes := c.Echo().Routes()
|
|
|
|
sort.Slice(routes, func(i, j int) bool { return routes[i].Path < routes[j].Path })
|
|
|
|
return c.JSON(http.StatusOK, routes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleV1Seed créé des buckets dans minio selon la liste de buckets dans server.documents.buckets
|
|
|
|
// Les buckets sont créés avec paramètres par défaut, et sont ensuite visible dans /v1/bucket.
|
|
|
|
func HandleV1Seed(c echo.Context) error {
|
2023-08-19 19:58:09 -04:00
|
|
|
var response models.V1SeedResponse
|
|
|
|
|
2023-07-24 14:28:55 -04:00
|
|
|
mediaClient, err := media.NewMediaClientFromViper()
|
|
|
|
if err != nil {
|
2023-08-19 19:58:09 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during media.NewMediaClientFromViper()"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
new_buckets, err := mediaClient.Seed()
|
2023-08-19 19:58:09 -04:00
|
|
|
response.Data.Buckets = new_buckets
|
2023-07-24 14:28:55 -04:00
|
|
|
if err != nil {
|
2023-08-19 19:58:09 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during mediaClient.Seed()"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(new_buckets) == 0 {
|
2023-08-19 19:58:09 -04:00
|
|
|
response.Message = "All buckets already exist"
|
2023-07-24 14:28:55 -04:00
|
|
|
|
|
|
|
} else {
|
2023-08-19 19:58:09 -04:00
|
|
|
response.Message = "Buckets successfully created"
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
2023-08-19 19:58:09 -04:00
|
|
|
response.StatusCode = http.StatusOK
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent.
|
|
|
|
func HandleV1BucketList(c echo.Context) error {
|
2023-08-20 17:57:14 -04:00
|
|
|
var response models.V1BucketListResponse
|
|
|
|
|
2023-07-24 14:28:55 -04:00
|
|
|
var cfg config.Config
|
|
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
2023-08-20 17:57:14 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during viper.Unmarshal"
|
|
|
|
// response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mediaClient, err := media.NewMediaClientFromViper()
|
|
|
|
if err != nil {
|
2023-08-20 17:57:14 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during media.NewMediaClientFromViper()"
|
|
|
|
// response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:52:03 -04:00
|
|
|
var buckets = make(map[string]string)
|
2023-07-24 14:28:55 -04:00
|
|
|
|
2023-08-15 15:52:03 -04:00
|
|
|
for bucket_name, bucket_display_name := range cfg.Server.Documents.Buckets {
|
2023-07-24 14:28:55 -04:00
|
|
|
exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket_name)
|
|
|
|
if err != nil {
|
2023-08-20 17:57:14 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during minio#BucketExists"
|
|
|
|
// response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2023-08-15 15:52:03 -04:00
|
|
|
buckets[bucket_name] = bucket_display_name
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 17:57:14 -04:00
|
|
|
response.StatusCode = http.StatusOK
|
|
|
|
response.Message = "Buckets list successful"
|
|
|
|
response.Data.Buckets = buckets
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
|
|
|
//return c.JSON(response.StatusCode, response.Data.Buckets)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleV1BucketRead(c echo.Context) error {
|
2023-08-22 15:08:10 -04:00
|
|
|
var response models.V1BucketReadResponse
|
|
|
|
|
2023-07-24 14:28:55 -04:00
|
|
|
var cfg config.Config
|
|
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
2023-08-22 15:08:10 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bucket := c.Param("bucket")
|
|
|
|
|
|
|
|
allowed := false
|
2023-08-15 15:52:03 -04:00
|
|
|
for bucket_allowed := range cfg.Server.Documents.Buckets {
|
2023-07-24 14:28:55 -04:00
|
|
|
if bucket == bucket_allowed {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
mediaClient, err := media.NewMediaClientFromViper()
|
|
|
|
if err != nil {
|
2023-08-22 15:08:10 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during media.NewMediaClientFromViper()"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := mediaClient.MinioClient.BucketExists(ctx, bucket)
|
|
|
|
if err != nil {
|
2023-08-22 15:08:10 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during minio#BucketExists"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
objectCh := mediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
|
|
|
|
for object := range objectCh {
|
|
|
|
if object.Err != nil {
|
2023-08-22 15:08:10 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during minio#ListObjects"
|
|
|
|
//TODO make sure this is safe
|
|
|
|
//response.Error = object.Err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:08:10 -04:00
|
|
|
response.Data.Keys = append(response.Data.Keys, object.Key)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:08:10 -04:00
|
|
|
response.StatusCode = http.StatusOK
|
|
|
|
response.Message = "V1BucketRead ok"
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data
|
|
|
|
func HandleV1DocumentCreate(c echo.Context) error {
|
2023-08-23 15:26:01 -04:00
|
|
|
var response models.V1DocumentCreateResponse
|
|
|
|
|
2023-07-24 14:28:55 -04:00
|
|
|
var cfg config.Config
|
|
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
2023-08-23 15:26:01 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during HandleV1DocumentCreate's echo#Context.FormFile"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bucket := c.Param("bucket")
|
|
|
|
|
|
|
|
form_file, err := c.FormFile("document")
|
|
|
|
if err != nil {
|
2023-08-23 15:26:01 -04:00
|
|
|
response.StatusCode = http.StatusBadRequest
|
|
|
|
response.Message = "Error during HandleV1DocumentCreate's echo#Context.FormFile"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
allowed := false
|
2023-08-15 15:52:03 -04:00
|
|
|
for bucket_allowed := range cfg.Server.Documents.Buckets {
|
2023-07-24 14:28:55 -04:00
|
|
|
if bucket == bucket_allowed {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
mediaClient, err := media.NewMediaClientFromViper()
|
|
|
|
if err != nil {
|
2023-08-23 15:26:01 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during media.NewMediaClientFromViper()"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
src, err := form_file.Open()
|
|
|
|
if err != nil {
|
2023-08-23 15:26:01 -04:00
|
|
|
response.StatusCode = http.StatusBadRequest
|
|
|
|
response.Message = "Error during form_file.Open()"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
/*
|
|
|
|
reg, err := regexp.Compile("[^.a-zA-Z0-9_-]+")
|
|
|
|
if err != nil {
|
|
|
|
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename_processed := reg.ReplaceAllString(form_file.Filename, "")
|
|
|
|
*/
|
|
|
|
|
|
|
|
info, err := mediaClient.MinioClient.PutObject(ctx, bucket, form_file.Filename, src, form_file.Size, minio.PutObjectOptions{
|
|
|
|
ContentType: form_file.Header.Get("Content-Type"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-08-23 15:26:01 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Error during minio#PutObject"
|
|
|
|
//response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
response.StatusCode = http.StatusOK
|
2023-08-23 16:01:57 -04:00
|
|
|
response.Message = "ok"
|
2023-08-23 15:26:01 -04:00
|
|
|
response.Data.Bucket = info.Bucket
|
|
|
|
response.Data.Key = info.Key
|
|
|
|
response.Data.Size = info.Size
|
2023-08-23 15:43:10 -04:00
|
|
|
|
2023-08-23 15:26:01 -04:00
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleV1DocumentRead permet de lire le contenu d'un fichier et protentiellement de le télécharger
|
|
|
|
func HandleV1DocumentRead(c echo.Context) error {
|
|
|
|
var cfg config.Config
|
|
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := c.Param("bucket")
|
|
|
|
document := c.Param("document")
|
|
|
|
|
|
|
|
allowed := false
|
2023-08-15 15:52:03 -04:00
|
|
|
for bucket_allowed := range cfg.Server.Documents.Buckets {
|
2023-07-24 14:28:55 -04:00
|
|
|
if bucket == bucket_allowed {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
mediaClient, err := media.NewMediaClientFromViper()
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during media.NewMediaClientFromViper()",
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket_exists, err := mediaClient.MinioClient.BucketExists(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bucket_exists {
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "The specified key does not exist." {
|
|
|
|
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
|
|
"message": "Error during minio#StatObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = document_info
|
|
|
|
|
|
|
|
document_object, err := mediaClient.MinioClient.GetObject(ctx, bucket, document, minio.GetObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#GetObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
defer document_object.Close()
|
|
|
|
|
|
|
|
return c.Stream(http.StatusOK, document_info.ContentType, document_object)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename
|
|
|
|
func HandleV1DocumentUpdate(c echo.Context) error {
|
2023-08-23 15:43:10 -04:00
|
|
|
return c.JSON(models.NotImplementedResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleV1DocumentDelete permet de supprimer un object
|
|
|
|
func HandleV1DocumentDelete(c echo.Context) error {
|
|
|
|
var cfg config.Config
|
|
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
bucket := c.Param("bucket")
|
|
|
|
document := c.Param("document")
|
|
|
|
|
|
|
|
allowed := false
|
2023-08-15 15:52:03 -04:00
|
|
|
for bucket_allowed := range cfg.Server.Documents.Buckets {
|
2023-07-24 14:28:55 -04:00
|
|
|
if bucket == bucket_allowed {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
mediaClient, err := media.NewMediaClientFromViper()
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during media.NewMediaClientFromViper()",
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket_exists, err := mediaClient.MinioClient.BucketExists(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bucket_exists {
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "The specified key does not exist." {
|
|
|
|
|
2023-08-23 16:01:57 -04:00
|
|
|
return c.JSON(models.NotFoundResponse())
|
2023-07-24 14:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
|
|
"message": "Error during minio#StatObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO Add error validation
|
|
|
|
_ = document_info
|
|
|
|
|
|
|
|
err = mediaClient.MinioClient.RemoveObject(ctx, bucket, document, minio.RemoveObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#RemoveObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, map[string]string{
|
|
|
|
"message": "Document deleted",
|
|
|
|
})
|
|
|
|
}
|