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"
|
2022-04-03 14:51:58 +02:00
|
|
|
"time"
|
2021-08-21 16:36:00 +02:00
|
|
|
|
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"
|
2021-12-09 02:33:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2021-08-21 16:36:00 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
)
|
|
|
|
|
2022-04-03 14:51:58 +02:00
|
|
|
// Checks if background worker runs less than once per hour.
|
|
|
|
func wakeupIntervalTooHigh(c *gin.Context) bool {
|
|
|
|
if conf := service.Config(); conf.Unsafe() {
|
|
|
|
return false
|
|
|
|
} else if i := conf.WakeupInterval(); i > time.Hour {
|
|
|
|
Abort(c, http.StatusForbidden, i18n.ErrWakeupInterval, i.String())
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:36:00 +02:00
|
|
|
// 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)
|
2022-04-05 13:54:50 +02:00
|
|
|
return nil, nil, fmt.Errorf("uid %s %s", uid, err)
|
2021-09-01 20:46:15 +02:00
|
|
|
} 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.
|
2022-04-05 13:54:50 +02:00
|
|
|
if file, err = query.FileByUID(marker.FileUID); err != nil {
|
2021-08-21 16:36:00 +02:00
|
|
|
AbortEntityNotFound(c)
|
2022-04-05 13:54:50 +02:00
|
|
|
return file, marker, fmt.Errorf("file %s %s", marker.FileUID, err)
|
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) {
|
2022-04-03 14:51:58 +02:00
|
|
|
// Abort if workers runs less than once per hour.
|
|
|
|
if wakeupIntervalTooHigh(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort if another update is running.
|
2021-12-09 02:33:41 +01:00
|
|
|
if err := mutex.People.Start(); err != nil {
|
|
|
|
AbortBusy(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mutex.People.Stop()
|
|
|
|
|
2021-08-21 16:36:00 +02:00
|
|
|
file, marker, err := findFileMarker(c)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-05 13:54:50 +02:00
|
|
|
log.Debugf("faces: %s (find marker to update)", 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 {
|
2022-04-05 13:54:50 +02:00
|
|
|
log.Errorf("faces: %s (create marker update 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 {
|
2022-04-05 13:54:50 +02:00
|
|
|
log.Errorf("faces: %s (set updated marker values)", 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 {
|
2022-04-05 13:54:50 +02:00
|
|
|
log.Errorf("faces: %s (update marker)", 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) {
|
2022-04-03 14:51:58 +02:00
|
|
|
// Abort if workers runs less than once per hour.
|
|
|
|
if wakeupIntervalTooHigh(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort if another update is running.
|
2021-12-09 02:33:41 +01:00
|
|
|
if err := mutex.People.Start(); err != nil {
|
|
|
|
AbortBusy(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mutex.People.Stop()
|
|
|
|
|
2021-09-02 14:23:40 +02:00
|
|
|
file, marker, err := findFileMarker(c)
|
2021-08-21 16:36:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-05 13:54:50 +02:00
|
|
|
log.Debugf("faces: %s (find marker to clear subject)", err)
|
2021-08-21 16:36:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := marker.ClearSubject(entity.SrcManual); err != nil {
|
2022-04-05 13:54:50 +02:00
|
|
|
log.Errorf("faces: %s (clear marker 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)
|
|
|
|
})
|
|
|
|
}
|