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-03-28 15:29:17 +01:00
|
|
|
q := query.New(conf.Db())
|
2020-01-05 14:18:40 +01:00
|
|
|
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-04-20 10:38:01 +02:00
|
|
|
db := conf.Db()
|
2020-04-16 20:57:00 +02:00
|
|
|
uuid := c.Param("uuid")
|
2020-04-20 10:38:01 +02:00
|
|
|
q := query.New(db)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-04-16 20:57:00 +02:00
|
|
|
m, err := q.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-20 10:38:01 +02:00
|
|
|
if err := entity.SavePhotoForm(m, f, db, conf.GeoCodingApi()); err != nil {
|
|
|
|
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-04-16 20:57:00 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, uuid, 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-04-16 20:57:00 +02:00
|
|
|
p, err := q.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-03-28 15:29:17 +01:00
|
|
|
q := query.New(conf.Db())
|
|
|
|
f, err := q.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-03-28 15:29:17 +01:00
|
|
|
q := query.New(conf.Db())
|
|
|
|
m, err := q.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
|
|
|
|
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-03-28 15:29:17 +01:00
|
|
|
q := query.New(conf.Db())
|
|
|
|
m, err := q.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
|
|
|
|
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})
|
|
|
|
})
|
|
|
|
}
|