2019-12-11 19:11:44 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-12-13 16:25:47 +01:00
|
|
|
"path"
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-01-05 14:18:40 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
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-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2020-01-10 10:43:51 +01:00
|
|
|
// GET /api/v1/photos/:uuid
|
2019-12-11 19:11:44 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uuid: string PhotoUUID as returned by the API
|
|
|
|
func GetPhoto(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/photos/:uuid", func(c *gin.Context) {
|
|
|
|
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())
|
|
|
|
p, err := q.PreloadPhotoByUUID(c.Param("uuid"))
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:16:18 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/v1/photos/:uuid
|
|
|
|
func UpdatePhoto(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.PUT("/photos/:uuid", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
id := c.Param("uuid")
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
m, err := q.FindPhotoByUUID(id)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&m); err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Db().Save(&m)
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
PublishPhotoEvent(EntityUpdated, id, c, q)
|
2020-01-30 09:51:23 +01:00
|
|
|
|
2019-12-27 07:02:49 +01:00
|
|
|
event.Success("photo saved")
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
p, err := q.PreloadPhotoByUUID(id)
|
2020-01-30 06:17:02 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
2019-12-11 19:11:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/photos/:uuid/download
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uuid: string PhotoUUID as returned by the API
|
|
|
|
func GetPhotoDownload(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/photos/:uuid/download", func(c *gin.Context) {
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2020-01-06 14:32:15 +01:00
|
|
|
f, err := q.FindFileByPhotoUUID(c.Param("uuid"))
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), f.FileName)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2019-12-11 19:11:44 +01:00
|
|
|
log.Errorf("could not find original: %s", c.Param("uuid"))
|
2020-01-23 10:10:32 +01:00
|
|
|
c.Data(http.StatusNotFound, "image/svg+xml", photoIconSvg)
|
2019-12-11 19:11:44 +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-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
downloadFileName := f.DownloadFileName()
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadFileName))
|
|
|
|
|
|
|
|
c.File(fileName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/v1/photos/:uuid/like
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uuid: string PhotoUUID as returned by the API
|
|
|
|
func LikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/photos/:uuid/like", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
id := c.Param("uuid")
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2020-01-30 18:19:26 +01:00
|
|
|
m, err := q.FindPhotoByUUID(id)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.PhotoFavorite = true
|
|
|
|
conf.Db().Save(&m)
|
|
|
|
|
|
|
|
event.Publish("count.favorites", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
PublishPhotoEvent(EntityUpdated, id, c, q)
|
2020-01-30 09:51:23 +01:00
|
|
|
|
2019-12-11 19:11:44 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/v1/photos/:uuid/like
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uuid: string PhotoUUID as returned by the API
|
|
|
|
func DislikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.DELETE("/photos/:uuid/like", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
id := c.Param("uuid")
|
2020-01-05 14:18:40 +01:00
|
|
|
q := query.New(conf.OriginalsPath(), conf.Db())
|
2020-01-30 18:19:26 +01:00
|
|
|
m, err := q.FindPhotoByUUID(id)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 10:10:32 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.PhotoFavorite = false
|
|
|
|
conf.Db().Save(&m)
|
|
|
|
|
|
|
|
event.Publish("count.favorites", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
PublishPhotoEvent(EntityUpdated, id, c, q)
|
2020-01-30 09:51:23 +01:00
|
|
|
|
2019-12-11 19:11:44 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
|
|
})
|
|
|
|
}
|