2020-04-08 13:24:06 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-06-22 15:16:26 +02:00
|
|
|
"strings"
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-12-14 18:34:52 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2020-04-08 13:24:06 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-07-07 10:51:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-04-08 13:24:06 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2021-12-14 18:34:52 +01:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-04-08 13:24:06 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// PUT /api/v1/:entity/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdateLink(c *gin.Context) {
|
|
|
|
s := Auth(SessionID(c), acl.ResourceLinks, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Link
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
link := entity.FindLink(clean.Token(c.Param("link")))
|
2020-06-22 15:16:26 +02:00
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
link.SetSlug(f.ShareSlug)
|
|
|
|
link.MaxViews = f.MaxViews
|
2020-06-28 14:52:26 +02:00
|
|
|
link.LinkExpires = f.LinkExpires
|
2020-06-22 15:16:26 +02:00
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
if f.LinkToken != "" {
|
2020-07-05 15:03:12 +02:00
|
|
|
link.LinkToken = strings.TrimSpace(strings.ToLower(f.LinkToken))
|
2020-06-22 15:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.Password != "" {
|
|
|
|
if err := link.SetPassword(f.Password); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusConflict, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := link.Save(); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusConflict, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-12 15:13:16 +01:00
|
|
|
UpdateClientConfig()
|
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgAlbumSaved)
|
|
|
|
|
|
|
|
PublishAlbumEvent(EntityUpdated, link.ShareUID, c)
|
2020-06-22 15:16:26 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, link)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/v1/:entity/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func DeleteLink(c *gin.Context) {
|
|
|
|
s := Auth(SessionID(c), acl.ResourceLinks, acl.ActionDelete)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
link := entity.FindLink(clean.Token(c.Param("link")))
|
2020-06-22 15:16:26 +02:00
|
|
|
|
|
|
|
if err := link.Delete(); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusConflict, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-12 15:13:16 +01:00
|
|
|
UpdateClientConfig()
|
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgAlbumSaved)
|
|
|
|
|
|
|
|
PublishAlbumEvent(EntityUpdated, link.ShareUID, c)
|
2020-06-22 15:16:26 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, link)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLink returns a new link entity initialized with request data
|
2020-06-25 14:54:04 +02:00
|
|
|
func CreateLink(c *gin.Context) {
|
|
|
|
s := Auth(SessionID(c), acl.ResourceLinks, acl.ActionCreate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:51:46 +02:00
|
|
|
var f form.Link
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
link := entity.NewLink(clean.IdString(c.Param("uid")), f.CanComment, f.CanEdit)
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
link.SetSlug(f.ShareSlug)
|
|
|
|
link.MaxViews = f.MaxViews
|
2020-06-28 14:52:26 +02:00
|
|
|
link.LinkExpires = f.LinkExpires
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
if f.Password != "" {
|
|
|
|
if err := link.SetPassword(f.Password); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusConflict, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := link.Save(); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusConflict, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-12 15:13:16 +01:00
|
|
|
UpdateClientConfig()
|
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgAlbumSaved)
|
|
|
|
|
|
|
|
PublishAlbumEvent(EntityUpdated, link.ShareUID, c)
|
2020-06-22 15:16:26 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, link)
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// POST /api/v1/albums/:uid/links
|
2020-06-25 14:54:04 +02:00
|
|
|
func CreateAlbumLink(router *gin.RouterGroup) {
|
2020-06-16 12:51:46 +02:00
|
|
|
router.POST("/albums/:uid/links", func(c *gin.Context) {
|
2022-04-15 09:42:07 +02:00
|
|
|
if _, err := query.AlbumByUID(clean.IdString(c.Param("uid"))); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
2020-04-08 13:24:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
CreateLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/v1/albums/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdateAlbumLink(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.PUT("/albums/:uid/links/:link", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/v1/albums/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func DeleteAlbumLink(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.DELETE("/albums/:uid/links/:link", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
DeleteLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/albums/:uid/links
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetAlbumLinks(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.GET("/albums/:uid/links", func(c *gin.Context) {
|
2022-04-15 09:42:07 +02:00
|
|
|
m, err := query.AlbumByUID(clean.IdString(c.Param("uid")))
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
2020-04-08 13:24:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
c.JSON(http.StatusOK, m.Links())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/v1/photos/:uid/links
|
2020-06-25 14:54:04 +02:00
|
|
|
func CreatePhotoLink(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.POST("/photos/:uid/links", func(c *gin.Context) {
|
2022-04-15 09:42:07 +02:00
|
|
|
if _, err := query.PhotoByUID(clean.IdString(c.Param("uid"))); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortEntityNotFound(c)
|
2020-04-08 13:24:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
CreateLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// PUT /api/v1/photos/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdatePhotoLink(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.PUT("/photos/:uid/links/:link", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateLink(c)
|
2020-04-08 13:24:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// DELETE /api/v1/photos/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func DeletePhotoLink(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.DELETE("/photos/:uid/links/:link", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
DeleteLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// GET /api/v1/photos/:uid/links
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetPhotoLinks(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.GET("/photos/:uid/links", func(c *gin.Context) {
|
2022-04-15 09:42:07 +02:00
|
|
|
m, err := query.PhotoByUID(clean.IdString(c.Param("uid")))
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
2020-04-08 13:24:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
c.JSON(http.StatusOK, m.Links())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/v1/labels/:uid/links
|
2020-06-25 14:54:04 +02:00
|
|
|
func CreateLabelLink(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.POST("/labels/:uid/links", func(c *gin.Context) {
|
2022-04-15 09:42:07 +02:00
|
|
|
if _, err := query.LabelByUID(clean.IdString(c.Param("uid"))); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrLabelNotFound)
|
2020-04-08 13:24:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
CreateLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// PUT /api/v1/labels/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdateLabelLink(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.PUT("/labels/:uid/links/:link", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateLink(c)
|
2020-04-08 13:24:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// DELETE /api/v1/labels/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func DeleteLabelLink(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.DELETE("/labels/:uid/links/:link", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
DeleteLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
// GET /api/v1/labels/:uid/links
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetLabelLinks(router *gin.RouterGroup) {
|
2020-06-22 15:16:26 +02:00
|
|
|
router.GET("/labels/:uid/links", func(c *gin.Context) {
|
2022-04-15 09:42:07 +02:00
|
|
|
m, err := query.LabelByUID(clean.IdString(c.Param("uid")))
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrAlbumNotFound)
|
2020-04-08 13:24:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
c.JSON(http.StatusOK, m.Links())
|
2020-04-08 13:24:06 +02:00
|
|
|
})
|
|
|
|
}
|