2021-09-18 15:32:39 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-11-26 14:28:50 +01:00
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-09-18 15:32:39 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetFace returns a face as JSON.
|
|
|
|
//
|
|
|
|
// GET /api/v1/faces/:id
|
|
|
|
func GetFace(router *gin.RouterGroup) {
|
|
|
|
router.GET("/faces/:id", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePeople, acl.ActionView)
|
2021-09-18 15:32:39 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Abort if permission was not granted.
|
|
|
|
if s.Abort(c) {
|
2021-09-18 15:32:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:42:57 +01:00
|
|
|
f := form.SearchFaces{UID: c.Param("id"), Markers: true}
|
2021-09-18 15:32:39 +02:00
|
|
|
|
|
|
|
if results, err := search.Faces(f); err != nil || len(results) < 1 {
|
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrFaceNotFound)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, results[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateFace updates face properties.
|
|
|
|
//
|
|
|
|
// PUT /api/v1/faces/:id
|
|
|
|
func UpdateFace(router *gin.RouterGroup) {
|
|
|
|
router.PUT("/faces/:id", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePeople, acl.ActionUpdate)
|
2021-09-18 15:32:39 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Abort if permission was not granted.
|
|
|
|
if s.Abort(c) {
|
2021-09-18 15:32:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Face
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
faceId := clean.Token(c.Param("id"))
|
2021-09-18 15:32:39 +02:00
|
|
|
m := entity.FindFace(faceId)
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrFaceNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-18 20:41:30 +02:00
|
|
|
// Change visibility?
|
|
|
|
if !f.FaceHidden && f.FaceHidden == m.FaceHidden {
|
|
|
|
// Do nothing.
|
|
|
|
} else if err := m.Update("FaceHidden", f.FaceHidden); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2021-09-18 15:32:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-18 20:41:30 +02:00
|
|
|
// Change subject?
|
|
|
|
if f.SubjUID == "" {
|
|
|
|
// Do nothing.
|
|
|
|
} else if err := m.SetSubjectUID(f.SubjUID); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2021-09-18 20:41:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgChangesSaved)
|
2021-09-18 15:32:39 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|