2019-11-15 16:36:48 +08:00
|
|
|
|
package model
|
|
|
|
|
|
2019-11-20 15:53:00 +08:00
|
|
|
|
import (
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
)
|
2019-11-15 16:36:48 +08:00
|
|
|
|
|
|
|
|
|
// File 文件
|
|
|
|
|
type File struct {
|
|
|
|
|
// 表字段
|
|
|
|
|
gorm.Model
|
|
|
|
|
Name string
|
|
|
|
|
SourceName string
|
2019-11-24 11:33:30 +08:00
|
|
|
|
UserID uint `gorm:"index:user_id"`
|
2019-11-15 16:36:48 +08:00
|
|
|
|
Size uint64
|
|
|
|
|
PicInfo string
|
2019-11-24 11:33:30 +08:00
|
|
|
|
FolderID uint `gorm:"index:folder_id"`
|
2019-11-15 16:36:48 +08:00
|
|
|
|
PolicyID uint
|
|
|
|
|
Dir string `gorm:"size:65536"`
|
|
|
|
|
}
|
2019-11-19 17:20:59 +08:00
|
|
|
|
|
2019-11-20 15:24:26 +08:00
|
|
|
|
// Create 创建文件记录
|
|
|
|
|
func (file *File) Create() (uint, error) {
|
|
|
|
|
if err := DB.Create(file).Error; err != nil {
|
2019-11-20 15:53:00 +08:00
|
|
|
|
util.Log().Warning("无法插入文件记录, %s", err)
|
2019-11-20 15:24:26 +08:00
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return file.ID, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-19 17:20:59 +08:00
|
|
|
|
// GetFileByPathAndName 给定路径、文件名、用户ID,查找文件
|
|
|
|
|
func GetFileByPathAndName(path string, name string, uid uint) (File, error) {
|
|
|
|
|
var file File
|
2019-11-24 13:06:15 +08:00
|
|
|
|
result := DB.Where("user_id = ? AND dir = ? AND name=?", uid, path, name).First(&file)
|
2019-11-19 17:20:59 +08:00
|
|
|
|
return file, result.Error
|
|
|
|
|
}
|
2019-11-24 13:06:15 +08:00
|
|
|
|
|
2019-11-24 16:28:41 +08:00
|
|
|
|
// GetChildFile 查找目录下子文件
|
2019-11-24 13:06:15 +08:00
|
|
|
|
func (folder *Folder) GetChildFile() ([]File, error) {
|
|
|
|
|
var files []File
|
|
|
|
|
result := DB.Where("folder_id = ?", folder.ID).Find(&files)
|
|
|
|
|
return files, result.Error
|
|
|
|
|
}
|