2019-12-11 18:11:44 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-12-13 15:25:47 +00:00
|
|
|
"path"
|
2019-12-11 18:11:44 +00:00
|
|
|
|
2020-02-02 02:36:00 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-12-11 18:11:44 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-02-02 02:36:00 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-11 18:11:44 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-02-02 02:36:00 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-01-05 13:18:40 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-01-13 10:07:09 +00:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-18 20:18:58 +00:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-11 18:11:44 +00:00
|
|
|
)
|
|
|
|
|
2020-05-18 20:18:58 +00:00
|
|
|
// SavePhotoAsYaml saves photo data as YAML file.
|
|
|
|
func SavePhotoAsYaml(p entity.Photo, conf *config.Config) {
|
|
|
|
// Write YAML sidecar file (optional).
|
|
|
|
if conf.SidecarYaml() {
|
2020-05-19 09:00:17 +00:00
|
|
|
yamlFile := p.YamlFileName(conf.OriginalsPath(), conf.SidecarHidden())
|
|
|
|
|
2020-05-18 20:18:58 +00:00
|
|
|
if err := p.SaveAsYaml(yamlFile); err != nil {
|
|
|
|
log.Errorf("photo: %s (update yaml)", err)
|
|
|
|
} else {
|
2020-05-19 07:21:10 +00:00
|
|
|
log.Infof("photo: updated yaml file %s", txt.Quote(fs.RelativeName(yamlFile, conf.OriginalsPath())))
|
2020-05-18 20:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
// GET /api/v1/photos/:uid
|
2019-12-11 18:11:44 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 18:58:58 +00:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2019-12-11 18:11:44 +00:00
|
|
|
func GetPhoto(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.GET("/photos/:uid", func(c *gin.Context) {
|
2019-12-11 18:11:44 +00:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
p, err := query.PreloadPhotoByUID(c.Param("uid"))
|
2019-12-11 18:11:44 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 09:16:18 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 18:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:22:29 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, p)
|
2019-12-11 18:11:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
// PUT /api/v1/photos/:uid
|
2019-12-11 18:11:44 +00:00
|
|
|
func UpdatePhoto(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.PUT("/photos/:uid", func(c *gin.Context) {
|
2019-12-11 18:11:44 +00:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
uid := c.Param("uid")
|
|
|
|
m, err := query.PhotoByUID(uid)
|
2019-12-11 18:11:44 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 09:10:32 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 18:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-02 02:36:00 +00: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 08:38:01 +00:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
2020-02-02 02:36:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) Update form with values from request
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-04-20 08:38:01 +00:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, ErrFormInvalid)
|
2019-12-11 18:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-02 02:36:00 +00:00
|
|
|
// 3) Save model with values from form
|
2020-04-30 18:07:03 +00:00
|
|
|
if err := entity.SavePhotoForm(m, f, conf.GeoCodingApi()); err != nil {
|
2020-04-20 08:38:01 +00:00
|
|
|
log.Error(err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
2020-02-02 02:36:00 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-11 18:11:44 +00:00
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
PublishPhotoEvent(EntityUpdated, uid, c)
|
2020-01-30 08:51:23 +00:00
|
|
|
|
2019-12-27 06:02:49 +00:00
|
|
|
event.Success("photo saved")
|
2019-12-11 18:11:44 +00:00
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
p, err := query.PreloadPhotoByUID(uid)
|
2020-01-30 05:17:02 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:18:58 +00:00
|
|
|
SavePhotoAsYaml(p, conf)
|
|
|
|
|
2020-01-30 05:17:02 +00:00
|
|
|
c.JSON(http.StatusOK, p)
|
2019-12-11 18:11:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
// GET /api/v1/photos/:uid/download
|
2019-12-11 18:11:44 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 18:58:58 +00:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2019-12-11 18:11:44 +00:00
|
|
|
func GetPhotoDownload(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.GET("/photos/:uid/download", func(c *gin.Context) {
|
|
|
|
f, err := query.FileByPhotoUID(c.Param("uid"))
|
2019-12-11 18:11:44 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 09:10:32 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 18:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 13:32:15 +00:00
|
|
|
fileName := path.Join(conf.OriginalsPath(), f.FileName)
|
2019-12-11 18:11:44 +00:00
|
|
|
|
2020-01-12 13:00:56 +00:00
|
|
|
if !fs.FileExists(fileName) {
|
2020-05-25 17:10:44 +00:00
|
|
|
log.Errorf("photo: file %s is missing", txt.Quote(f.FileName))
|
2020-01-23 09:10:32 +00:00
|
|
|
c.Data(http.StatusNotFound, "image/svg+xml", photoIconSvg)
|
2019-12-11 18:11:44 +00:00
|
|
|
|
2020-05-25 17:10:44 +00:00
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
|
|
|
report("photo", f.Update("FileMissing", true))
|
|
|
|
|
2019-12-11 18:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-01 10:00:45 +00:00
|
|
|
downloadFileName := f.ShareFileName()
|
2019-12-11 18:11:44 +00:00
|
|
|
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", downloadFileName))
|
|
|
|
|
|
|
|
c.File(fileName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
// GET /api/v1/photos/:uid/yaml
|
2020-05-18 20:18:58 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 18:58:58 +00:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-05-18 20:18:58 +00:00
|
|
|
func GetPhotoYaml(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.GET("/photos/:uid/yaml", func(c *gin.Context) {
|
2020-05-18 20:18:58 +00:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
p, err := query.PreloadPhotoByUID(c.Param("uid"))
|
2020-05-18 20:18:58 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := p.Yaml()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Query("download") != "" {
|
2020-05-23 18:58:58 +00:00
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", c.Param("uid")+fs.YamlExt))
|
2020-05-18 20:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, "text/x-yaml; charset=utf-8", data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
// POST /api/v1/photos/:uid/like
|
2019-12-11 18:11:44 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 18:58:58 +00:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2019-12-11 18:11:44 +00:00
|
|
|
func LikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.POST("/photos/:uid/like", func(c *gin.Context) {
|
2019-12-11 18:11:44 +00:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
id := c.Param("uid")
|
|
|
|
m, err := query.PhotoByUID(id)
|
2019-12-11 18:11:44 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 09:10:32 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 18:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.PhotoFavorite = true
|
2020-04-24 11:21:18 +00:00
|
|
|
m.PhotoQuality = m.QualityScore()
|
2019-12-11 18:11:44 +00:00
|
|
|
conf.Db().Save(&m)
|
|
|
|
|
2020-05-18 20:18:58 +00:00
|
|
|
SavePhotoAsYaml(m, conf)
|
|
|
|
|
2019-12-11 18:11:44 +00:00
|
|
|
event.Publish("count.favorites", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
|
2020-05-08 13:41:01 +00:00
|
|
|
PublishPhotoEvent(EntityUpdated, id, c)
|
2020-01-30 08:51:23 +00:00
|
|
|
|
2019-12-11 18:11:44 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
// DELETE /api/v1/photos/:uid/like
|
2019-12-11 18:11:44 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 18:58:58 +00:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2019-12-11 18:11:44 +00:00
|
|
|
func DislikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.DELETE("/photos/:uid/like", func(c *gin.Context) {
|
2019-12-11 18:11:44 +00:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
id := c.Param("uid")
|
|
|
|
m, err := query.PhotoByUID(id)
|
2019-12-11 18:11:44 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-23 09:10:32 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
2019-12-11 18:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.PhotoFavorite = false
|
2020-04-24 11:21:18 +00:00
|
|
|
m.PhotoQuality = m.QualityScore()
|
2020-05-08 13:41:01 +00:00
|
|
|
entity.Db().Save(&m)
|
2019-12-11 18:11:44 +00:00
|
|
|
|
2020-05-18 20:18:58 +00:00
|
|
|
SavePhotoAsYaml(m, conf)
|
|
|
|
|
2019-12-11 18:11:44 +00:00
|
|
|
event.Publish("count.favorites", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
|
2020-05-08 13:41:01 +00:00
|
|
|
PublishPhotoEvent(EntityUpdated, id, c)
|
2020-01-30 08:51:23 +00:00
|
|
|
|
2019-12-11 18:11:44 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
|
|
})
|
|
|
|
}
|
2020-04-21 08:23:27 +00:00
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
// POST /api/v1/photos/:uid/primary/:file_uid
|
2020-04-21 08:23:27 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 18:58:58 +00:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-04-21 08:23:27 +00:00
|
|
|
func SetPhotoPrimary(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.POST("/photos/:uid/primary/:file_uid", func(c *gin.Context) {
|
2020-04-21 08:23:27 +00:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
uid := c.Param("uid")
|
|
|
|
fileUID := c.Param("file_uid")
|
|
|
|
err := query.SetPhotoPrimary(uid, fileUID)
|
2020-04-21 08:23:27 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
PublishPhotoEvent(EntityUpdated, uid, c)
|
2020-04-21 08:23:27 +00:00
|
|
|
|
|
|
|
event.Success("photo saved")
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
p, err := query.PreloadPhotoByUID(uid)
|
2020-04-21 08:23:27 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|