diff --git a/cmd/server.go b/cmd/server.go index c56b1a8..e5d07f6 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -18,7 +18,6 @@ import ( "sort" "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -281,21 +280,18 @@ func handleV1Seed(c echo.Context) error { // handleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent. func handleV1BucketList(c echo.Context) error { - // Initialize minio client object - client, err := minio.New(cfg.Server.Documents.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(cfg.Server.Documents.AccessKeyId, cfg.Server.Documents.SecretAccessKey, ""), - Secure: cfg.Server.Documents.UseSSL, - }) + mediaClient, err := media.NewMediaClientFromViper() if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during minio#New", + "message": "Error during media.NewMediaClientFromViper()", + "error": err.Error(), }) } var buckets []string for _, bucket_name := range cfg.Server.Documents.Buckets { - exists, err := client.BucketExists(context.Background(), bucket_name) + exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket_name) if err != nil { return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") } @@ -331,18 +327,15 @@ func handleV1BucketRead(c echo.Context) error { defer cancel() - // Initialize minio client object - client, err := minio.New(cfg.Server.Documents.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(cfg.Server.Documents.AccessKeyId, cfg.Server.Documents.SecretAccessKey, ""), - Secure: cfg.Server.Documents.UseSSL, - }) + mediaClient, err := media.NewMediaClientFromViper() if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during minio#New", + "message": "Error during media.NewMediaClientFromViper()", + "error": err.Error(), }) } - exists, err := client.BucketExists(ctx, bucket) + exists, err := mediaClient.MinioClient.BucketExists(ctx, bucket) if err != nil { return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") } @@ -353,7 +346,7 @@ func handleV1BucketRead(c echo.Context) error { var keys []string - objectCh := client.ListObjects(ctx, bucket, minio.ListObjectsOptions{}) + objectCh := mediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{}) for object := range objectCh { if object.Err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ @@ -394,14 +387,11 @@ func handleV1DocumentCreate(c echo.Context) error { defer cancel() - // Initialize minio client object - client, err := minio.New(cfg.Server.Documents.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(cfg.Server.Documents.AccessKeyId, cfg.Server.Documents.SecretAccessKey, ""), - Secure: cfg.Server.Documents.UseSSL, - }) + mediaClient, err := media.NewMediaClientFromViper() if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during minio#New", + "message": "Error during media.NewMediaClientFromViper()", + "error": err.Error(), }) } @@ -418,7 +408,7 @@ func handleV1DocumentCreate(c echo.Context) error { filename_processed := reg.ReplaceAllString(form_file.Filename, "") - info, err := client.PutObject(ctx, bucket, filename_processed, src, form_file.Size, minio.PutObjectOptions{ + info, err := mediaClient.MinioClient.PutObject(ctx, bucket, filename_processed, src, form_file.Size, minio.PutObjectOptions{ ContentType: form_file.Header.Get("Content-Type"), }) if err != nil { @@ -462,18 +452,15 @@ func handleV1DocumentRead(c echo.Context) error { defer cancel() - // Initialize minio client object - client, err := minio.New(cfg.Server.Documents.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(cfg.Server.Documents.AccessKeyId, cfg.Server.Documents.SecretAccessKey, ""), - Secure: cfg.Server.Documents.UseSSL, - }) + mediaClient, err := media.NewMediaClientFromViper() if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during minio#New", + "message": "Error during media.NewMediaClientFromViper()", + "error": err.Error(), }) } - bucket_exists, err := client.BucketExists(ctx, bucket) + bucket_exists, err := mediaClient.MinioClient.BucketExists(ctx, bucket) if err != nil { return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") } @@ -482,7 +469,7 @@ func handleV1DocumentRead(c echo.Context) error { return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) } - document_info, err := client.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) + document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) if err != nil { if err.Error() == "The specified key does not exist." { @@ -497,7 +484,7 @@ func handleV1DocumentRead(c echo.Context) error { _ = document_info - document_object, err := client.GetObject(ctx, bucket, document, minio.GetObjectOptions{}) + 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", @@ -541,18 +528,15 @@ func handleV1DocumentDelete(c echo.Context) error { defer cancel() - // Initialize minio client object - client, err := minio.New(cfg.Server.Documents.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(cfg.Server.Documents.AccessKeyId, cfg.Server.Documents.SecretAccessKey, ""), - Secure: cfg.Server.Documents.UseSSL, - }) + mediaClient, err := media.NewMediaClientFromViper() if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during minio#New", + "message": "Error during media.NewMediaClientFromViper()", + "error": err.Error(), }) } - bucket_exists, err := client.BucketExists(ctx, bucket) + bucket_exists, err := mediaClient.MinioClient.BucketExists(ctx, bucket) if err != nil { return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") } @@ -561,7 +545,7 @@ func handleV1DocumentDelete(c echo.Context) error { return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) } - document_info, err := client.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) + document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) if err != nil { if err.Error() == "The specified key does not exist." { @@ -576,7 +560,7 @@ func handleV1DocumentDelete(c echo.Context) error { //TODO Add error validation _ = document_info - err = client.RemoveObject(ctx, bucket, document, minio.RemoveObjectOptions{}) + 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",