mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: get share info / unlock share
This commit is contained in:
@@ -19,11 +19,6 @@ type ShareCreateService struct {
|
||||
Score int `json:"score" binding:"gte=0"`
|
||||
}
|
||||
|
||||
// ShareGetService 获取分享服务
|
||||
type ShareGetService struct {
|
||||
Password string `form:"password" binding:"max=255"`
|
||||
}
|
||||
|
||||
// Create 创建新分享
|
||||
func (service *ShareCreateService) Create(c *gin.Context) serializer.Response {
|
||||
user := currentUser(c)
|
||||
@@ -51,11 +46,12 @@ func (service *ShareCreateService) Create(c *gin.Context) serializer.Response {
|
||||
}
|
||||
|
||||
newShare := model.Share{
|
||||
Password: service.Password,
|
||||
IsDir: service.IsDir,
|
||||
UserID: user.ID,
|
||||
SourceID: service.SourceID,
|
||||
Score: service.Score,
|
||||
Password: service.Password,
|
||||
IsDir: service.IsDir,
|
||||
UserID: user.ID,
|
||||
SourceID: service.SourceID,
|
||||
Score: service.Score,
|
||||
RemainDownloads: -1,
|
||||
}
|
||||
|
||||
// 如果开启了自动过期
|
||||
@@ -85,20 +81,6 @@ func (service *ShareCreateService) Create(c *gin.Context) serializer.Response {
|
||||
|
||||
}
|
||||
|
||||
// Get 获取分享内容
|
||||
func (service *ShareGetService) Get(c *gin.Context) serializer.Response {
|
||||
user := currentUser(c)
|
||||
share := model.GetShareByHashID(c.Param("id"))
|
||||
if share == nil {
|
||||
return serializer.Err(serializer.CodeNotFound, "分享不存在或已被取消", nil)
|
||||
}
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Data: user,
|
||||
}
|
||||
}
|
||||
|
||||
func currentUser(c *gin.Context) *model.User {
|
||||
var user *model.User
|
||||
if userCtx, ok := c.Get("user"); ok {
|
||||
|
||||
41
service/share/visit.go
Normal file
41
service/share/visit.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ShareGetService 获取分享服务
|
||||
type ShareGetService struct {
|
||||
Password string `form:"password" binding:"max=255"`
|
||||
}
|
||||
|
||||
// Get 获取分享内容
|
||||
func (service *ShareGetService) Get(c *gin.Context) serializer.Response {
|
||||
share := model.GetShareByHashID(c.Param("id"))
|
||||
if share == nil || !share.IsAvailable() {
|
||||
return serializer.Err(serializer.CodeNotFound, "分享不存在或已被取消", nil)
|
||||
}
|
||||
|
||||
// 是否已解锁
|
||||
unlocked := true
|
||||
if share.Password != "" {
|
||||
sessionKey := fmt.Sprintf("share_unlock_%d", share.ID)
|
||||
unlocked = util.GetSession(c, sessionKey) != nil
|
||||
if !unlocked && service.Password != "" {
|
||||
// 如果未解锁,且指定了密码,则尝试解锁
|
||||
if service.Password == share.Password {
|
||||
unlocked = true
|
||||
util.SetSession(c, map[string]interface{}{sessionKey: true})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Data: serializer.BuildShareResponse(share, unlocked),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user