Ajouter upload de documents par API #40

Merged
vlbeaudoin merged 6 commits from feature/v1-document-upload into main 2023-04-24 18:41:31 -04:00

View file

@ -116,6 +116,14 @@ func RunServer() {
groupV1.GET("/bucket/:bucket", handleV1BucketRead) groupV1.GET("/bucket/:bucket", handleV1BucketRead)
groupV1.POST("/bucket/:bucket", handleV1DocumentCreate)
groupV1.GET("/bucket/:bucket/:document", handleV1DocumentRead)
groupV1.PUT("/bucket/:bucket/:document", handleV1DocumentUpdate)
groupV1.DELETE("/bucket/:bucket/:document", handleV1DocumentDelete)
// Static Routes // Static Routes
e.GET("/static/general.css", handleStaticCSSGeneral) e.GET("/static/general.css", handleStaticCSSGeneral)
@ -277,7 +285,7 @@ func handleV1BucketRead(c echo.Context) error {
} }
if !exists { if !exists {
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not found"}) return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
} }
var keys []string var keys []string
@ -296,6 +304,81 @@ func handleV1BucketRead(c echo.Context) error {
return c.JSON(http.StatusOK, keys) return c.JSON(http.StatusOK, keys)
} }
// handleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data
func handleV1DocumentCreate(c echo.Context) error {
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.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 {
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,
},
})
}
// handleV1DocumentRead permet de lire le contenu d'un fichier et protentiellement de le télécharger
func handleV1DocumentRead(c echo.Context) error {
return c.JSON(http.StatusNotImplemented, map[string]string{
"message": "Not Implemented",
})
}
// handleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename
func handleV1DocumentUpdate(c echo.Context) error {
return c.JSON(http.StatusNotImplemented, map[string]string{
"message": "Not Implemented",
})
}
// handleV1DocumentDelete permet de supprimer un object
func handleV1DocumentDelete(c echo.Context) error {
return c.JSON(http.StatusNotImplemented, map[string]string{
"message": "Not Implemented",
})
}
// HTML Handlers // HTML Handlers
func handleIndex(c echo.Context) error { func handleIndex(c echo.Context) error {