2019-06-09 04:37:02 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-12-16 11:04:49 +01:00
|
|
|
"fmt"
|
2019-12-16 23:33:52 +01:00
|
|
|
"io/ioutil"
|
2019-06-09 04:37:02 +02:00
|
|
|
"net/http"
|
2019-12-16 11:04:49 +01:00
|
|
|
"path"
|
2019-06-09 04:37:02 +02:00
|
|
|
"strconv"
|
2019-12-16 23:33:52 +01:00
|
|
|
"time"
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-12-16 23:33:52 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-12-05 19:21:35 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-01-05 14:18:40 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-09 04:37:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/labels
|
|
|
|
func GetLabels(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/labels", func(c *gin.Context) {
|
2020-01-22 13:43:07 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
var f form.LabelSearch
|
2019-06-09 04:37:02 +02:00
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2019-12-05 19:21:35 +01:00
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
result, err := q.Labels(f)
|
2019-06-09 04:37:02 +02:00
|
|
|
if err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
c.Header("X-Result-Count", strconv.Itoa(f.Count))
|
|
|
|
c.Header("X-Result-Offset", strconv.Itoa(f.Offset))
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-02 02:00:47 +01:00
|
|
|
// PUT /api/v1/labels/:uuid
|
|
|
|
func UpdateLabel(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.PUT("/labels/:uuid", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Label
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := c.Param("uuid")
|
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
|
|
|
|
|
|
|
m, err := q.FindLabelByUUID(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrLabelNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Rename(f.LabelName)
|
|
|
|
conf.Db().Save(&m)
|
|
|
|
|
|
|
|
event.Success("label saved")
|
|
|
|
|
|
|
|
PublishLabelEvent(EntityUpdated, id, c, q)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
// POST /api/v1/labels/:uuid/like
|
2019-06-09 04:37:02 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2019-12-16 23:33:52 +01:00
|
|
|
// uuid: string Label UUID
|
2019-06-09 04:37:02 +02:00
|
|
|
func LikeLabel(router *gin.RouterGroup, conf *config.Config) {
|
2019-12-16 23:33:52 +01:00
|
|
|
router.POST("/labels/:uuid/like", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2019-06-09 04:37:02 +02:00
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
label, err := q.FindLabelByUUID(c.Param("uuid"))
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
label.LabelFavorite = true
|
|
|
|
conf.Db().Save(&label)
|
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
if label.LabelPriority < 0 {
|
|
|
|
event.Publish("count.labels", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-09 04:37:02 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
// DELETE /api/v1/labels/:uuid/like
|
2019-06-09 04:37:02 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2019-12-16 23:33:52 +01:00
|
|
|
// uuid: string Label UUID
|
2019-06-09 04:37:02 +02:00
|
|
|
func DislikeLabel(router *gin.RouterGroup, conf *config.Config) {
|
2019-12-16 23:33:52 +01:00
|
|
|
router.DELETE("/labels/:uuid/like", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2019-06-09 04:37:02 +02:00
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
label, err := q.FindLabelByUUID(c.Param("uuid"))
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(404, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
label.LabelFavorite = false
|
|
|
|
conf.Db().Save(&label)
|
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
if label.LabelPriority < 0 {
|
|
|
|
event.Publish("count.labels", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-09 04:37:02 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
// GET /api/v1/labels/:uuid/thumbnail/:type
|
2019-12-16 11:04:49 +01:00
|
|
|
//
|
|
|
|
// Example: /api/v1/labels/cheetah/thumbnail/tile_500
|
|
|
|
//
|
|
|
|
// Parameters:
|
2019-12-16 23:33:52 +01:00
|
|
|
// uuid: string Label UUID
|
2019-12-16 11:04:49 +01:00
|
|
|
// type: string Thumbnail type, see photoprism.ThumbnailTypes
|
|
|
|
func LabelThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
2019-12-16 23:33:52 +01:00
|
|
|
router.GET("/labels/:uuid/thumbnail/:type", func(c *gin.Context) {
|
2019-12-16 11:04:49 +01:00
|
|
|
typeName := c.Param("type")
|
2019-12-16 23:33:52 +01:00
|
|
|
labelUUID := c.Param("uuid")
|
|
|
|
start := time.Now()
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
thumbType, ok := thumb.Types[typeName]
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
if !ok {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Errorf("label: invalid thumb type \"%s\"", typeName)
|
2019-12-17 04:39:23 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
2019-12-16 11:04:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
gc := conf.Cache()
|
|
|
|
cacheKey := fmt.Sprintf("label-thumbnail:%s:%s", labelUUID, typeName)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
if cacheData, ok := gc.Get(cacheKey); ok {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Debugf("label: %s cache hit [%s]", cacheKey, time.Since(start))
|
2019-12-16 23:33:52 +01:00
|
|
|
c.Data(http.StatusOK, "image/jpeg", cacheData.([]byte))
|
|
|
|
return
|
|
|
|
}
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
f, err := q.FindLabelThumbByUUID(labelUUID)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2019-12-17 04:39:23 +01:00
|
|
|
log.Errorf(err.Error())
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
2019-12-16 11:04:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), f.FileName)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Errorf("label: could not find original for %s", fileName)
|
2019-12-17 04:39:23 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore
|
2020-01-06 14:32:15 +01:00
|
|
|
f.FileMissing = true
|
|
|
|
conf.Db().Save(&f)
|
2019-12-16 11:04:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-08 19:51:21 +01:00
|
|
|
// Use original file if thumb size exceeds limit, see https://github.com/photoprism/photoprism/issues/157
|
2020-01-13 11:07:09 +01:00
|
|
|
if thumbType.ExceedsLimit() {
|
2020-01-08 19:51:21 +01:00
|
|
|
log.Debugf("label: using original, thumbnail size exceeds limit (width %d, height %d)", thumbType.Width, thumbType.Height)
|
|
|
|
|
|
|
|
c.File(fileName)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
if thumbnail, err := thumb.FromFile(fileName, f.FileHash, conf.ThumbnailsPath(), thumbType.Width, thumbType.Height, thumbType.Options...); err == nil {
|
2019-12-16 23:33:52 +01:00
|
|
|
thumbData, err := ioutil.ReadFile(thumbnail)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
if err != nil {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Errorf("label: %s", err)
|
2019-12-17 04:39:23 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
2019-12-16 23:33:52 +01:00
|
|
|
return
|
2019-12-16 11:04:49 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 17:24:29 +01:00
|
|
|
gc.Set(cacheKey, thumbData, time.Hour*4)
|
2019-12-16 23:33:52 +01:00
|
|
|
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Debugf("label: %s cached [%s]", cacheKey, time.Since(start))
|
2019-12-16 23:33:52 +01:00
|
|
|
|
|
|
|
c.Data(http.StatusOK, "image/jpeg", thumbData)
|
2019-12-16 11:04:49 +01:00
|
|
|
} else {
|
2020-02-02 00:31:09 +01:00
|
|
|
log.Errorf("label: %s", err)
|
2019-12-16 23:33:52 +01:00
|
|
|
|
2019-12-17 04:39:23 +01:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
2019-12-16 23:33:52 +01:00
|
|
|
return
|
2019-12-16 11:04:49 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|