Skip to content

Commit b9f058f

Browse files
authored
fix(backup-restore): add shares (#1500)
1 parent 6de15b6 commit b9f058f

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

internal/db/sharing.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,26 @@ func GetSharingsByCreatorId(creator uint, pageIndex, pageSize int) (sharings []m
3838
}
3939

4040
func CreateSharing(s *model.SharingDB) (string, error) {
41-
id := random.String(8)
42-
for len(id) < 12 {
43-
old := model.SharingDB{
44-
ID: id,
41+
if s.ID == "" {
42+
id := random.String(8)
43+
for len(id) < 12 {
44+
old := model.SharingDB{
45+
ID: id,
46+
}
47+
if err := db.Where(old).First(&old).Error; err != nil {
48+
s.ID = id
49+
return id, errors.WithStack(db.Create(s).Error)
50+
}
51+
id += random.String(1)
4552
}
46-
if err := db.Where(old).First(&old).Error; err != nil {
47-
s.ID = id
48-
return id, errors.WithStack(db.Create(s).Error)
53+
return "", errors.New("failed find valid id")
54+
} else {
55+
query := model.SharingDB{ID: s.ID}
56+
if err := db.Where(query).First(&query).Error; err == nil {
57+
return "", errors.New("sharing already exist")
4958
}
50-
id += random.String(1)
59+
return s.ID, errors.WithStack(db.Create(s).Error)
5160
}
52-
return "", errors.New("failed find valid id")
5361
}
5462

5563
func UpdateSharing(s *model.SharingDB) error {

server/handles/sharing.go

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ func ListSharings(c *gin.Context) {
408408
})
409409
}
410410

411-
type CreateSharingReq struct {
411+
type UpdateSharingReq struct {
412412
Files []string `json:"files"`
413413
Expires *time.Time `json:"expires"`
414414
Pwd string `json:"pwd"`
@@ -418,12 +418,9 @@ type CreateSharingReq struct {
418418
Readme string `json:"readme"`
419419
Header string `json:"header"`
420420
model.Sort
421-
}
422-
423-
type UpdateSharingReq struct {
424-
ID string `json:"id"`
425-
Accessed int `json:"accessed"`
426-
CreateSharingReq
421+
CreatorName string `json:"creator"`
422+
Accessed int `json:"accessed"`
423+
ID string `json:"id"`
427424
}
428425

429426
func UpdateSharing(c *gin.Context) {
@@ -436,24 +433,38 @@ func UpdateSharing(c *gin.Context) {
436433
common.ErrorStrResp(c, "must add at least 1 object", 400)
437434
return
438435
}
439-
user := c.Request.Context().Value(conf.UserKey).(*model.User)
440-
if !user.CanShare() {
441-
common.ErrorStrResp(c, "permission denied", 403)
442-
return
436+
var user *model.User
437+
var err error
438+
reqUser := c.Request.Context().Value(conf.UserKey).(*model.User)
439+
if reqUser.IsAdmin() && req.CreatorName != "" {
440+
user, err = op.GetUserByName(req.CreatorName)
441+
if err != nil {
442+
common.ErrorStrResp(c, "no such a user", 400)
443+
return
444+
}
445+
} else {
446+
user = reqUser
447+
if !user.CanShare() {
448+
common.ErrorStrResp(c, "permission denied", 403)
449+
return
450+
}
443451
}
444452
for i, s := range req.Files {
445453
s = utils.FixAndCleanPath(s)
446454
req.Files[i] = s
447-
if !user.IsAdmin() && !strings.HasPrefix(s, user.BasePath) {
455+
if !reqUser.IsAdmin() && !strings.HasPrefix(s, user.BasePath) {
448456
common.ErrorStrResp(c, fmt.Sprintf("permission denied to share path [%s]", s), 500)
449457
return
450458
}
451459
}
452460
s, err := op.GetSharingById(req.ID)
453-
if err != nil || (!user.IsAdmin() && s.CreatorId != user.ID) {
461+
if err != nil || (!reqUser.IsAdmin() && s.CreatorId != user.ID) {
454462
common.ErrorStrResp(c, "sharing not found", 404)
455463
return
456464
}
465+
if reqUser.IsAdmin() && req.CreatorName == "" {
466+
user = s.Creator
467+
}
457468
s.Files = req.Files
458469
s.Expires = req.Expires
459470
s.Pwd = req.Pwd
@@ -464,6 +475,7 @@ func UpdateSharing(c *gin.Context) {
464475
s.Header = req.Header
465476
s.Readme = req.Readme
466477
s.Remark = req.Remark
478+
s.Creator = user
467479
if err = op.UpdateSharing(s); err != nil {
468480
common.ErrorResp(c, err, 500)
469481
} else {
@@ -476,7 +488,7 @@ func UpdateSharing(c *gin.Context) {
476488
}
477489

478490
func CreateSharing(c *gin.Context) {
479-
var req CreateSharingReq
491+
var req UpdateSharingReq
480492
var err error
481493
if err = c.ShouldBind(&req); err != nil {
482494
common.ErrorResp(c, err, 400)
@@ -486,24 +498,35 @@ func CreateSharing(c *gin.Context) {
486498
common.ErrorStrResp(c, "must add at least 1 object", 400)
487499
return
488500
}
489-
user := c.Request.Context().Value(conf.UserKey).(*model.User)
490-
if !user.CanShare() {
491-
common.ErrorStrResp(c, "permission denied", 403)
492-
return
501+
var user *model.User
502+
reqUser := c.Request.Context().Value(conf.UserKey).(*model.User)
503+
if reqUser.IsAdmin() && req.CreatorName != "" {
504+
user, err = op.GetUserByName(req.CreatorName)
505+
if err != nil {
506+
common.ErrorStrResp(c, "no such a user", 400)
507+
return
508+
}
509+
} else {
510+
user = reqUser
511+
if !user.CanShare() || (!user.IsAdmin() && req.ID != "") {
512+
common.ErrorStrResp(c, "permission denied", 403)
513+
return
514+
}
493515
}
494516
for i, s := range req.Files {
495517
s = utils.FixAndCleanPath(s)
496518
req.Files[i] = s
497-
if !user.IsAdmin() && !strings.HasPrefix(s, user.BasePath) {
519+
if !reqUser.IsAdmin() && !strings.HasPrefix(s, user.BasePath) {
498520
common.ErrorStrResp(c, fmt.Sprintf("permission denied to share path [%s]", s), 500)
499521
return
500522
}
501523
}
502524
s := &model.Sharing{
503525
SharingDB: &model.SharingDB{
526+
ID: req.ID,
504527
Expires: req.Expires,
505528
Pwd: req.Pwd,
506-
Accessed: 0,
529+
Accessed: req.Accessed,
507530
MaxAccessed: req.MaxAccessed,
508531
Disabled: req.Disabled,
509532
Sort: req.Sort,

0 commit comments

Comments
 (0)