Modify: add unique index for folder & file

pull/247/head
HFO4 2019-12-01 11:23:54 +08:00
parent b3f13c56bc
commit ceb25ce1c3
6 changed files with 16 additions and 17 deletions

View File

@ -10,12 +10,12 @@ import (
type File struct { type File struct {
// 表字段 // 表字段
gorm.Model gorm.Model
Name string Name string `gorm:"unique_index:idx_only_one"`
SourceName string SourceName string
UserID uint `gorm:"index:user_id"` UserID uint `gorm:"index:user_id;unique_index:idx_only_one"`
Size uint64 Size uint64
PicInfo string PicInfo string
FolderID uint `gorm:"index:folder_id"` FolderID uint `gorm:"index:folder_id;unique_index:idx_only_one"`
PolicyID uint PolicyID uint
Dir string `gorm:"size:65536"` Dir string `gorm:"size:65536"`

View File

@ -9,11 +9,11 @@ import (
type Folder struct { type Folder struct {
// 表字段 // 表字段
gorm.Model gorm.Model
Name string Name string `gorm:"unique_index:idx_only_one_name"`
ParentID uint `gorm:"index:parent_id"` ParentID uint `gorm:"index:parent_id;unique_index:idx_only_one_name"`
Position string `gorm:"size:65536"` Position string `gorm:"size:65536"`
OwnerID uint `gorm:"index:owner_id"` OwnerID uint `gorm:"index:owner_id"`
//PositionAbsolute string `gorm:"size:65536"` PositionAbsolute string `gorm:"size:65536"`
} }
// Create 创建目录 // Create 创建目录

View File

@ -143,6 +143,7 @@ func (user *User) AfterCreate(tx *gorm.DB) (err error) {
Name: "根目录", Name: "根目录",
Position: ".", Position: ".",
OwnerID: user.ID, OwnerID: user.ID,
PositionAbsolute: "/",
} }
tx.Create(defaultFolder) tx.Create(defaultFolder)
return err return err

View File

@ -42,7 +42,7 @@ func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model
Size: file.GetSize(), Size: file.GetSize(),
FolderID: parent.ID, FolderID: parent.ID,
PolicyID: fs.User.Policy.ID, PolicyID: fs.User.Policy.ID,
//Dir: parent.PositionAbsolute, Dir: parent.PositionAbsolute,
} }
_, err := newFile.Create() _, err := newFile.Create()

View File

@ -22,7 +22,7 @@ func TestFileSystem_AddFile(t *testing.T) {
Model: gorm.Model{ Model: gorm.Model{
ID: 1, ID: 1,
}, },
Position: "/", PositionAbsolute: "/我的文件",
} }
fs := FileSystem{ fs := FileSystem{
User: &model.User{ User: &model.User{

View File

@ -177,13 +177,11 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
} }
for _, file := range childFiles { for _, file := range childFiles {
filePath := path.Join(folder.Position, folder.Name, file.Name)
if processedPath == "" { if processedPath == "" {
if pathProcessor != nil { if pathProcessor != nil {
processedPath = pathProcessor(filePath) processedPath = pathProcessor(file.Dir)
} else { } else {
processedPath = filePath processedPath = file.Dir
} }
} }