Skip to content

Commit

Permalink
refactor: not allow the duplicate folders and files
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Aug 27, 2020
1 parent 41677fb commit 4348087
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var serverCmd = &cobra.Command{
&model.Share{},
&model.Storage{},
)
gormutil.Debug()

fmt.Println(conf.Provider)
diskProvider, err := disk.New(conf.Provider)
Expand Down
10 changes: 8 additions & 2 deletions rest/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,14 @@ func (f *FileResource) createFolder(c *gin.Context) {
}

uid := moreu.GetUserId(c)
if service.DirNotExist(uid, p.Dir) {
ginutil.JSONBadRequest(c, fmt.Errorf("direction %s not exist", p.Dir))
if !service.MatterParentExist(uid, p.Dir) {
ginutil.JSONBadRequest(c, fmt.Errorf("parent dir not exist"))
return
}

// matter exist
if service.MatterExist(uid, p.Name, p.Dir) {
ginutil.JSONBadRequest(c, fmt.Errorf("matter already exist"))
return
}

Expand Down
16 changes: 14 additions & 2 deletions rest/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package rest

import (
"fmt"
"path/filepath"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/saltbo/gopkg/ginutil"
"github.com/saltbo/gopkg/timeutil"
moreu "github.com/saltbo/moreu/client"
uuid "github.com/satori/go.uuid"

Expand Down Expand Up @@ -51,11 +55,19 @@ func (rs *LinkResource) createUploadURL(c *gin.Context) {
return
}

if service.DirNotExist(uid, p.Dir) {
ginutil.JSONBadRequest(c, fmt.Errorf("direction %s not exist", p.Dir))
if !service.MatterParentExist(uid, p.Dir) {
ginutil.JSONBadRequest(c, fmt.Errorf("parent dir not exist"))
return
}

// auto append a suffix if matter exist
if service.MatterExist(uid, p.Name, p.Dir) {
ext := filepath.Ext(p.Name)
name := strings.TrimSuffix(p.Name, ext)
suffix := fmt.Sprintf("_%s", timeutil.Format(time.Now(), "YYYYMMDD_HHmmss"))
p.Name = name + suffix + ext
}

publicRead := false
if p.Dir == ".pics/" {
publicRead = true
Expand Down
25 changes: 17 additions & 8 deletions service/matter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ import (
"github.com/saltbo/zpan/model"
)

func DirNotExist(uid int64, dir string) bool {
if dir == "" {
return false
func MatterExist(uid int64, name, parent string) bool {
return !gormutil.DB().Where("uid=? and name=? and parent=?", uid, name, parent).First(&model.Matter{}).RecordNotFound()
}

func MatterParentExist(uid int64, parentDir string) bool {
if parentDir == "" {
return true
}

// parent matter exist, eg: test123/234/
items := strings.Split(parentDir, "/")
name := items[len(items)-2] // -> 234
parent := strings.TrimSuffix(parentDir, name+"/") // -> test123/
if MatterExist(uid, name, parent) {
return true
}

items := strings.Split(dir, "/")
name := items[len(items)-2]
parent := strings.TrimSuffix(dir, name+"/")
return gormutil.DB().Where("uid=? and name=? and parent=?", uid, name, parent).First(&model.Matter{}).RecordNotFound()
return false
}

func FileGet(alias string) (*model.Matter, error) {
Expand Down Expand Up @@ -78,7 +87,7 @@ func (m *Matter) SetType(mt string) {
}

func (m *Matter) Find(offset, limit int) (list []model.Matter, total int64, err error) {
sn := gormutil.DB().Where(m.query, m.params...).Debug()
sn := gormutil.DB().Where(m.query, m.params...)
sn.Model(model.Matter{}).Count(&total)
sn = sn.Order("dirtype desc")
err = sn.Offset(offset).Limit(limit).Find(&list).Error
Expand Down

0 comments on commit 4348087

Please sign in to comment.