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"
|
|
|
|
"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"
|
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
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
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()
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
token := c.Param("token")
|
|
|
|
share := 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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if uid != share {
|
2020-06-28 14:52:26 +02:00
|
|
|
c.Redirect(http.StatusPermanentRedirect, fmt.Sprintf("/s/%s/%s", token, uid))
|
2020-06-27 13:08:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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/%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
|
|
|
})
|
|
|
|
}
|