2021-09-18 15:32:39 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
"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"
|
|
|
|
"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) {
|
|
|
|
s := Auth(SessionID(c), acl.ResourceSubjects, acl.ActionRead)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
f := form.SearchFaces{ID: 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) {
|
|
|
|
s := Auth(SessionID(c), acl.ResourceSubjects, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Face
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
faceId := sanitize.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 {
|
2021-09-18 15:32:39 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
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 {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgChangesSaved)
|
2021-09-18 15:32:39 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|