-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"github.com/Xhofe/alist/conf" | ||
"github.com/Xhofe/alist/server/models" | ||
"github.com/Xhofe/alist/utils" | ||
log "github.com/sirupsen/logrus" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func InitModel() bool { | ||
log.Infof("初始化数据库...") | ||
switch conf.Conf.Database.Type { | ||
case "sqlite3":{ | ||
needMigrate := !utils.Exists(conf.Conf.Database.DBFile) | ||
db, err := gorm.Open(sqlite.Open(conf.Conf.Database.DBFile), &gorm.Config{}) | ||
if err!=nil { | ||
log.Errorf("连接数据库出现错误:%s", err.Error()) | ||
return false | ||
} | ||
conf.DB = db | ||
if needMigrate { | ||
log.Infof("迁移数据库...") | ||
err = conf.DB.AutoMigrate(&models.File{}) | ||
if err !=nil { | ||
log.Errorf("数据库迁移失败:%s",err.Error()) | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
default: | ||
log.Errorf("不支持的数据库类型:%s", conf.Conf.Database.Type) | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Xhofe/alist/conf" | ||
"time" | ||
) | ||
|
||
type File struct { | ||
ParentPath string `json:"parent_path" gorm:"index"` | ||
FileExtension string `json:"file_extension"` | ||
FileId string `json:"file_id"` | ||
Name string `json:"name" gorm:"index"` | ||
Type string `json:"type"` | ||
UpdatedAt *time.Time `json:"updated_at"` | ||
Category string `json:"category"` | ||
ContentType string `json:"content_type"` | ||
Size int64 `json:"size"` | ||
} | ||
|
||
func (file *File) Create() error { | ||
return conf.DB.Create(file).Error | ||
} | ||
|
||
func GetFilesByParentPath(parentPath string) (*[]File,error) { | ||
var files []File | ||
if err := conf.DB.Where("parent_path = ?", parentPath).Find(&files).Error; err != nil { | ||
return nil,err | ||
} | ||
return &files, nil | ||
} | ||
|
||
func SearchByNameGlobal(keyword string) (*[]File, error) { | ||
var files []File | ||
if err := conf.DB.Where("name LIKE ?", fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil { | ||
return nil,err | ||
} | ||
return &files, nil | ||
} | ||
|
||
func SearchByNameInPath(keyword string, parentPath string) (*[]File, error) { | ||
var files []File | ||
if err := conf.DB.Where("parent_path LIKE ? AND name LIKE ?",fmt.Sprintf("%s%%", parentPath), fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil { | ||
return nil,err | ||
} | ||
return &files, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// copy interface val | ||
func SimpleCopyProperties(dst, src interface{}) (err error) { | ||
// 防止意外panic | ||
defer func() { | ||
if e := recover(); e != nil { | ||
err = fmt.Errorf("%v", e) | ||
} | ||
}() | ||
|
||
dstType, dstValue := reflect.TypeOf(dst), reflect.ValueOf(dst) | ||
srcType, srcValue := reflect.TypeOf(src), reflect.ValueOf(src) | ||
|
||
// dst必须结构体指针类型 | ||
if dstType.Kind() != reflect.Ptr || dstType.Elem().Kind() != reflect.Struct { | ||
return errors.New("dst type should be a struct pointer") | ||
} | ||
|
||
// src必须为结构体或者结构体指针,.Elem()类似于*ptr的操作返回指针指向的地址反射类型 | ||
if srcType.Kind() == reflect.Ptr { | ||
srcType, srcValue = srcType.Elem(), srcValue.Elem() | ||
} | ||
if srcType.Kind() != reflect.Struct { | ||
return errors.New("src type should be a struct or a struct pointer") | ||
} | ||
|
||
// 取具体内容 | ||
dstType, dstValue = dstType.Elem(), dstValue.Elem() | ||
|
||
// 属性个数 | ||
propertyNums := dstType.NumField() | ||
|
||
for i := 0; i < propertyNums; i++ { | ||
// 属性 | ||
property := dstType.Field(i) | ||
// 待填充属性值 | ||
propertyValue := srcValue.FieldByName(property.Name) | ||
|
||
// 无效,说明src没有这个属性 || 属性同名但类型不同 | ||
if !propertyValue.IsValid() || property.Type != propertyValue.Type() { | ||
continue | ||
} | ||
|
||
if dstValue.Field(i).CanSet() { | ||
dstValue.Field(i).Set(propertyValue) | ||
} | ||
} | ||
|
||
return nil | ||
} |