2020-01-26 13:07:05 +08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2020-01-28 13:40:19 +08:00
|
|
|
"context"
|
2020-01-26 13:07:05 +08:00
|
|
|
"github.com/HFO4/cloudreve/service/share"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateShare 创建分享
|
|
|
|
func CreateShare(c *gin.Context) {
|
|
|
|
var service share.ShareCreateService
|
|
|
|
if err := c.ShouldBindJSON(&service); err == nil {
|
|
|
|
res := service.Create(c)
|
|
|
|
c.JSON(200, res)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 14:57:07 +08:00
|
|
|
|
|
|
|
// GetShare 查看分享
|
|
|
|
func GetShare(c *gin.Context) {
|
|
|
|
var service share.ShareGetService
|
|
|
|
if err := c.ShouldBindQuery(&service); err == nil {
|
|
|
|
res := service.Get(c)
|
|
|
|
c.JSON(200, res)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 12:41:00 +08:00
|
|
|
|
|
|
|
// GetShareDownload 创建分享下载会话
|
|
|
|
func GetShareDownload(c *gin.Context) {
|
|
|
|
var service share.SingleFileService
|
|
|
|
if err := c.ShouldBindQuery(&service); err == nil {
|
|
|
|
res := service.CreateDownloadSession(c)
|
|
|
|
c.JSON(200, res)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 13:40:19 +08:00
|
|
|
|
|
|
|
// PreviewShare 预览分享文件内容
|
|
|
|
func PreviewShare(c *gin.Context) {
|
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var service share.SingleFileService
|
|
|
|
if err := c.ShouldBindUri(&service); err == nil {
|
|
|
|
res := service.PreviewContent(ctx, c, false)
|
|
|
|
// 是否需要重定向
|
|
|
|
if res.Code == -301 {
|
|
|
|
c.Redirect(301, res.Data.(string))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 是否有错误发生
|
|
|
|
if res.Code != 0 {
|
|
|
|
c.JSON(200, res)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|