Modify: add unique index for folder & file

pull/247/head
HFO4 5 years ago
parent b3f13c56bc
commit ceb25ce1c3

@ -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"`

@ -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 创建目录

@ -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

@ -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()

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

@ -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
}
}

Loading…
Cancel
Save