parent
4abd5b2346
commit
0ee0ac5e89
@ -0,0 +1,31 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/jinzhu/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Share 分享模型
|
||||
type Share struct {
|
||||
gorm.Model
|
||||
Password string // 分享密码,空值为非加密分享
|
||||
IsDir bool // 原始资源是否为目录
|
||||
UserID uint // 创建用户ID
|
||||
SourceID uint // 原始资源ID
|
||||
Views int // 浏览数
|
||||
Downloads int // 下载数
|
||||
RemainDownloads int // 剩余下载配额,负值标识无限制
|
||||
Expires *time.Time // 过期时间,空值表示无过期时间
|
||||
Score int // 每人次下载扣除积分
|
||||
}
|
||||
|
||||
// Create 创建分享
|
||||
// TODO 测试
|
||||
func (share *Share) Create() (uint, error) {
|
||||
if err := DB.Create(share).Error; err != nil {
|
||||
util.Log().Warning("无法插入数据库记录, %s", err)
|
||||
return 0, err
|
||||
}
|
||||
return share.ID, nil
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package hashid
|
||||
|
||||
import "github.com/HFO4/cloudreve/pkg/conf"
|
||||
import "github.com/speps/go-hashids"
|
||||
|
||||
// ID类型
|
||||
const (
|
||||
ShareID = iota // 分享
|
||||
UserID // 用户
|
||||
)
|
||||
|
||||
// HashEncode 对给定数据计算HashID
|
||||
func HashEncode(v []int) (string, error) {
|
||||
hd := hashids.NewData()
|
||||
hd.Salt = conf.SystemConfig.HashIDSalt
|
||||
|
||||
h, err := hashids.NewWithData(hd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
id, err := h.Encode(v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// HashID 计算数据库内主键对应的HashID
|
||||
func HashID(id uint, t int) string {
|
||||
v, _ := HashEncode([]int{int(id), t})
|
||||
return v
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"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))
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/hashid"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ShareCreateService 创建新分享服务
|
||||
type ShareCreateService struct {
|
||||
SourceID uint `json:"id" binding:"required"`
|
||||
IsDir bool `json:"is_dir"`
|
||||
Password string `json:"password" binding:"max=255"`
|
||||
RemainDownloads int `json:"downloads"`
|
||||
Expire int `json:"expire"`
|
||||
Score int `json:"score" binding:"gte=0"`
|
||||
}
|
||||
|
||||
// Create 创建新分享
|
||||
func (service *ShareCreateService) Create(c *gin.Context) serializer.Response {
|
||||
userCtx, _ := c.Get("user")
|
||||
user := userCtx.(*model.User)
|
||||
|
||||
// 是否拥有权限
|
||||
if !user.Group.ShareEnabled {
|
||||
return serializer.Err(serializer.CodeNoRightErr, "您无权创建分享链接", nil)
|
||||
}
|
||||
|
||||
// 对象是否存在
|
||||
exist := true
|
||||
if service.IsDir {
|
||||
folder, err := model.GetFoldersByIDs([]uint{service.SourceID}, user.ID)
|
||||
if err != nil || len(folder) == 0 {
|
||||
exist = false
|
||||
}
|
||||
} else {
|
||||
file, err := model.GetFilesByIDs([]uint{service.SourceID}, user.ID)
|
||||
if err != nil || len(file) == 0 {
|
||||
exist = false
|
||||
}
|
||||
}
|
||||
if !exist {
|
||||
return serializer.Err(serializer.CodeNotFound, "原始资源不存在", nil)
|
||||
}
|
||||
|
||||
newShare := model.Share{
|
||||
Password: service.Password,
|
||||
IsDir: service.IsDir,
|
||||
UserID: user.ID,
|
||||
SourceID: service.SourceID,
|
||||
Score: service.RemainDownloads,
|
||||
}
|
||||
|
||||
// 如果开启了自动过期
|
||||
if service.RemainDownloads > 0 {
|
||||
expires := time.Now().Add(time.Duration(service.Expire) * time.Second)
|
||||
newShare.RemainDownloads = service.RemainDownloads
|
||||
newShare.Expires = &expires
|
||||
}
|
||||
|
||||
// 创建分享
|
||||
id, err := newShare.Create()
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeDBError, "分享链接创建失败", err)
|
||||
}
|
||||
|
||||
// 获取分享的唯一id
|
||||
uid := hashid.HashID(id, hashid.ShareID)
|
||||
// 最终得到分享链接
|
||||
siteURL := model.GetSiteURL()
|
||||
sharePath, _ := url.Parse("/#/s/" + uid)
|
||||
shareURL := siteURL.ResolveReference(sharePath)
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Data: shareURL.String(),
|
||||
}
|
||||
}
|
Loading…
Reference in new issue