agecem-org/apihandler/document.go

400 lines
11 KiB
Go

package apihandler
import (
"context"
"fmt"
"net/http"
"codeberg.org/vlbeaudoin/voki/v3"
"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 {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
form, err := c.MultipartForm()
if err != nil {
response.Message = fmt.Sprintf("Téléversement invalide: %s", err)
response.SetStatusCode(http.StatusBadRequest)
return c.JSON(response.StatusCode(), response)
}
if form == nil {
response.Message = "MultipartForm pointe vers une addresse mémoire nil"
response.SetStatusCode(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.SetStatusCode(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.SetStatusCode(http.StatusBadRequest)
return c.JSON(response.StatusCode(), response)
}
if !request.Complete() {
response.Message = "Requête V1DocumentsPOST incomplète reçue"
response.SetStatusCode(http.StatusBadRequest)
return c.JSON(response.StatusCode(), response)
}
var code int
code, response.Message = h.MediaClient.UploadFormFiles(request.Params.Bucket, request.Data.Documents)
response.SetStatusCode(code)
return c.JSON(response.StatusCode(), response)
}
// V1DocumentPOST permet d'ajouter un object dans un bucket, par multipart/form-data
func (h *V1Handler) V1DocumentPOST(c echo.Context) (err error) {
var request apirequest.V1DocumentPOST
var response apiresponse.V1DocumentPOST
request.Params.Bucket = c.Param("bucket")
request.Data.Document, err = c.FormFile("document")
if err != nil {
response.SetStatusCode(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 {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
if !request.Complete() {
response.Message = "Incomplete V1DocumentPOST request received"
response.SetStatusCode(http.StatusBadRequest)
return c.JSON(response.StatusCode(), response)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
src, err := request.Data.Document.Open()
if err != nil {
response.SetStatusCode(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{
ContentType: request.Data.Document.Header.Get("Content-Type"),
})
if err != nil {
response.SetStatusCode(http.StatusInternalServerError)
response.Message = "Error during minio#PutObject"
//response.Error = err.Error()
return c.JSON(response.StatusCode(), response)
}
response.SetStatusCode(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 {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
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 {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
if err != nil {
if err.Error() == "The specified key does not exist." {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
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 {
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 {
if request.Params.Bucket == bucket_allowed {
allowed = true
}
}
if !allowed {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
if !request.Complete() {
response.Message = "Incomplete V1DocumentDELETE request received"
response.SetStatusCode(http.StatusBadRequest)
return c.JSON(response.StatusCode(), response)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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 {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
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." {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
//response.Error = err.Error()
response.Message = "Error during minio#StatObject"
response.SetStatusCode(http.StatusInternalServerError)
return c.JSON(response.StatusCode(), response)
}
//TODO Add error validation
_ = document_info
err = h.MediaClient.MinioClient.RemoveObject(ctx, request.Params.Bucket, request.Params.Document, minio.RemoveObjectOptions{})
if err != nil {
//response.Error = err.Error()
response.Message = "Error during minio#RemoveObject"
response.SetStatusCode(http.StatusInternalServerError)
return c.JSON(response.StatusCode(), response)
}
response.Message = "Document deleted"
response.SetStatusCode(http.StatusOK)
return c.JSON(response.StatusCode(), response)
}
// V1DocumentKeyPUT
func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) {
var request apirequest.V1DocumentKeyPUT
var response apiresponse.V1DocumentKeyPUT
bucket := c.Param("bucket")
document := c.Param("document")
var newKey string
err = c.Bind(&newKey)
if err != nil {
response.SetStatusCode(http.StatusBadRequest)
response.Message = err.Error()
return c.JSON(response.StatusCode(), response)
}
request, err = apirequest.NewV1DocumentKeyPUT(bucket, document, newKey)
if err != nil {
response.SetStatusCode(http.StatusBadRequest)
response.Message = err.Error()
return c.JSON(response.StatusCode(), response)
}
if !request.Complete() {
response.SetStatusCode(http.StatusBadRequest)
response.Message = "Incomplete V1DocumentKeyPUT request received"
return c.JSON(response.StatusCode(), response)
}
var allowed bool
for bucketAllowed := range h.Config.Server.Documents.Buckets {
if bucket == bucketAllowed {
allowed = true
}
}
if !allowed {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bucketExists, err := h.MediaClient.MinioClient.BucketExists(ctx, request.Params.Bucket)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Could not validate bucket exists")
}
if !bucketExists {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
// Check source object exists
if _, err := h.MediaClient.MinioClient.StatObject(ctx, request.Params.Bucket, request.Params.Document, minio.StatObjectOptions{}); err != nil {
if err.Error() == "The specified key does not exist." {
response := voki.ResponseNotFound{}
return c.JSON(response.StatusCode(), response)
}
response.Message = fmt.Sprintf("Could not obtain information on %s/%s", request.Params.Bucket, request.Params.Document)
response.SetStatusCode(http.StatusInternalServerError)
return c.JSON(response.StatusCode(), response)
}
// Copy object to newKey
if _, err := h.MediaClient.MinioClient.CopyObject(ctx,
minio.CopyDestOptions{Bucket: request.Params.Bucket, Object: request.Data.NewKey},
minio.CopySrcOptions{Bucket: request.Params.Bucket, Object: request.Params.Document},
); err != nil {
response.SetStatusCode(http.StatusInternalServerError)
response.Message = "Impossible de copier un document pour le renommer"
return c.JSON(response.StatusCode(), response)
}
// Verify copy was successful
if _, err := h.MediaClient.MinioClient.StatObject(ctx,
request.Params.Bucket, request.Data.NewKey, minio.GetObjectOptions{}); err != nil {
response.SetStatusCode(http.StatusInternalServerError)
response.Message = "Copie de document ne semble pas avoir fonctionnée"
return c.JSON(response.StatusCode(), response)
}
// Delete old file
if err := h.MediaClient.MinioClient.RemoveObject(ctx,
request.Params.Bucket, request.Params.Document, minio.RemoveObjectOptions{}); err != nil {
response.SetStatusCode(http.StatusInternalServerError)
response.Message = "Erreur pendant tentative de supprimer document source"
return c.JSON(response.StatusCode(), response)
}
//TODO cleanup
// Return result
response.SetStatusCode(http.StatusOK)
response.Message = "Document renommé avec succès"
response.Data.Bucket = request.Params.Bucket
response.Data.Key = request.Data.NewKey
return c.JSON(response.StatusCode(), response)
}