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"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
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-22 20:15:08 +02:00
|
|
|
|
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 {
|
|
|
|
log.Warn("share: invalid token")
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/")
|
|
|
|
return
|
|
|
|
}
|
2020-06-22 20:15:08 +02:00
|
|
|
|
2020-06-26 14:26:36 +02:00
|
|
|
clientConfig := conf.GuestConfig()
|
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
|
|
|
|
2020-06-27 09:28:32 +02:00
|
|
|
c.HTML(http.StatusOK, "share.tmpl", gin.H{"config": clientConfig})
|
2020-06-22 20:15:08 +02:00
|
|
|
})
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
router.GET("/:token/:share", 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"))
|
|
|
|
share := clean.Token(c.Param("share"))
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
links := entity.FindValidLinks(token, share)
|
2020-06-26 14:26:36 +02:00
|
|
|
|
2020-12-31 12:02:26 +01:00
|
|
|
if len(links) < 1 {
|
2020-06-28 14:52:26 +02:00
|
|
|
log.Warn("share: invalid token or share")
|
2020-06-26 14:26:36 +02:00
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
uid := links[0].ShareUID
|
2021-12-14 18:36:36 +01:00
|
|
|
clientConfig := conf.GuestConfig()
|
2020-06-27 13:08:45 +02:00
|
|
|
|
|
|
|
if uid != share {
|
2021-12-14 18:36:36 +01:00
|
|
|
c.Redirect(http.StatusPermanentRedirect, fmt.Sprintf("%ss/%s/%s", clientConfig.SiteUrl, token, uid))
|
2020-06-27 13:08:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
clientConfig.SiteUrl = fmt.Sprintf("%ss/%s/%s", clientConfig.SiteUrl, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "share.tmpl", gin.H{"config": clientConfig})
|
2020-06-22 20:15:08 +02:00
|
|
|
})
|
|
|
|
}
|