From 4348087136cbef1486d0b900fefc959d2e672c2f Mon Sep 17 00:00:00 2001 From: saltbo Date: Thu, 27 Aug 2020 23:26:39 +0800 Subject: [PATCH] refactor: not allow the duplicate folders and files --- cmd/server.go | 1 + rest/file.go | 10 ++++++++-- rest/link.go | 16 ++++++++++++++-- service/matter.go | 25 +++++++++++++++++-------- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index bfa0779..fb8be3c 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -49,6 +49,7 @@ var serverCmd = &cobra.Command{ &model.Share{}, &model.Storage{}, ) + gormutil.Debug() fmt.Println(conf.Provider) diskProvider, err := disk.New(conf.Provider) diff --git a/rest/file.go b/rest/file.go index 78b7097..ba67499 100644 --- a/rest/file.go +++ b/rest/file.go @@ -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 } diff --git a/rest/link.go b/rest/link.go index dda2092..b093256 100644 --- a/rest/link.go +++ b/rest/link.go @@ -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" @@ -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 diff --git a/service/matter.go b/service/matter.go index 0317530..b1d3758 100644 --- a/service/matter.go +++ b/service/matter.go @@ -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) { @@ -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