fix(backup-restore): add shares (#1500)

This commit is contained in:
KirCute
2025-11-05 12:11:20 +08:00
committed by GitHub
parent 6de15b6310
commit b9f058fcc9
2 changed files with 60 additions and 29 deletions

View File

@@ -38,6 +38,7 @@ func GetSharingsByCreatorId(creator uint, pageIndex, pageSize int) (sharings []m
}
func CreateSharing(s *model.SharingDB) (string, error) {
if s.ID == "" {
id := random.String(8)
for len(id) < 12 {
old := model.SharingDB{
@@ -50,6 +51,13 @@ func CreateSharing(s *model.SharingDB) (string, error) {
id += random.String(1)
}
return "", errors.New("failed find valid id")
} else {
query := model.SharingDB{ID: s.ID}
if err := db.Where(query).First(&query).Error; err == nil {
return "", errors.New("sharing already exist")
}
return s.ID, errors.WithStack(db.Create(s).Error)
}
}
func UpdateSharing(s *model.SharingDB) error {

View File

@@ -408,7 +408,7 @@ func ListSharings(c *gin.Context) {
})
}
type CreateSharingReq struct {
type UpdateSharingReq struct {
Files []string `json:"files"`
Expires *time.Time `json:"expires"`
Pwd string `json:"pwd"`
@@ -418,12 +418,9 @@ type CreateSharingReq struct {
Readme string `json:"readme"`
Header string `json:"header"`
model.Sort
}
type UpdateSharingReq struct {
ID string `json:"id"`
CreatorName string `json:"creator"`
Accessed int `json:"accessed"`
CreateSharingReq
ID string `json:"id"`
}
func UpdateSharing(c *gin.Context) {
@@ -436,24 +433,38 @@ func UpdateSharing(c *gin.Context) {
common.ErrorStrResp(c, "must add at least 1 object", 400)
return
}
user := c.Request.Context().Value(conf.UserKey).(*model.User)
var user *model.User
var err error
reqUser := c.Request.Context().Value(conf.UserKey).(*model.User)
if reqUser.IsAdmin() && req.CreatorName != "" {
user, err = op.GetUserByName(req.CreatorName)
if err != nil {
common.ErrorStrResp(c, "no such a user", 400)
return
}
} else {
user = reqUser
if !user.CanShare() {
common.ErrorStrResp(c, "permission denied", 403)
return
}
}
for i, s := range req.Files {
s = utils.FixAndCleanPath(s)
req.Files[i] = s
if !user.IsAdmin() && !strings.HasPrefix(s, user.BasePath) {
if !reqUser.IsAdmin() && !strings.HasPrefix(s, user.BasePath) {
common.ErrorStrResp(c, fmt.Sprintf("permission denied to share path [%s]", s), 500)
return
}
}
s, err := op.GetSharingById(req.ID)
if err != nil || (!user.IsAdmin() && s.CreatorId != user.ID) {
if err != nil || (!reqUser.IsAdmin() && s.CreatorId != user.ID) {
common.ErrorStrResp(c, "sharing not found", 404)
return
}
if reqUser.IsAdmin() && req.CreatorName == "" {
user = s.Creator
}
s.Files = req.Files
s.Expires = req.Expires
s.Pwd = req.Pwd
@@ -464,6 +475,7 @@ func UpdateSharing(c *gin.Context) {
s.Header = req.Header
s.Readme = req.Readme
s.Remark = req.Remark
s.Creator = user
if err = op.UpdateSharing(s); err != nil {
common.ErrorResp(c, err, 500)
} else {
@@ -476,7 +488,7 @@ func UpdateSharing(c *gin.Context) {
}
func CreateSharing(c *gin.Context) {
var req CreateSharingReq
var req UpdateSharingReq
var err error
if err = c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
@@ -486,24 +498,35 @@ func CreateSharing(c *gin.Context) {
common.ErrorStrResp(c, "must add at least 1 object", 400)
return
}
user := c.Request.Context().Value(conf.UserKey).(*model.User)
if !user.CanShare() {
var user *model.User
reqUser := c.Request.Context().Value(conf.UserKey).(*model.User)
if reqUser.IsAdmin() && req.CreatorName != "" {
user, err = op.GetUserByName(req.CreatorName)
if err != nil {
common.ErrorStrResp(c, "no such a user", 400)
return
}
} else {
user = reqUser
if !user.CanShare() || (!user.IsAdmin() && req.ID != "") {
common.ErrorStrResp(c, "permission denied", 403)
return
}
}
for i, s := range req.Files {
s = utils.FixAndCleanPath(s)
req.Files[i] = s
if !user.IsAdmin() && !strings.HasPrefix(s, user.BasePath) {
if !reqUser.IsAdmin() && !strings.HasPrefix(s, user.BasePath) {
common.ErrorStrResp(c, fmt.Sprintf("permission denied to share path [%s]", s), 500)
return
}
}
s := &model.Sharing{
SharingDB: &model.SharingDB{
ID: req.ID,
Expires: req.Expires,
Pwd: req.Pwd,
Accessed: 0,
Accessed: req.Accessed,
MaxAccessed: req.MaxAccessed,
Disabled: req.Disabled,
Sort: req.Sort,