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"
|
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"
|
|
|
|
)
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// UpdateLink updates a share link and return it as JSON.
|
|
|
|
//
|
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) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceShares, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
|
|
|
if s.Invalid() {
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortForbidden(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Link
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2022-09-30 19:15:10 +02:00
|
|
|
log.Debugf("share: %s", err)
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// DeleteLink deletes a share link.
|
|
|
|
//
|
2020-06-22 15:16:26 +02:00
|
|
|
// DELETE /api/v1/:entity/:uid/links/:link
|
2020-06-25 14:54:04 +02:00
|
|
|
func DeleteLink(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceShares, acl.ActionDelete)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
|
|
|
if s.Invalid() {
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortForbidden(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)
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// CreateLink adds a new share link and return it as JSON.
|
|
|
|
//
|
|
|
|
// POST /api/v1/:entity/:uid/links
|
2020-06-25 14:54:04 +02:00
|
|
|
func CreateLink(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceShares, acl.ActionCreate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := clean.UID(c.Param("uid"))
|
|
|
|
|
|
|
|
if uid == "" {
|
|
|
|
AbortBadRequest(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 {
|
2022-09-30 19:15:10 +02:00
|
|
|
log.Debugf("share: %s", err)
|
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-10-02 11:38:30 +02:00
|
|
|
link := entity.NewUserLink(uid, s.UserUID)
|
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
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// CreateAlbumLink adds a new album share link and return it as JSON.
|
|
|
|
//
|
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-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if _, err := query.AlbumByUID(clean.UID(c.Param("uid"))); err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// UpdateAlbumLink updates an album share link and return it as JSON.
|
|
|
|
//
|
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) {
|
2022-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// DeleteAlbumLink deletes an album share link.
|
|
|
|
//
|
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) {
|
2022-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
DeleteLink(c)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// GetAlbumLinks returns all share links for the given UID as JSON.
|
|
|
|
//
|
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-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourceAlbums, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
m, err := query.AlbumByUID(clean.UID(c.Param("uid")))
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2020-04-08 13:24:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
c.JSON(http.StatusOK, m.Links())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
// CreatePhotoLink adds a new photo share link and return it as JSON.
|
|
|
|
//
|
2020-06-22 15:16:26 +02:00
|
|
|
// 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-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if _, err := query.PhotoByUID(clean.UID(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
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// UpdatePhotoLink updates an existing photo sharing link.
|
|
|
|
//
|
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) {
|
2022-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateLink(c)
|
2020-04-08 13:24:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// DeletePhotoLink deletes a photo sharing link.
|
|
|
|
//
|
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) {
|
2022-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// GetPhotoLinks returns all share links for the given UID as JSON.
|
|
|
|
//
|
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-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourcePhotos, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
m, err := query.PhotoByUID(clean.UID(c.Param("uid")))
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
2020-04-08 13:24:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
c.JSON(http.StatusOK, m.Links())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// CreateLabelLink adds a new label share link and return it as JSON.
|
|
|
|
//
|
2020-06-22 15:16:26 +02:00
|
|
|
// 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-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourceLabels, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if _, err := query.LabelByUID(clean.UID(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
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// UpdateLabelLink updates a label share link and return it as JSON.
|
|
|
|
//
|
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) {
|
2022-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourceLabels, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateLink(c)
|
2020-04-08 13:24:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// DeleteLabelLink deletes a label share link.
|
|
|
|
//
|
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) {
|
2022-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourceLabels, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// GetLabelLinks returns all share links for the given UID as JSON.
|
|
|
|
//
|
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-09-30 19:15:10 +02:00
|
|
|
s := Auth(c, acl.ResourceLabels, acl.ActionShare)
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
m, err := query.LabelByUID(clean.UID(c.Param("uid")))
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-01 15:57:19 +02:00
|
|
|
AbortAlbumNotFound(c)
|
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
|
|
|
})
|
|
|
|
}
|
2022-09-30 19:15:10 +02:00
|
|
|
*/
|