/
faces.go
89 lines (72 loc) Β· 2.12 KB
/
faces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"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/search"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/i18n"
"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(c, acl.ResourcePeople, acl.ActionView)
// Abort if permission was not granted.
if s.Abort(c) {
return
}
f := form.SearchFaces{UID: c.Param("id"), Markers: true}
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(c, acl.ResourcePeople, acl.ActionUpdate)
// Abort if permission was not granted.
if s.Abort(c) {
return
}
var f form.Face
// Assign and validate request form values.
if err := c.BindJSON(&f); err != nil {
AbortBadRequest(c)
return
}
faceId := clean.Token(c.Param("id"))
m := entity.FindFace(faceId)
if m == nil {
Abort(c, http.StatusNotFound, i18n.ErrFaceNotFound)
return
}
// Change visibility?
if !f.FaceHidden && f.FaceHidden == m.FaceHidden {
// Do nothing.
} else if err := m.Update("FaceHidden", f.FaceHidden); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
return
}
// Change subject?
if f.SubjUID == "" {
// Do nothing.
} else if err := m.SetSubjectUID(f.SubjUID); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
return
}
event.SuccessMsg(i18n.MsgChangesSaved)
c.JSON(http.StatusOK, m)
})
}