2020-01-29 15:28:20 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
|
|
|
|
2020-01-29 15:28:20 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
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"
|
|
|
|
"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:
|
2020-05-23 20:58:58 +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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
m, err := query.PhotoByUID(sanitize.IdString(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
p, err := query.PhotoPreloadByUID(sanitize.IdString(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 {
|
2020-04-16 20:57:00 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
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:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-01-29 15:28:20 +01:00
|
|
|
// 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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-01-29 15:28:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
m, err := query.PhotoByUID(sanitize.IdString(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
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
labelId, err := strconv.Atoi(sanitize.Token(c.Param("id")))
|
2020-01-29 15:28:20 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
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 {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
p, err := query.PhotoPreloadByUID(sanitize.IdString(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 {
|
2020-04-17 21:20:38 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
PublishPhotoEvent(EntityUpdated, sanitize.IdString(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:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-04-17 21:20:38 +02:00
|
|
|
// 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) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Code clean-up, simplify
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
m, err := query.PhotoByUID(sanitize.IdString(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
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
labelId, err := strconv.Atoi(sanitize.Token(c.Param("id")))
|
2020-04-17 21:20:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
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 {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
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 {
|
2020-04-17 21:20:38 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
2020-01-29 15:28:20 +01:00
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
p, err := query.PhotoPreloadByUID(sanitize.IdString(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 {
|
2020-04-16 20:57:00 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:34:52 +01:00
|
|
|
PublishPhotoEvent(EntityUpdated, sanitize.IdString(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)
|
|
|
|
})
|
|
|
|
}
|