diff --git a/cmd/server.go b/cmd/server.go index 1ba060d..5a75c0d 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -116,7 +116,7 @@ func RunServer() { groupV1.GET("/bucket/:bucket", handleV1BucketRead) - groupV1.POST("/bucket/:bucket/:document", handleV1DocumentCreate) + groupV1.POST("/bucket/:bucket", handleV1DocumentCreate) groupV1.GET("/bucket/:bucket/:document", handleV1DocumentRead) @@ -305,8 +305,59 @@ func handleV1BucketRead(c echo.Context) error { } func handleV1DocumentCreate(c echo.Context) error { - return c.JSON(http.StatusNotImplemented, map[string]string{ - "message": "Not Implemented", + documents_endpoint := viper.GetString("server.documents.endpoint") + documents_access_key_id := viper.GetString("server.documents.access_key_id") + documents_secret_access_key := viper.GetString("server.documents.secret_access_key") + documents_use_ssl := viper.GetBool("server.documents.use_ssl") + + //bucket := c.FormValue("bucket") + bucket := c.Param("bucket") + + form_file, err := c.FormFile("file") + if err != nil { + return err + } + + ctx, cancel := context.WithCancel(context.Background()) + + defer cancel() + + // Initialize minio client object + client, err := minio.New(documents_endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""), + Secure: documents_use_ssl, + }) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Error during minio#New", + }) + } + + src, err := form_file.Open() + if err != nil { + return err + } + defer src.Close() + + info, err := client.PutObject(ctx, bucket, form_file.Filename, src, form_file.Size, minio.PutObjectOptions{ + ContentType: form_file.Header.Get("Content-Type"), + }) + if err != nil { + //TODO + log.Println(err) + + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Error during minio#PutObject", + }) + } + + return c.JSON(http.StatusOK, map[string]interface{}{ + "message": "ok", + "info": map[string]interface{}{ + "bucket": info.Bucket, + "key": info.Key, + "size": info.Size, + }, }) }