2020-01-29 15:28:20 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2020-05-28 21:20:42 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
2020-01-29 15:28:20 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-29 15:28:20 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// POST /api/v1/photos/:uid/label
|
2020-01-29 15:28:20 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func AddPhotoLabel(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.POST("/photos/:uid/label", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
m, err := query.PhotoByUID(clean.UID(c.Param("uid")))
|
2020-01-29 15:28:20 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Label
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
labelEntity := entity.FirstOrCreateLabel(entity.NewLabel(f.LabelName, f.LabelPriority))
|
2020-01-29 15:28:20 +01:00
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
if labelEntity == nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed creating label"})
|
|
|
|
return
|
2020-01-29 15:28:20 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
if err := labelEntity.Restore(); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "could not restore label"})
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
photoLabel := entity.FirstOrCreatePhotoLabel(entity.NewPhotoLabel(m.ID, labelEntity.ID, f.Uncertainty, "manual"))
|
2020-01-29 15:28:20 +01:00
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
if photoLabel == nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed updating photo label"})
|
|
|
|
return
|
|
|
|
}
|
2020-01-29 15:28:20 +01:00
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
if photoLabel.Uncertainty > f.Uncertainty {
|
|
|
|
if err := photoLabel.Updates(map[string]interface{}{
|
|
|
|
"Uncertainty": f.Uncertainty,
|
|
|
|
"LabelSrc": entity.SrcManual,
|
|
|
|
}); err != nil {
|
2020-01-29 15:28:20 +01:00
|
|
|
log.Errorf("label: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(clean.UID(c.Param("uid")))
|
2020-01-29 15:28:20 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
if err := p.SaveLabels(); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-04-16 20:57:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, c.Param("uid"), c)
|
2020-04-19 01:13:55 +02:00
|
|
|
|
|
|
|
event.Success("label updated")
|
|
|
|
|
2020-01-29 15:28:20 +01:00
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// DELETE /api/v1/photos/:uid/label/:id
|
2020-01-29 15:28:20 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string PhotoUID as returned by the API
|
|
|
|
// id: int LabelId as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func RemovePhotoLabel(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.DELETE("/photos/:uid/label/:id", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
m, err := query.PhotoByUID(clean.UID(c.Param("uid")))
|
2020-01-29 15:28:20 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
labelId, err := strconv.Atoi(clean.Token(c.Param("id")))
|
2020-01-29 15:28:20 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
label, err := query.PhotoLabel(m.ID, uint(labelId))
|
2020-04-17 21:20:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if label.LabelSrc == classify.SrcManual || label.LabelSrc == classify.SrcKeyword {
|
2020-05-28 21:20:42 +02:00
|
|
|
logError("label", entity.Db().Delete(&label).Error)
|
2020-04-17 21:20:38 +02:00
|
|
|
} else {
|
2020-04-19 01:13:55 +02:00
|
|
|
label.Uncertainty = 100
|
2020-05-28 21:20:42 +02:00
|
|
|
logError("label", entity.Db().Save(&label).Error)
|
2020-04-17 21:20:38 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(clean.UID(c.Param("uid")))
|
2020-04-17 21:20:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:20:42 +02:00
|
|
|
logError("label", p.RemoveKeyword(label.Label.LabelName))
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
if err := p.SaveLabels(); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, clean.UID(c.Param("uid")), c)
|
2020-04-19 01:13:55 +02:00
|
|
|
|
|
|
|
event.Success("label removed")
|
|
|
|
|
2020-04-17 21:20:38 +02:00
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// PUT /api/v1/photos/:uid/label/:id
|
2020-04-17 21:20:38 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string PhotoUID as returned by the API
|
|
|
|
// id: int LabelId as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdatePhotoLabel(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.PUT("/photos/:uid/label/:id", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Code clean-up, simplify
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
m, err := query.PhotoByUID(clean.UID(c.Param("uid")))
|
2020-04-17 21:20:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
labelId, err := strconv.Atoi(clean.Token(c.Param("id")))
|
2020-04-17 21:20:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
label, err := query.PhotoLabel(m.ID, uint(labelId))
|
2020-04-17 21:20:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&label); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
if err := label.Save(); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
2020-01-29 15:28:20 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(clean.UID(c.Param("uid")))
|
2020-01-29 15:28:20 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
if err := p.SaveLabels(); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-04-16 20:57:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, clean.UID(c.Param("uid")), c)
|
2020-04-19 01:13:55 +02:00
|
|
|
|
|
|
|
event.Success("label saved")
|
|
|
|
|
2020-01-29 15:28:20 +01:00
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|