From ceb25ce1c3f38d6b15b459f2af212a0cba2a5ac6 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sun, 1 Dec 2019 11:23:54 +0800 Subject: [PATCH] Modify: add unique index for folder & file --- models/file.go | 6 +++--- models/folder.go | 10 +++++----- models/user.go | 7 ++++--- pkg/filesystem/file.go | 2 +- pkg/filesystem/file_test.go | 2 +- pkg/filesystem/path.go | 6 ++---- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/models/file.go b/models/file.go index b9f118e..19d5fc8 100644 --- a/models/file.go +++ b/models/file.go @@ -10,12 +10,12 @@ import ( type File struct { // 表字段 gorm.Model - Name string + Name string `gorm:"unique_index:idx_only_one"` SourceName string - UserID uint `gorm:"index:user_id"` + UserID uint `gorm:"index:user_id;unique_index:idx_only_one"` Size uint64 PicInfo string - FolderID uint `gorm:"index:folder_id"` + FolderID uint `gorm:"index:folder_id;unique_index:idx_only_one"` PolicyID uint Dir string `gorm:"size:65536"` diff --git a/models/folder.go b/models/folder.go index 1691b79..c2d4517 100644 --- a/models/folder.go +++ b/models/folder.go @@ -9,11 +9,11 @@ import ( type Folder struct { // 表字段 gorm.Model - Name string - ParentID uint `gorm:"index:parent_id"` - Position string `gorm:"size:65536"` - OwnerID uint `gorm:"index:owner_id"` - //PositionAbsolute string `gorm:"size:65536"` + Name string `gorm:"unique_index:idx_only_one_name"` + ParentID uint `gorm:"index:parent_id;unique_index:idx_only_one_name"` + Position string `gorm:"size:65536"` + OwnerID uint `gorm:"index:owner_id"` + PositionAbsolute string `gorm:"size:65536"` } // Create 创建目录 diff --git a/models/user.go b/models/user.go index 88eb8d5..d59c132 100644 --- a/models/user.go +++ b/models/user.go @@ -140,9 +140,10 @@ func (user *User) BeforeSave() (err error) { func (user *User) AfterCreate(tx *gorm.DB) (err error) { // 创建用户的默认根目录 defaultFolder := &Folder{ - Name: "根目录", - Position: ".", - OwnerID: user.ID, + Name: "根目录", + Position: ".", + OwnerID: user.ID, + PositionAbsolute: "/", } tx.Create(defaultFolder) return err diff --git a/pkg/filesystem/file.go b/pkg/filesystem/file.go index 71b52b9..ec850f1 100644 --- a/pkg/filesystem/file.go +++ b/pkg/filesystem/file.go @@ -42,7 +42,7 @@ func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model Size: file.GetSize(), FolderID: parent.ID, PolicyID: fs.User.Policy.ID, - //Dir: parent.PositionAbsolute, + Dir: parent.PositionAbsolute, } _, err := newFile.Create() diff --git a/pkg/filesystem/file_test.go b/pkg/filesystem/file_test.go index ea3db7e..e47bfc4 100644 --- a/pkg/filesystem/file_test.go +++ b/pkg/filesystem/file_test.go @@ -22,7 +22,7 @@ func TestFileSystem_AddFile(t *testing.T) { Model: gorm.Model{ ID: 1, }, - Position: "/", + PositionAbsolute: "/我的文件", } fs := FileSystem{ User: &model.User{ diff --git a/pkg/filesystem/path.go b/pkg/filesystem/path.go index 4733a54..0e10f8e 100644 --- a/pkg/filesystem/path.go +++ b/pkg/filesystem/path.go @@ -177,13 +177,11 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu } for _, file := range childFiles { - filePath := path.Join(folder.Position, folder.Name, file.Name) - if processedPath == "" { if pathProcessor != nil { - processedPath = pathProcessor(filePath) + processedPath = pathProcessor(file.Dir) } else { - processedPath = filePath + processedPath = file.Dir } }