2020-06-22 20:15:08 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-10-02 11:38:30 +02:00
|
|
|
"path"
|
2020-06-22 20:15:08 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-12-14 18:36:36 +01:00
|
|
|
|
2020-06-22 20:15:08 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2020-06-27 09:28:32 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-06-22 20:15:08 +02:00
|
|
|
)
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Shares handles link share
|
|
|
|
//
|
2020-06-26 14:26:36 +02:00
|
|
|
// GET /s/:token/...
|
|
|
|
func Shares(router *gin.RouterGroup) {
|
|
|
|
router.GET("/:token", func(c *gin.Context) {
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-06-22 20:15:08 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
token := clean.Token(c.Param("token"))
|
2020-06-28 14:52:26 +02:00
|
|
|
links := entity.FindValidLinks(token, "")
|
2020-06-22 20:15:08 +02:00
|
|
|
|
2020-06-26 14:26:36 +02:00
|
|
|
if len(links) == 0 {
|
2022-10-02 11:38:30 +02:00
|
|
|
log.Debugf("share: invalid token")
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.BaseUri(""))
|
2020-06-26 14:26:36 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-22 20:15:08 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
clientConfig := conf.ClientShare()
|
2023-08-21 11:29:41 +02:00
|
|
|
clientConfig.SiteUrl = clientConfig.SiteUrl + path.Join("s", token)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-10-15 23:08:26 +02:00
|
|
|
uri := conf.BaseUri("/library/albums")
|
2022-10-18 14:21:23 +02:00
|
|
|
c.HTML(http.StatusOK, "share.gohtml", gin.H{"shared": gin.H{"token": token, "uri": uri}, "config": clientConfig})
|
2020-06-22 20:15:08 +02:00
|
|
|
})
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
router.GET("/:token/:shared", func(c *gin.Context) {
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
token := clean.Token(c.Param("token"))
|
2022-10-02 11:38:30 +02:00
|
|
|
shared := clean.Token(c.Param("shared"))
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
links := entity.FindValidLinks(token, shared)
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2020-12-31 12:02:26 +01:00
|
|
|
if len(links) < 1 {
|
2022-10-02 11:38:30 +02:00
|
|
|
log.Debugf("share: invalid token or slug")
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, conf.BaseUri(""))
|
2020-06-26 14:26:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
uid := links[0].ShareUID
|
2022-09-28 09:01:17 +02:00
|
|
|
clientConfig := conf.ClientShare()
|
2023-08-21 11:29:41 +02:00
|
|
|
clientConfig.SiteUrl = clientConfig.SiteUrl + path.Join("s", token, uid)
|
|
|
|
clientConfig.SitePreview = clientConfig.SiteUrl + "/preview"
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
if a, err := query.AlbumByUID(uid); err == nil {
|
2020-06-27 09:28:32 +02:00
|
|
|
clientConfig.SiteCaption = a.AlbumTitle
|
|
|
|
|
|
|
|
if a.AlbumDescription != "" {
|
|
|
|
clientConfig.SiteDescription = a.AlbumDescription
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 23:08:26 +02:00
|
|
|
uri := conf.BaseUri(path.Join("/library/albums", uid, shared))
|
2022-10-02 11:38:30 +02:00
|
|
|
|
2022-10-18 14:21:23 +02:00
|
|
|
c.HTML(http.StatusOK, "share.gohtml", gin.H{"shared": gin.H{"token": token, "uri": uri}, "config": clientConfig})
|
2020-06-22 20:15:08 +02:00
|
|
|
})
|
|
|
|
}
|