2020-01-27 00:34:39 -05:00
|
|
|
|
package share
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-27 23:41:00 -05:00
|
|
|
|
"context"
|
2020-01-27 00:34:39 -05:00
|
|
|
|
"fmt"
|
|
|
|
|
model "github.com/HFO4/cloudreve/models"
|
2020-01-27 23:41:00 -05:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem"
|
2020-01-28 00:40:19 -05:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
2020-01-27 00:34:39 -05:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
2020-01-28 00:40:19 -05:00
|
|
|
|
"github.com/HFO4/cloudreve/service/explorer"
|
2020-01-27 00:34:39 -05:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ShareGetService 获取分享服务
|
|
|
|
|
type ShareGetService struct {
|
|
|
|
|
Password string `form:"password" binding:"max=255"`
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 23:41:00 -05:00
|
|
|
|
// SingleFileService 对单文件进行操作的服务,path为可选文件完整路径
|
|
|
|
|
type SingleFileService struct {
|
|
|
|
|
Path string `form:"path" binding:"max=65535"`
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 00:34:39 -05:00
|
|
|
|
// Get 获取分享内容
|
|
|
|
|
func (service *ShareGetService) Get(c *gin.Context) serializer.Response {
|
2020-01-29 00:45:27 -05:00
|
|
|
|
shareCtx, _ := c.Get("share")
|
|
|
|
|
share := shareCtx.(*model.Share)
|
|
|
|
|
userCtx, _ := c.Get("user")
|
|
|
|
|
user := userCtx.(*model.User)
|
2020-01-27 00:34:39 -05:00
|
|
|
|
|
|
|
|
|
// 是否已解锁
|
|
|
|
|
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})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 23:41:00 -05:00
|
|
|
|
if unlocked {
|
|
|
|
|
share.Viewed()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果已经下载过,不需要付积分
|
2020-01-28 22:03:09 -05:00
|
|
|
|
if share.WasDownloadedBy(user, c) {
|
2020-01-27 23:41:00 -05:00
|
|
|
|
share.Score = 0
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 00:34:39 -05:00
|
|
|
|
return serializer.Response{
|
|
|
|
|
Code: 0,
|
|
|
|
|
Data: serializer.BuildShareResponse(share, unlocked),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-27 23:41:00 -05:00
|
|
|
|
|
|
|
|
|
// CreateDownloadSession 创建下载会话
|
|
|
|
|
func (service *SingleFileService) CreateDownloadSession(c *gin.Context) serializer.Response {
|
2020-01-29 00:45:27 -05:00
|
|
|
|
shareCtx, _ := c.Get("share")
|
|
|
|
|
share := shareCtx.(*model.Share)
|
|
|
|
|
userCtx, _ := c.Get("user")
|
|
|
|
|
user := userCtx.(*model.User)
|
2020-01-27 23:41:00 -05:00
|
|
|
|
|
|
|
|
|
// 创建文件系统
|
|
|
|
|
fs, err := filesystem.NewFileSystem(user)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
|
|
|
|
}
|
|
|
|
|
defer fs.Recycle()
|
|
|
|
|
|
|
|
|
|
// 重设文件系统处理目标为源文件
|
|
|
|
|
err = fs.SetTargetByInterface(share.GetSource())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serializer.Err(serializer.CodePolicyNotAllowed, "源文件不存在", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取得下载地址
|
|
|
|
|
downloadURL, err := fs.GetDownloadURL(context.Background(), "", "download_timeout")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serializer.Response{
|
|
|
|
|
Code: 0,
|
|
|
|
|
Data: downloadURL,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-28 00:40:19 -05:00
|
|
|
|
|
|
|
|
|
// PreviewContent 预览文件,需要登录会话, isText - 是否为文本文件,文本文件会
|
|
|
|
|
// 强制经由服务端中转
|
|
|
|
|
func (service *SingleFileService) PreviewContent(ctx context.Context, c *gin.Context, isText bool) serializer.Response {
|
2020-01-29 00:45:27 -05:00
|
|
|
|
shareCtx, _ := c.Get("share")
|
|
|
|
|
share := shareCtx.(*model.Share)
|
2020-01-28 00:40:19 -05:00
|
|
|
|
|
2020-01-28 22:03:09 -05:00
|
|
|
|
// 用于调下层service
|
2020-01-28 00:40:19 -05:00
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.GetSource())
|
|
|
|
|
subService := explorer.SingleFileService{
|
|
|
|
|
Path: "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return subService.PreviewContent(ctx, c, isText)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 23:16:52 -05:00
|
|
|
|
// CreateDocPreviewSession 创建Office预览会话,返回预览地址
|
|
|
|
|
func (service *SingleFileService) CreateDocPreviewSession(c *gin.Context) serializer.Response {
|
2020-01-29 00:45:27 -05:00
|
|
|
|
shareCtx, _ := c.Get("share")
|
|
|
|
|
share := shareCtx.(*model.Share)
|
2020-01-28 23:16:52 -05:00
|
|
|
|
|
|
|
|
|
// 用于调下层service
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, share.GetSource())
|
|
|
|
|
subService := explorer.SingleFileService{
|
|
|
|
|
Path: "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return subService.CreateDocPreviewSession(ctx, c)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 00:45:27 -05:00
|
|
|
|
// SaveToMyFile 将此分享转存到自己的网盘
|
|
|
|
|
func (service *SingleFileService) SaveToMyFile(c *gin.Context) serializer.Response {
|
|
|
|
|
shareCtx, _ := c.Get("share")
|
|
|
|
|
share := shareCtx.(*model.Share)
|
|
|
|
|
userCtx, _ := c.Get("user")
|
|
|
|
|
user := userCtx.(*model.User)
|
|
|
|
|
|
|
|
|
|
// 不能转存自己的文件
|
|
|
|
|
if share.UserID == user.ID {
|
|
|
|
|
return serializer.Err(serializer.CodePolicyNotAllowed, "不能转存自己的分享", nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建文件系统
|
|
|
|
|
fs, err := filesystem.NewFileSystem(user)
|
2020-01-28 00:40:19 -05:00
|
|
|
|
if err != nil {
|
2020-01-29 00:45:27 -05:00
|
|
|
|
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
2020-01-28 00:40:19 -05:00
|
|
|
|
}
|
2020-01-29 00:45:27 -05:00
|
|
|
|
defer fs.Recycle()
|
2020-01-28 00:40:19 -05:00
|
|
|
|
|
2020-01-29 00:45:27 -05:00
|
|
|
|
// 重设文件系统处理目标为源文件
|
|
|
|
|
err = fs.SetTargetByInterface(share.GetSource())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serializer.Err(serializer.CodePolicyNotAllowed, "源文件不存在", err)
|
2020-01-28 22:03:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 00:45:27 -05:00
|
|
|
|
err = fs.SaveTo(context.Background(), service.Path)
|
2020-01-28 00:40:19 -05:00
|
|
|
|
if err != nil {
|
2020-01-29 00:45:27 -05:00
|
|
|
|
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
2020-01-28 00:40:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 00:45:27 -05:00
|
|
|
|
return serializer.Response{}
|
2020-01-28 00:40:19 -05:00
|
|
|
|
}
|