2019-12-11 18:11:44 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2020-02-02 02:36:00 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-06-25 12:54:04 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
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-06-07 08:09:35 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-01-05 13:18:40 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-06-25 12:54:04 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
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.
|
2020-06-25 12:54:04 +00:00
|
|
|
func SavePhotoAsYaml(p entity.Photo) {
|
|
|
|
conf := service.Config()
|
|
|
|
|
2020-05-18 20:18:58 +00:00
|
|
|
// Write YAML sidecar file (optional).
|
|
|
|
if conf.SidecarYaml() {
|
2020-06-07 08:09:35 +00:00
|
|
|
yamlFile := p.YamlFileName(conf.OriginalsPath(), conf.SidecarPath())
|
2020-05-19 09:00:17 +00:00
|
|
|
|
2020-05-18 20:18:58 +00:00
|
|
|
if err := p.SaveAsYaml(yamlFile); err != nil {
|
|
|
|
log.Errorf("photo: %s (update yaml)", err)
|
|
|
|
} else {
|
2020-06-07 08:09:35 +00:00
|
|
|
log.Infof("photo: updated yaml file %s", txt.Quote(fs.Rel(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
|
2020-06-25 12:54:04 +00:00
|
|
|
func GetPhoto(router *gin.RouterGroup) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.GET("/photos/:uid", func(c *gin.Context) {
|
2020-06-25 12:54:04 +00:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionRead)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2019-12-11 18:11:44 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:27:29 +00:00
|
|
|
p, err := query.PhotoPreloadByUID(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
|
2020-06-25 12:54:04 +00:00
|
|
|
func UpdatePhoto(router *gin.RouterGroup) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.PUT("/photos/:uid", func(c *gin.Context) {
|
2020-06-25 12:54:04 +00:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2019-12-11 18:11:44 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:54:04 +00:00
|
|
|
conf := service.Config()
|
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-05-26 11:43:16 +00:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
2020-04-20 08:38:01 +00:00
|
|
|
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-05-26 11:43:16 +00:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
2020-04-20 08:38:01 +00:00
|
|
|
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-26 17:27:29 +00:00
|
|
|
p, err := query.PhotoPreloadByUID(uid)
|
2020-01-30 05:17:02 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:54:04 +00:00
|
|
|
SavePhotoAsYaml(p)
|
2020-05-18 20:18:58 +00:00
|
|
|
|
2020-01-30 05:17:02 +00:00
|
|
|
c.JSON(http.StatusOK, p)
|
2019-12-11 18:11:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:38:40 +00:00
|
|
|
// GET /api/v1/photos/:uid/dl
|
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
|
2020-06-25 12:54:04 +00:00
|
|
|
func GetPhotoDownload(router *gin.RouterGroup) {
|
2020-05-27 17:38:40 +00:00
|
|
|
router.GET("/photos/:uid/dl", func(c *gin.Context) {
|
2020-06-25 12:54:04 +00:00
|
|
|
if InvalidDownloadToken(c) {
|
2020-05-27 17:38:40 +00:00
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
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-06-07 08:09:35 +00:00
|
|
|
fileName := photoprism.FileName(f.FileRoot, 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.
|
2020-05-28 14:26:22 +00:00
|
|
|
logError("photo", f.Update("FileMissing", true))
|
2020-05-25 17:10:44 +00:00
|
|
|
|
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-06-25 12:54:04 +00:00
|
|
|
func GetPhotoYaml(router *gin.RouterGroup) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.GET("/photos/:uid/yaml", func(c *gin.Context) {
|
2020-06-25 12:54:04 +00:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionExport)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-05-18 20:18:58 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:27:29 +00:00
|
|
|
p, err := query.PhotoPreloadByUID(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-06-09 07:20:20 +00:00
|
|
|
// POST /api/v1/photos/:uid/approve
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-06-25 12:54:04 +00:00
|
|
|
func ApprovePhoto(router *gin.RouterGroup) {
|
2020-06-09 07:20:20 +00:00
|
|
|
router.POST("/photos/:uid/approve", func(c *gin.Context) {
|
2020-06-25 12:54:04 +00:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-06-09 07:20:20 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := c.Param("uid")
|
|
|
|
m, err := query.PhotoByUID(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Approve(); err != nil {
|
|
|
|
log.Errorf("photo: %s", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:54:04 +00:00
|
|
|
SavePhotoAsYaml(m)
|
2020-06-09 07:20:20 +00:00
|
|
|
|
|
|
|
PublishPhotoEvent(EntityUpdated, id, c)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2020-06-25 12:54:04 +00:00
|
|
|
func LikePhoto(router *gin.RouterGroup) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.POST("/photos/:uid/like", func(c *gin.Context) {
|
2020-06-25 12:54:04 +00:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionLike)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2019-12-11 18:11:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:15:14 +00:00
|
|
|
if err := m.SetFavorite(true); err != nil {
|
2020-05-26 11:43:16 +00:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2019-12-11 18:11:44 +00:00
|
|
|
|
2020-06-25 12:54:04 +00:00
|
|
|
SavePhotoAsYaml(m)
|
2020-05-18 20:18:58 +00:00
|
|
|
|
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
|
2020-06-25 12:54:04 +00:00
|
|
|
func DislikePhoto(router *gin.RouterGroup) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.DELETE("/photos/:uid/like", func(c *gin.Context) {
|
2020-06-25 12:54:04 +00:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionLike)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2019-12-11 18:11:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:15:14 +00:00
|
|
|
if err := m.SetFavorite(false); err != nil {
|
2020-05-26 11:43:16 +00:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2019-12-11 18:11:44 +00:00
|
|
|
|
2020-06-25 12:54:04 +00:00
|
|
|
SavePhotoAsYaml(m)
|
2020-05-18 20:18:58 +00:00
|
|
|
|
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-06-25 12:54:04 +00:00
|
|
|
func SetPhotoPrimary(router *gin.RouterGroup) {
|
2020-05-23 18:58:58 +00:00
|
|
|
router.POST("/photos/:uid/primary/:file_uid", func(c *gin.Context) {
|
2020-06-25 12:54:04 +00:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePhotos, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-04-21 08:23:27 +00:00
|
|
|
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-26 17:27:29 +00:00
|
|
|
p, err := query.PhotoPreloadByUID(uid)
|
2020-04-21 08:23:27 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|