2021-08-21 16:36:00 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-09-01 20:46:15 +02:00
|
|
|
"fmt"
|
2021-08-21 16:36:00 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
// findFileMarker returns a file and marker entity matching the api request.
|
2021-09-01 20:46:15 +02:00
|
|
|
func findFileMarker(c *gin.Context) (file *entity.File, marker *entity.Marker, err error) {
|
|
|
|
// Check authorization.
|
2021-08-21 16:36:00 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceFiles, acl.ActionUpdate)
|
|
|
|
if s.Invalid() {
|
|
|
|
AbortUnauthorized(c)
|
2021-09-01 20:46:15 +02:00
|
|
|
return nil, nil, fmt.Errorf("unauthorized")
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// Check feature flags.
|
2021-08-21 16:36:00 +02:00
|
|
|
conf := service.Config()
|
2021-08-31 13:34:29 +02:00
|
|
|
if !conf.Settings().Features.People || !conf.Settings().Features.Edit {
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortFeatureDisabled(c)
|
2021-09-01 20:46:15 +02:00
|
|
|
return nil, nil, fmt.Errorf("feature disabled")
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// Find marker.
|
|
|
|
if uid := c.Param("marker_uid"); uid == "" {
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortBadRequest(c)
|
2021-09-01 20:46:15 +02:00
|
|
|
return nil, nil, fmt.Errorf("bad request")
|
|
|
|
} else if marker, err = query.MarkerByUID(uid); err != nil {
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortEntityNotFound(c)
|
2021-09-01 20:46:15 +02:00
|
|
|
return nil, nil, err
|
|
|
|
} else if marker.FileUID == "" {
|
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return nil, marker, fmt.Errorf("marker file missing")
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// Find file.
|
|
|
|
if f, err := query.FileByUID(marker.FileUID); err != nil {
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortEntityNotFound(c)
|
2021-09-01 20:46:15 +02:00
|
|
|
return nil, marker, err
|
|
|
|
} else if !f.FilePrimary {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return nil, marker, fmt.Errorf("can't update markers for non-primary files")
|
|
|
|
} else {
|
|
|
|
file = &f
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return file, marker, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMarker updates an existing file marker e.g. representing a face.
|
|
|
|
//
|
2021-09-01 20:46:15 +02:00
|
|
|
// PUT /api/v1/markers/:marker_uid
|
2021-08-21 16:36:00 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uid: string Photo UID as returned by the API
|
|
|
|
// file_uid: string File UID as returned by the API
|
|
|
|
// id: int Marker ID as returned by the API
|
|
|
|
func UpdateMarker(router *gin.RouterGroup) {
|
2021-09-01 20:46:15 +02:00
|
|
|
router.PUT("/markers/:marker_uid", func(c *gin.Context) {
|
2021-08-21 16:36:00 +02:00
|
|
|
file, marker, err := findFileMarker(c)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-01 20:46:15 +02:00
|
|
|
log.Debugf("api: %s (update marker)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
markerForm, err := form.NewMarker(*marker)
|
2021-08-21 16:36:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("photo: %s (new marker form)", err)
|
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&markerForm); err != nil {
|
|
|
|
log.Errorf("photo: %s (update marker form)", err)
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// Save marker.
|
2021-08-21 16:36:00 +02:00
|
|
|
if err := marker.SaveForm(markerForm); err != nil {
|
|
|
|
log.Errorf("photo: %s (save marker form)", err)
|
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
2021-08-22 21:06:44 +02:00
|
|
|
} else if marker.SubjectUID != "" && marker.SubjectSrc == entity.SrcManual && marker.FaceID != "" {
|
|
|
|
if res, err := service.Faces().Optimize(); err != nil {
|
|
|
|
log.Errorf("faces: %s (optimize)", err)
|
|
|
|
} else if res.Merged > 0 {
|
|
|
|
log.Infof("faces: %d clusters merged", res.Merged)
|
|
|
|
}
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// Update photo metadata.
|
|
|
|
if p, err := query.PhotoByUID(file.PhotoUID); err != nil {
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if faceCount := file.FaceCount(); p.PhotoFaces == faceCount {
|
|
|
|
// Do nothing.
|
|
|
|
} else if err := p.Update("PhotoFaces", faceCount); err != nil {
|
|
|
|
log.Errorf("photo: %s (update face count)", err)
|
|
|
|
} else {
|
2021-09-01 20:46:15 +02:00
|
|
|
// Notify clients.
|
2021-08-21 16:36:00 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, file.PhotoUID, c)
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 20:46:15 +02:00
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgChangesSaved)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, marker)
|
2021-08-21 16:36:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearMarkerSubject removes an existing marker subject association.
|
|
|
|
//
|
2021-09-01 20:46:15 +02:00
|
|
|
// DELETE /api/v1/markers/:marker_uid/subject
|
2021-08-21 16:36:00 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uid: string Photo UID as returned by the API
|
|
|
|
// file_uid: string File UID as returned by the API
|
|
|
|
// id: int Marker ID as returned by the API
|
|
|
|
func ClearMarkerSubject(router *gin.RouterGroup) {
|
2021-09-01 20:46:15 +02:00
|
|
|
router.DELETE("/markers/:marker_uid/subject", func(c *gin.Context) {
|
2021-08-21 16:36:00 +02:00
|
|
|
_, marker, err := findFileMarker(c)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-01 20:46:15 +02:00
|
|
|
log.Debugf("api: %s (clear marker subject)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := marker.ClearSubject(entity.SrcManual); err != nil {
|
2021-08-22 16:14:34 +02:00
|
|
|
log.Errorf("faces: %s (clear subject)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgChangesSaved)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, marker)
|
|
|
|
})
|
|
|
|
}
|