agecem-org/apihandler/document.go

280 lines
7.5 KiB
Go
Raw Normal View History

package apihandler
import (
"context"
"fmt"
"net/http"
2023-11-20 16:29:10 -05:00
"git.agecem.com/agecem/agecem-org/apirequest"
"git.agecem.com/agecem/agecem-org/apiresponse"
"github.com/labstack/echo/v4"
"github.com/minio/minio-go/v7"
)
/*
V1DocumentsPOST permet d'ajouter un object dans un bucket, par multipart/form-data
Example:
Téléverser plusieurs fichiers à cette route avec `curl`:
curl <endpoint> -F 'documents=@example.pdf' -F 'documents=@example.md;type=text/markdown'
*/
func (h *V1Handler) V1DocumentsPOST(c echo.Context) (err error) {
var request apirequest.V1DocumentsPOST
var response apiresponse.V1DocumentsPOST
request.Params.Bucket = c.Param("bucket")
var allowed bool
for allowedBucket := range h.Config.Server.Documents.Buckets {
if request.Params.Bucket == allowedBucket {
allowed = true
}
}
if !allowed {
return c.JSON(apiresponse.NotFoundResponse())
}
form, err := c.MultipartForm()
if err != nil {
response.Message = fmt.Sprintf("Téléversement invalide: %s", err)
response.StatusCode = http.StatusBadRequest
return c.JSON(response.StatusCode, response)
}
if form == nil {
response.Message = "MultipartForm pointe vers une addresse mémoire nil"
response.StatusCode = http.StatusBadRequest
return c.JSON(response.StatusCode, response)
}
if len(form.File) == 0 {
response.Message = "Veuillez sélectionner au moins 1 document à téléverser"
response.StatusCode = http.StatusBadRequest
return c.JSON(response.StatusCode, response)
}
for inputName, inputFileHeaders := range form.File {
if inputName == "documents" {
request.Data.Documents = inputFileHeaders
}
}
if request.Data.Documents == nil {
response.Message = "Impossible d'obtenir les documents depuis le formulaire"
response.StatusCode = http.StatusBadRequest
return c.JSON(response.StatusCode, response)
}
if !request.Complete() {
response.Message = "Requête V1DocumentsPOST incomplète reçue"
response.StatusCode = http.StatusBadRequest
return c.JSON(response.StatusCode, response)
}
response.StatusCode, response.Message = h.MediaClient.UploadFormFiles(request.Params.Bucket, request.Data.Documents)
return c.JSON(response.StatusCode, response)
}
// V1DocumentPOST permet d'ajouter un object dans un bucket, par multipart/form-data
2023-11-20 16:29:10 -05:00
func (h *V1Handler) V1DocumentPOST(c echo.Context) (err error) {
var request apirequest.V1DocumentPOST
var response apiresponse.V1DocumentPOST
request.Params.Bucket = c.Param("bucket")
2023-11-20 16:29:10 -05:00
request.Data.Document, err = c.FormFile("document")
if err != nil {
response.StatusCode = http.StatusBadRequest
response.Message = "Error during HandleV1DocumentCreate's echo#Context.FormFile"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
allowed := false
for bucket_allowed := range h.Config.Server.Documents.Buckets {
if request.Params.Bucket == bucket_allowed {
allowed = true
}
}
if !allowed {
return c.JSON(apiresponse.NotFoundResponse())
}
2023-11-20 16:29:10 -05:00
if !request.Complete() {
response.Message = "Incomplete V1DocumentPOST request received"
response.StatusCode = http.StatusBadRequest
return c.JSON(response.StatusCode, response)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2023-11-20 16:29:10 -05:00
src, err := request.Data.Document.Open()
if err != nil {
response.StatusCode = http.StatusBadRequest
response.Message = "Error during form_file.Open()"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
defer src.Close()
info, err := h.MediaClient.MinioClient.PutObject(ctx, request.Params.Bucket, request.Data.Document.Filename, src, request.Data.Document.Size, minio.PutObjectOptions{
2023-11-20 16:29:10 -05:00
ContentType: request.Data.Document.Header.Get("Content-Type"),
})
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#PutObject"
//response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
response.StatusCode = http.StatusOK
response.Message = "ok"
response.Data.Bucket = info.Bucket
response.Data.Key = info.Key
response.Data.Size = info.Size
return c.JSON(response.StatusCode, response)
}
// V1DocumentGET permet de lire le contenu d'un fichier et protentiellement de le télécharger
func (h *V1Handler) V1DocumentGET(c echo.Context) error {
bucket := c.Param("bucket")
document := c.Param("document")
allowed := false
for bucket_allowed := range h.Config.Server.Documents.Buckets {
if bucket == bucket_allowed {
allowed = true
}
}
if !allowed {
return c.JSON(apiresponse.NotFoundResponse())
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bucket_exists, err := h.MediaClient.MinioClient.BucketExists(ctx, bucket)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
}
if !bucket_exists {
return c.JSON(apiresponse.NotFoundResponse())
}
document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
if err != nil {
if err.Error() == "The specified key does not exist." {
return c.JSON(apiresponse.NotFoundResponse())
}
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
"message": "Error during minio#StatObject",
})
}
_ = document_info
document_object, err := h.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)
}
// V1DocumentDELETE permet de supprimer un object
func (h *V1Handler) V1DocumentDELETE(c echo.Context) error {
2023-11-20 16:56:44 -05:00
var request apirequest.V1DocumentDELETE
var response apiresponse.V1DocumentDELETE
request.Params.Bucket = c.Param("bucket")
request.Params.Document = c.Param("document")
allowed := false
for bucket_allowed := range h.Config.Server.Documents.Buckets {
2023-11-20 16:56:44 -05:00
if request.Params.Bucket == bucket_allowed {
allowed = true
}
}
if !allowed {
return c.JSON(apiresponse.NotFoundResponse())
}
2023-11-20 16:56:44 -05:00
if !request.Complete() {
response.Message = "Incomplete V1DocumentDELETE request received"
response.StatusCode = http.StatusBadRequest
return c.JSON(response.StatusCode, response)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2023-11-20 16:56:44 -05:00
bucket_exists, err := h.MediaClient.MinioClient.BucketExists(ctx, request.Params.Bucket)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
}
if !bucket_exists {
return c.JSON(apiresponse.NotFoundResponse())
}
2023-11-20 16:56:44 -05:00
document_info, err := h.MediaClient.MinioClient.StatObject(ctx, request.Params.Bucket, request.Params.Document, minio.StatObjectOptions{})
if err != nil {
if err.Error() == "The specified key does not exist." {
return c.JSON(apiresponse.NotFoundResponse())
}
2023-11-20 16:56:44 -05:00
//response.Error = err.Error()
response.Message = "Error during minio#StatObject"
response.StatusCode = http.StatusInternalServerError
return c.JSON(response.StatusCode, response)
}
//TODO Add error validation
_ = document_info
2023-11-20 16:56:44 -05:00
err = h.MediaClient.MinioClient.RemoveObject(ctx, request.Params.Bucket, request.Params.Document, minio.RemoveObjectOptions{})
if err != nil {
2023-11-20 16:56:44 -05:00
//response.Error = err.Error()
response.Message = "Error during minio#RemoveObject"
response.StatusCode = http.StatusInternalServerError
return c.JSON(response.StatusCode, response)
}
2023-11-20 16:56:44 -05:00
response.Message = "Document deleted"
response.StatusCode = http.StatusOK
return c.JSON(response.StatusCode, response)
}