2019-06-09 04:37:02 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-05-30 01:41:47 +02:00
|
|
|
"encoding/json"
|
2019-12-16 11:04:49 +01:00
|
|
|
"fmt"
|
2019-06-09 04:37:02 +02:00
|
|
|
"net/http"
|
2020-05-30 01:41:47 +02:00
|
|
|
"path/filepath"
|
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"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
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-06-07 10:09:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-05-22 20:00:33 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
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
|
|
|
|
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-05-08 15:41:01 +02:00
|
|
|
result, err := query.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
|
|
|
|
}
|
|
|
|
|
2020-04-20 20:07:58 +02:00
|
|
|
// TODO c.Header("X-Count", strconv.Itoa(count))
|
|
|
|
c.Header("X-Limit", strconv.Itoa(f.Count))
|
|
|
|
c.Header("X-Offset", strconv.Itoa(f.Offset))
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// PUT /api/v1/labels/:uid
|
2020-02-02 02:00:47 +01:00
|
|
|
func UpdateLabel(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.PUT("/labels/:uid", func(c *gin.Context) {
|
2020-02-02 02:00:47 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
id := c.Param("uid")
|
|
|
|
m, err := query.LabelByUID(id)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrLabelNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetName(f.LabelName)
|
2020-05-08 15:41:01 +02:00
|
|
|
entity.Db().Save(&m)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
|
|
|
event.Success("label saved")
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishLabelEvent(EntityUpdated, id, c)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// POST /api/v1/labels/:uid/like
|
2019-06-09 04:37:02 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string Label UID
|
2019-06-09 04:37:02 +02:00
|
|
|
func LikeLabel(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.POST("/labels/:uid/like", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
id := c.Param("uid")
|
|
|
|
label, err := query.LabelByUID(id)
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-30 14:52:47 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
if err := label.Update("LabelFavorite", true); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
2019-06-09 04:37:02 +02:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
if label.LabelPriority < 0 {
|
|
|
|
event.Publish("count.labels", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishLabelEvent(EntityUpdated, id, c)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2019-06-09 04:37:02 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// DELETE /api/v1/labels/:uid/like
|
2019-06-09 04:37:02 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string Label UID
|
2019-06-09 04:37:02 +02:00
|
|
|
func DislikeLabel(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.DELETE("/labels/:uid/like", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
id := c.Param("uid")
|
|
|
|
label, err := query.LabelByUID(id)
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-30 14:52:47 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
if err := label.Update("LabelFavorite", false); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
2019-06-09 04:37:02 +02:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
if label.LabelPriority < 0 {
|
|
|
|
event.Publish("count.labels", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishLabelEvent(EntityUpdated, id, c)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2019-06-09 04:37:02 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-05-27 19:38:40 +02:00
|
|
|
// GET /api/v1/labels/:uid/t/:token/:type
|
2019-12-16 11:04:49 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string Label UID
|
2019-12-16 11:04:49 +01:00
|
|
|
// type: string Thumbnail type, see photoprism.ThumbnailTypes
|
|
|
|
func LabelThumbnail(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-27 19:38:40 +02:00
|
|
|
router.GET("/labels/:uid/t/:token/:type", func(c *gin.Context) {
|
|
|
|
if InvalidToken(c, conf) {
|
2020-05-30 01:41:47 +02:00
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", labelIconSvg)
|
2020-05-27 19:38:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-28 16:26:22 +02:00
|
|
|
start := time.Now()
|
2019-12-16 11:04:49 +01:00
|
|
|
typeName := c.Param("type")
|
2020-05-30 01:41:47 +02:00
|
|
|
uid := c.Param("uid")
|
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-05-30 01:41:47 +02:00
|
|
|
log.Errorf("label-thumbnail: invalid type %s", txt.Quote(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-05-30 01:41:47 +02:00
|
|
|
cache := service.Cache()
|
|
|
|
cacheKey := fmt.Sprintf("label-thumbnail:%s:%s", uid, typeName)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
if cacheData, err := cache.Get(cacheKey); err == nil {
|
2020-05-23 20:58:58 +02:00
|
|
|
log.Debugf("cache hit for %s [%s]", cacheKey, time.Since(start))
|
2020-05-30 01:41:47 +02:00
|
|
|
|
|
|
|
var cached ThumbCache
|
|
|
|
|
|
|
|
if err := json.Unmarshal(cacheData, &cached); err != nil {
|
|
|
|
log.Errorf("label-thumbnail: %s not found", uid)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fs.FileExists(cached.FileName) {
|
|
|
|
log.Errorf("label-thumbnail: %s not found", uid)
|
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.FileAttachment(cached.FileName, cached.ShareName)
|
|
|
|
} else {
|
|
|
|
c.File(cached.FileName)
|
|
|
|
}
|
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
return
|
|
|
|
}
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
f, err := query.LabelThumbByUID(uid)
|
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-06-07 10:09:35 +02:00
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2020-05-30 01:41:47 +02:00
|
|
|
log.Errorf("label-thumbnail: file %s is missing", txt.Quote(f.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
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2020-05-30 01:41:47 +02:00
|
|
|
logError("label-thumbnail", f.Update("FileMissing", true))
|
2020-05-25 19:10:44 +02:00
|
|
|
|
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-05-30 01:41:47 +02:00
|
|
|
log.Debugf("label-thumbnail: using original, size exceeds limit (width %d, height %d)", thumbType.Width, thumbType.Height)
|
2020-01-08 19:51:21 +01:00
|
|
|
|
|
|
|
c.File(fileName)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
var thumbnail string
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-05-05 18:26:44 +02:00
|
|
|
if conf.ThumbUncached() || thumbType.OnDemand() {
|
2020-05-05 17:04:13 +02:00
|
|
|
thumbnail, err = thumb.FromFile(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
|
2020-05-05 15:42:54 +02:00
|
|
|
} else {
|
2020-05-05 17:04:13 +02:00
|
|
|
thumbnail, err = thumb.FromCache(fileName, f.FileHash, conf.ThumbPath(), thumbType.Width, thumbType.Height, thumbType.Options...)
|
2020-05-05 15:42:54 +02:00
|
|
|
}
|
2019-12-16 11:04:49 +01:00
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
if err != nil {
|
2020-05-30 01:41:47 +02:00
|
|
|
log.Errorf("label-thumbnail: %s", err)
|
2020-05-05 15:42:54 +02:00
|
|
|
c.Data(http.StatusOK, "image/svg+xml", labelIconSvg)
|
|
|
|
return
|
2020-05-30 01:41:47 +02:00
|
|
|
} else if thumbnail == "" {
|
|
|
|
log.Errorf("label-thumbnail: %s has empty thumb name - bug?", filepath.Base(fileName))
|
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
|
|
|
}
|
2020-05-05 15:42:54 +02:00
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
if cached, err := json.Marshal(ThumbCache{thumbnail, f.ShareFileName()}); err == nil {
|
|
|
|
logError("label-thumbnail", cache.Set(cacheKey, cached))
|
|
|
|
log.Debugf("cached %s [%s]", cacheKey, time.Since(start))
|
|
|
|
}
|
2020-05-05 15:42:54 +02:00
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
if c.Query("download") != "" {
|
|
|
|
c.FileAttachment(thumbnail, f.ShareFileName())
|
|
|
|
} else {
|
|
|
|
c.File(thumbnail)
|
|
|
|
}
|
2019-12-16 11:04:49 +01:00
|
|
|
})
|
|
|
|
}
|