2019-12-11 19:11:44 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-07-17 17:38:22 +02:00
|
|
|
"path/filepath"
|
2019-12-11 19:11:44 +01:00
|
|
|
|
2020-02-02 03:36:00 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-11-26 14:28:50 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
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-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
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-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-12-11 19:11:44 +01:00
|
|
|
)
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
// SavePhotoAsYaml saves photo data as YAML file.
|
2020-06-25 14:54:04 +02:00
|
|
|
func SavePhotoAsYaml(p entity.Photo) {
|
2020-12-17 18:24:55 +01:00
|
|
|
c := service.Config()
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
// Write YAML sidecar file (optional).
|
2020-12-18 09:11:42 +01:00
|
|
|
if !c.BackupYaml() {
|
2020-12-17 18:24:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := p.YamlFileName(c.OriginalsPath(), c.SidecarPath())
|
2020-05-19 11:00:17 +02:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if err := p.SaveAsYaml(fileName); err != nil {
|
|
|
|
log.Errorf("photo: %s (update yaml)", err)
|
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("photo: updated yaml file %s", clean.Log(filepath.Base(fileName)))
|
2020-05-18 22:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
// GetPhoto returns photo details as JSON.
|
2019-12-11 19:11:44 +01:00
|
|
|
//
|
2021-11-26 14:28:50 +01:00
|
|
|
// Route : GET /api/v1/photos/:uid
|
|
|
|
// Params:
|
|
|
|
// - uid (string) PhotoUID as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetPhoto(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.GET("/photos/:uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionView)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(clean.UID(c.Param("uid")))
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
// UpdatePhoto updates photo details and returns them as JSON.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// PUT /api/v1/photos/:uid
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdatePhoto(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.PUT("/photos/:uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-12-11 19:11:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
2020-05-23 20:58:58 +02:00
|
|
|
m, err := query.PhotoByUID(uid)
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
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-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusInternalServerError, i18n.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-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrBadRequest)
|
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-12-05 00:13:44 +01:00
|
|
|
if err := entity.SavePhotoForm(m, f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
Abort(c, http.StatusInternalServerError, i18n.ErrSaveFailed)
|
2020-02-02 03:36:00 +01:00
|
|
|
return
|
2021-08-30 18:58:27 +02:00
|
|
|
} else if f.PhotoPrivate {
|
|
|
|
FlushCoverCache()
|
2020-02-02 03:36:00 +01:00
|
|
|
}
|
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
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgChangesSaved)
|
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 {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-01-30 06:17:02 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
SavePhotoAsYaml(p)
|
2020-05-18 22:18:58 +02:00
|
|
|
|
2021-02-05 18:22:52 +01:00
|
|
|
UpdateClientConfig()
|
|
|
|
|
2020-01-30 06:17:02 +01:00
|
|
|
c.JSON(http.StatusOK, p)
|
2019-12-11 19:11:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
// GetPhotoDownload returns the primary file matching that belongs to the photo.
|
2019-12-11 19:11:44 +01:00
|
|
|
//
|
2021-11-26 14:28:50 +01:00
|
|
|
// Route :GET /api/v1/photos/:uid/dl
|
|
|
|
// Params:
|
|
|
|
// - uid (string) PhotoUID as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetPhotoDownload(router *gin.RouterGroup) {
|
2020-05-27 19:38:40 +02:00
|
|
|
router.GET("/photos/:uid/dl", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
if InvalidDownloadToken(c) {
|
2020-05-27 19:38:40 +02:00
|
|
|
c.Data(http.StatusForbidden, "image/svg+xml", brokenIconSvg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
f, err := query.FileByPhotoUID(clean.UID(c.Param("uid")))
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
c.Data(http.StatusNotFound, "image/svg+xml", photoIconSvg)
|
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) {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("photo: file %s is missing", clean.Log(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
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
c.FileAttachment(fileName, f.DownloadName(DownloadName(c), 0))
|
2019-12-11 19:11:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// GetPhotoYaml returns photo details as YAML.
|
2020-05-18 22:18:58 +02:00
|
|
|
//
|
2022-09-28 09:01:17 +02:00
|
|
|
// GET /api/v1/photos/:uid/yaml
|
|
|
|
// Params:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetPhotoYaml(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.GET("/photos/:uid/yaml", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.AccessAll)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-05-18 22:18:58 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(clean.UID(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") != "" {
|
2022-09-28 09:01:17 +02:00
|
|
|
AddDownloadHeader(c, clean.UID(c.Param("uid"))+fs.ExtYAML)
|
2020-05-18 22:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, "text/x-yaml; charset=utf-8", data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// ApprovePhoto marks a photo in review as approved.
|
2020-06-09 09:20:20 +02:00
|
|
|
//
|
2022-09-28 09:01:17 +02:00
|
|
|
// POST /api/v1/photos/:uid/approve
|
|
|
|
// Params:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string PhotoUID as returned by the API
|
2020-06-25 14:54:04 +02:00
|
|
|
func ApprovePhoto(router *gin.RouterGroup) {
|
2020-06-09 09:20:20 +02:00
|
|
|
router.POST("/photos/:uid/approve", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-06-09 09:20:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.UID(c.Param("uid"))
|
2020-06-09 09:20:20 +02:00
|
|
|
m, err := query.PhotoByUID(id)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-06-09 09:20:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Approve(); err != nil {
|
|
|
|
log.Errorf("photo: %s", err.Error())
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortSaveFailed(c)
|
2020-06-09 09:20:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
SavePhotoAsYaml(m)
|
2020-06-09 09:20:20 +02:00
|
|
|
|
|
|
|
PublishPhotoEvent(EntityUpdated, id, c)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"photo": m})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// PhotoPrimary sets the primary file for a photo.
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
2022-09-28 09:01:17 +02:00
|
|
|
// POST /photos/:uid/files/:file_uid/primary
|
|
|
|
// Params:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string PhotoUID as returned by the API
|
|
|
|
// file_uid: string File UID as returned by the API
|
2020-07-14 11:00:49 +02:00
|
|
|
func PhotoPrimary(router *gin.RouterGroup) {
|
2020-06-30 16:58:32 +02:00
|
|
|
router.POST("/photos/:uid/files/:file_uid/primary", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-04-21 10:23:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
|
|
|
fileUID := clean.UID(c.Param("file_uid"))
|
2020-05-23 20:58:58 +02:00
|
|
|
err := query.SetPhotoPrimary(uid, fileUID)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-04-21 10:23:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, uid, c)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
event.SuccessMsg(i18n.MsgChangesSaved)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
2020-05-26 19:27:29 +02:00
|
|
|
p, err := query.PhotoPreloadByUID(uid)
|
2020-04-21 10:23:27 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-04-21 10:23:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
})
|
|
|
|
}
|