2020-06-22 20:15:08 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-06-26 14:26:36 +02:00
|
|
|
"fmt"
|
2020-06-22 20:15:08 +02:00
|
|
|
"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"
|
2020-06-27 09:28:32 +02: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-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) {
|
|
|
|
conf := service.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()
|
2020-06-28 14:52:26 +02:00
|
|
|
clientConfig.SiteUrl = fmt.Sprintf("%ss/%s", clientConfig.SiteUrl, token)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
uri := conf.BaseUri("/albums")
|
|
|
|
c.HTML(http.StatusOK, "share.tmpl", 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) {
|
2020-06-26 14:26:36 +02:00
|
|
|
conf := service.Config()
|
|
|
|
|
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()
|
2022-10-02 11:38:30 +02:00
|
|
|
clientConfig.SiteUrl = fmt.Sprintf("%s/%s", clientConfig.SiteUrl, path.Join("s", token, uid))
|
2020-06-27 17:39:53 +02:00
|
|
|
clientConfig.SitePreview = fmt.Sprintf("%s/preview", clientConfig.SiteUrl)
|
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-02 11:38:30 +02:00
|
|
|
uri := conf.BaseUri(path.Join("/albums", uid, shared))
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "share.tmpl", gin.H{"shared": gin.H{"token": token, "uri": uri}, "config": clientConfig})
|
2020-06-22 20:15:08 +02:00
|
|
|
})
|
|
|
|
}
|