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
|
|
|
|
2020-02-02 03:36:00 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-12-11 19:11:44 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-02-02 03:36:00 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-11 19:11:44 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-02-02 03:36:00 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
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"
|
2019-12-11 19:11:44 +01:00
|
|
|
)
|
|
|
|
|
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-05-08 15:41:01 +02:00
|
|
|
p, err := query.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
|
|
|
|
}
|
|
|
|
|
2020-04-26 16:22:29 +02:00
|
|
|
c.IndentedJSON(http.StatusOK, p)
|
2019-12-11 19:11:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-16 20:57:00 +02:00
|
|
|
uuid := c.Param("uuid")
|
2020-05-08 15:41:01 +02:00
|
|
|
m, err := query.PhotoByUUID(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-02-02 03:36:00 +01:00
|
|
|
// TODO: Proof-of-concept for form handling - might need refactoring
|
|
|
|
// 1) Init form with model values
|
|
|
|
f, err := form.NewPhoto(m)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
2020-02-02 03:36:00 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) Update form with values from request
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, ErrFormInvalid)
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-02 03:36:00 +01:00
|
|
|
// 3) Save model with values from form
|
2020-04-30 20:07:03 +02:00
|
|
|
if err := entity.SavePhotoForm(m, f, conf.GeoCodingApi()); err != nil {
|
2020-04-20 10:38:01 +02:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
2020-02-02 03:36:00 +01:00
|
|
|
return
|
|
|
|
}
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
if err := query.UpdatePhotoCounts(); err != nil {
|
|
|
|
log.Errorf("photo: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
PublishPhotoEvent(EntityUpdated, uuid, c)
|
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-05-08 15:41:01 +02:00
|
|
|
p, err := query.PreloadPhotoByUUID(uuid)
|
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-05-08 15:41:01 +02:00
|
|
|
f, err := query.FileByPhotoUUID(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-04-01 12:00:45 +02:00
|
|
|
downloadFileName := f.ShareFileName()
|
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-05-08 15:41:01 +02:00
|
|
|
m, err := query.PhotoByUUID(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
|
2020-04-24 13:21:18 +02:00
|
|
|
m.PhotoQuality = m.QualityScore()
|
2019-12-11 19:11:44 +01:00
|
|
|
conf.Db().Save(&m)
|
|
|
|
|
|
|
|
event.Publish("count.favorites", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, id, c)
|
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-05-08 15:41:01 +02:00
|
|
|
m, err := query.PhotoByUUID(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
|
2020-04-24 13:21:18 +02:00
|
|
|
m.PhotoQuality = m.QualityScore()
|
2020-05-08 15:41:01 +02:00
|
|
|
entity.Db().Save(&m)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
event.Publish("count.favorites", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, id, c)
|
2020-01-30 09:51:23 +01:00
|
|
|
|
2019-12-11 19:11:44 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
|
|
})
|
|
|
|
}
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
// POST /api/v1/photos/:uuid/primary/:file_uuid
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uuid: string PhotoUUID as returned by the API
|
|
|
|
func SetPhotoPrimary(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/photos/:uuid/primary/:file_uuid", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid := c.Param("uuid")
|
|
|
|
fileUUID := c.Param("file_uuid")
|
2020-05-08 15:41:01 +02:00
|
|
|
err := query.SetPhotoPrimary(uuid, fileUUID)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, uuid, c)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
event.Success("photo saved")
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
p, err := query.PreloadPhotoByUUID(uuid)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|