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"
|
|
|
|
|
2021-10-05 19:06:05 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2021-08-21 16:36:00 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-05 19:06:05 +02:00
|
|
|
|
2021-08-21 16:36:00 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2021-10-05 19:06:05 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2021-08-21 16:36:00 +02:00
|
|
|
"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)
|
2021-10-06 20:06:52 +02:00
|
|
|
|
2021-08-21 16:36:00 +02:00
|
|
|
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-09-29 20:09:34 +02:00
|
|
|
if !conf.Settings().Features.People {
|
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 {
|
|
|
|
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-29 20:09:34 +02:00
|
|
|
log.Debugf("marker: %s (find)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:06:52 +02:00
|
|
|
// Initialize form.
|
|
|
|
f, err := form.NewMarker(*marker)
|
2021-08-21 16:36:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2021-09-29 20:09:34 +02:00
|
|
|
log.Errorf("marker: %s (new form)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
2021-10-06 20:06:52 +02:00
|
|
|
} else if err := c.BindJSON(&f); err != nil {
|
2021-09-29 20:09:34 +02:00
|
|
|
log.Errorf("marker: %s (update form)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-06 15:27:17 +02:00
|
|
|
// Update marker from form values.
|
2021-10-06 20:06:52 +02:00
|
|
|
if changed, err := marker.SaveForm(f); err != nil {
|
2021-09-29 20:09:34 +02:00
|
|
|
log.Errorf("marker: %s", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
2021-09-17 18:51:24 +02:00
|
|
|
} else if changed {
|
|
|
|
if marker.FaceID != "" && marker.SubjUID != "" && marker.SubjSrc == entity.SrcManual {
|
|
|
|
if res, err := service.Faces().Optimize(); err != nil {
|
|
|
|
log.Errorf("faces: %s (optimize)", err)
|
|
|
|
} else if res.Merged > 0 {
|
2021-10-05 19:06:05 +02:00
|
|
|
log.Infof("faces: merged %s", english.Plural(res.Merged, "cluster", "clusters"))
|
2021-09-17 18:51:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
if err := query.UpdateSubjectCovers(); err != nil {
|
|
|
|
log.Errorf("faces: %s (update covers)", err)
|
2021-09-17 18:51:24 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 16:34:29 +02:00
|
|
|
if err := entity.UpdateSubjectCounts(); err != nil {
|
2021-09-17 18:51:24 +02:00
|
|
|
log.Errorf("faces: %s (update counts)", err)
|
2021-08-22 21:06:44 +02:00
|
|
|
}
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// Update photo metadata.
|
2021-11-09 18:00:24 +01:00
|
|
|
if !file.FilePrimary {
|
|
|
|
log.Infof("faces: skipped updating photo for non-primary file")
|
|
|
|
} else if p, err := query.PhotoByUID(file.PhotoUID); err != nil {
|
2021-09-02 14:23:40 +02:00
|
|
|
log.Errorf("faces: %s (find photo))", err)
|
|
|
|
} else if err := p.UpdateAndSaveTitle(); err != nil {
|
|
|
|
log.Errorf("faces: %s (update photo title)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
} else {
|
2021-09-02 14:23:40 +02:00
|
|
|
// Notify clients.
|
|
|
|
PublishPhotoEvent(EntityUpdated, file.PhotoUID, c)
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
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-09-02 14:23:40 +02:00
|
|
|
file, marker, err := findFileMarker(c)
|
2021-08-21 16:36:00 +02:00
|
|
|
|
|
|
|
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
|
2021-10-01 16:44:50 +02:00
|
|
|
} else if err := query.UpdateSubjectCovers(); err != nil {
|
|
|
|
log.Errorf("faces: %s (update covers)", err)
|
2021-10-01 16:34:29 +02:00
|
|
|
} else if err := entity.UpdateSubjectCounts(); err != nil {
|
2021-09-17 18:51:24 +02:00
|
|
|
log.Errorf("faces: %s (update counts)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 14:23:40 +02:00
|
|
|
// Update photo metadata.
|
2021-11-09 18:00:24 +01:00
|
|
|
if !file.FilePrimary {
|
|
|
|
log.Infof("faces: skipped updating photo for non-primary file")
|
|
|
|
} else if p, err := query.PhotoByUID(file.PhotoUID); err != nil {
|
2021-09-02 14:23:40 +02:00
|
|
|
log.Errorf("faces: %s (find photo))", err)
|
|
|
|
} else if err := p.UpdateAndSaveTitle(); err != nil {
|
|
|
|
log.Errorf("faces: %s (update photo title)", err)
|
|
|
|
} else {
|
|
|
|
// Notify clients.
|
|
|
|
PublishPhotoEvent(EntityUpdated, file.PhotoUID, c)
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:36:00 +02:00
|
|
|
event.SuccessMsg(i18n.MsgChangesSaved)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, marker)
|
|
|
|
})
|
|
|
|
}
|