feat(api): Implémenter V1DocumentKeyPUT

N'inclue pas de méthodes associées au API client
This commit is contained in:
Victor Lacasse-Beaudoin 2024-02-01 17:38:48 -05:00
parent 33c23d89e4
commit ef75eb29fa
2 changed files with 39 additions and 10 deletions

View file

@ -285,7 +285,15 @@ func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) {
bucket := c.Param("bucket")
document := c.Param("document")
newKey := c.Bind(
var newKey string
err = c.Bind(&newKey)
if err != nil {
response.StatusCode = http.StatusBadRequest
response.Message = err.Error()
return c.JSON(response.StatusCode, response)
}
request, err = apirequest.NewV1DocumentKeyPUT(bucket, document, newKey)
if err != nil {
@ -316,7 +324,7 @@ func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bucketExists, err := h.MediaClient.MinioClient.BucketExists(ctx, bucket)
bucketExists, err := h.MediaClient.MinioClient.BucketExists(ctx, request.Params.Bucket)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Could not validate bucket exists")
}
@ -325,33 +333,54 @@ func (h *V1Handler) V1DocumentKeyPUT(c echo.Context) (err error) {
return c.JSON(apiresponse.NotFoundResponse())
}
if _, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}); err != nil {
// 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." {
return c.JSON(apiresponse.NotFoundResponse())
}
response.Message = fmt.Sprintf("Could not obtain information on %s/%s", bucket, document)
response.Message = fmt.Sprintf("Could not obtain information on %s/%s", request.Params.Bucket, request.Params.Document)
response.StatusCode = http.StatusInternalServerError
return c.JSON(response.StatusCode, response)
}
// Copy object to newKey
if _, err := h.MediaClient.MinioClient.CopyObject(ctx,
minio.CopyDestOptions{Bucket: bucket, Object: newKey},
minio.CopySrcOptions{Bucket: bucket, Object: document},
minio.CopyDestOptions{Bucket: request.Params.Bucket, Object: request.Data.NewKey},
minio.CopySrcOptions{Bucket: request.Params.Bucket, Object: request.Params.Document},
); err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Impossible de copier un document pour le renommer"
return c.JSON(response.StatusCode, response)
}
//TODO verify copy was successful
// Verify copy was successful
if _, err := h.MediaClient.MinioClient.StatObject(ctx,
request.Params.Bucket, request.Data.NewKey, minio.GetObjectOptions{}); err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Copie de document ne semble pas avoir fonctionnée"
return c.JSON(response.StatusCode, response)
}
//TODO delete old file
// Delete old file
if err := h.MediaClient.MinioClient.RemoveObject(ctx,
request.Params.Bucket, request.Params.Document, minio.RemoveObjectOptions{}); err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Erreur pendant tentative de supprimer document source"
return c.JSON(response.StatusCode, response)
}
//TODO cleanup
//TODO return result
// Return result
response.StatusCode = 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)
}

View file

@ -226,7 +226,7 @@ func RunServer() {
if err := pave.EchoRegister[
apirequest.V1DocumentKeyPUT,
apiresponse.V1DocumentKeyPUT](groupV1, &p, "/v1", http.MethodPut, "/:bucket/:document", "Renommer un document", "V1DocumentKeyPUT", v1Handler.V1DocumentKeyPUT); err != nil {
apiresponse.V1DocumentKeyPUT](groupV1, &p, "/v1", http.MethodPut, "/bucket/:bucket/:document/key", "Renommer un document", "V1DocumentKeyPUT", v1Handler.V1DocumentKeyPUT); err != nil {
log.Fatal(err)
}