WIP: Ajouter fonction de renom de document
This commit is contained in:
parent
9fae3b0471
commit
33c23d89e4
4 changed files with 147 additions and 0 deletions
|
@ -277,3 +277,81 @@ func (h *V1Handler) V1DocumentDELETE(c echo.Context) error {
|
|||
|
||||
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")
|
||||
newKey := c.Bind(
|
||||
|
||||
request, err = apirequest.NewV1DocumentKeyPUT(bucket, document, newKey)
|
||||
if err != nil {
|
||||
response.StatusCode = http.StatusBadRequest
|
||||
response.Message = err.Error()
|
||||
|
||||
return c.JSON(response.StatusCode, response)
|
||||
}
|
||||
|
||||
if !request.Complete() {
|
||||
response.StatusCode = 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 {
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
bucketExists, err := h.MediaClient.MinioClient.BucketExists(ctx, bucket)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, "Could not validate bucket exists")
|
||||
}
|
||||
|
||||
if !bucketExists {
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
if _, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, 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.StatusCode = http.StatusInternalServerError
|
||||
|
||||
return c.JSON(response.StatusCode, response)
|
||||
}
|
||||
|
||||
if _, err := h.MediaClient.MinioClient.CopyObject(ctx,
|
||||
minio.CopyDestOptions{Bucket: bucket, Object: newKey},
|
||||
minio.CopySrcOptions{Bucket: bucket, Object: 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
|
||||
|
||||
//TODO delete old file
|
||||
|
||||
//TODO cleanup
|
||||
|
||||
//TODO return result
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue