2019-12-11 19:11:44 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
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-06-07 10:09:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
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-05-18 22:18:58 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-11 19:11:44 +01:00
|
|
|
)
|
|
|
|
|
2020-05-18 22:18:58 +02: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-06-07 10:09:35 +02:00
|
|
|
yamlFile := p.YamlFileName(conf.OriginalsPath(), conf.SidecarPath())
|
2020-05-19 11:00:17 +02:00
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
if err := p.SaveAsYaml(yamlFile); err != nil {
|
|
|
|
log.Errorf("photo: %s (update yaml)", err)
|
|
|
|
} else {
|
2020-06-07 10:09:35 +02:00
|
|
|
log.Infof("photo: updated yaml file %s", txt.Quote(fs.Rel(yamlFile, conf.OriginalsPath())))
|
2020-05-18 22:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// GET /api/v1/photos/:uid
|
2019-12-11 19:11:44 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2019-12-11 19:11:44 +01:00
|
|
|
func GetPhoto(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.GET("/photos/:uid", func(c *gin.Context) {
|
2019-12-11 19:11:44 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:27:29 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(c.Param("uid"))
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// PUT /api/v1/photos/:uid
|
2019-12-11 19:11:44 +01:00
|
|
|
func UpdatePhoto(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.PUT("/photos/:uid", func(c *gin.Context) {
|
2019-12-11 19:11:44 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
uid := c.Param("uid")
|
|
|
|
m, err := query.PhotoByUID(uid)
|
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-05-26 13:43:16 +02:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
2020-04-20 10:38:01 +02:00
|
|
|
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-05-26 13:43:16 +02:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
2020-04-20 10:38:01 +02:00
|
|
|
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-23 20:58:58 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, uid, 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-26 19:27:29 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(uid)
|
2020-01-30 06:17:02 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
SavePhotoAsYaml(p, conf)
|
|
|
|
|
2020-01-30 06:17:02 +01:00
|
|
|
c.JSON(http.StatusOK, p)
|
2019-12-11 19:11:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 19:38:40 +02:00
|
|
|
// GET /api/v1/photos/:uid/dl
|
2019-12-11 19:11:44 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2019-12-11 19:11:44 +01:00
|
|
|
func GetPhotoDownload(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-27 19:38:40 +02:00
|
|
|
router.GET("/photos/:uid/dl", func(c *gin.Context) {
|
|
|
|
if InvalidDownloadToken(c, conf) {
|
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
f, err := query.FileByPhotoUID(c.Param("uid"))
|
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-06-07 10:09:35 +02:00
|
|
|
fileName := photoprism.FileName(f.FileRoot, f.FileName)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2020-05-25 19:10:44 +02:00
|
|
|
log.Errorf("photo: file %s is missing", txt.Quote(f.FileName))
|
2020-01-23 10:10:32 +01:00
|
|
|
c.Data(http.StatusNotFound, "image/svg+xml", photoIconSvg)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
// Set missing flag so that the file doesn't show up in search results anymore.
|
2020-05-28 16:26:22 +02:00
|
|
|
logError("photo", f.Update("FileMissing", true))
|
2020-05-25 19:10:44 +02:00
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// GET /api/v1/photos/:uid/yaml
|
2020-05-18 22:18:58 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-05-18 22:18:58 +02:00
|
|
|
func GetPhotoYaml(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.GET("/photos/:uid/yaml", func(c *gin.Context) {
|
2020-05-18 22:18:58 +02:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:27:29 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(c.Param("uid"))
|
2020-05-18 22:18:58 +02: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 20:58:58 +02:00
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", c.Param("uid")+fs.YamlExt))
|
2020-05-18 22:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, "text/x-yaml; charset=utf-8", data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:20:20 +02:00
|
|
|
// POST /api/v1/photos/:uid/approve
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uid: string PhotoUID as returned by the API
|
|
|
|
func ApprovePhoto(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/photos/:uid/approve", func(c *gin.Context) {
|
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
SavePhotoAsYaml(m, conf)
|
|
|
|
|
|
|
|
PublishPhotoEvent(EntityUpdated, id, c)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// POST /api/v1/photos/:uid/like
|
2019-12-11 19:11:44 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2019-12-11 19:11:44 +01:00
|
|
|
func LikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.POST("/photos/:uid/like", func(c *gin.Context) {
|
2019-12-11 19:11:44 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
id := c.Param("uid")
|
|
|
|
m, err := query.PhotoByUID(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
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
if err := m.SetFavorite(true); err != nil {
|
2020-05-26 13:43:16 +02:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
SavePhotoAsYaml(m, conf)
|
|
|
|
|
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-05-23 20:58:58 +02:00
|
|
|
// DELETE /api/v1/photos/:uid/like
|
2019-12-11 19:11:44 +01:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2019-12-11 19:11:44 +01:00
|
|
|
func DislikePhoto(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.DELETE("/photos/:uid/like", func(c *gin.Context) {
|
2019-12-11 19:11:44 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
id := c.Param("uid")
|
|
|
|
m, err := query.PhotoByUID(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
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
if err := m.SetFavorite(false); err != nil {
|
2020-05-26 13:43:16 +02:00
|
|
|
log.Errorf("photo: %s", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrSaveFailed)
|
|
|
|
return
|
|
|
|
}
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
SavePhotoAsYaml(m, conf)
|
|
|
|
|
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
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// POST /api/v1/photos/:uid/primary/:file_uid
|
2020-04-21 10:23:27 +02:00
|
|
|
//
|
|
|
|
// Parameters:
|
2020-05-23 20:58:58 +02:00
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-04-21 10:23:27 +02:00
|
|
|
func SetPhotoPrimary(router *gin.RouterGroup, conf *config.Config) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.POST("/photos/:uid/primary/:file_uid", func(c *gin.Context) {
|
2020-04-21 10:23:27 +02:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
uid := c.Param("uid")
|
|
|
|
fileUID := c.Param("file_uid")
|
|
|
|
err := query.SetPhotoPrimary(uid, fileUID)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, uid, c)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
event.Success("photo saved")
|
|
|
|
|
2020-05-26 19:27:29 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(uid)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, ErrPhotoNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|