mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: after uploading hooks and checks
This commit is contained in:
@@ -15,3 +15,10 @@ type Folder struct {
|
||||
// 关联模型
|
||||
OptionsSerialized PolicyOption `gorm:"-"`
|
||||
}
|
||||
|
||||
// GetFolderByPath 根据绝对路径和UID查找目录
|
||||
func GetFolderByPath(path string, uid uint) (Folder, error) {
|
||||
var folder Folder
|
||||
result := DB.Where("owner = ? AND position_absolute = ?", uid, path).Find(&folder)
|
||||
return folder, result.Error
|
||||
}
|
||||
|
||||
20
models/folder_test.go
Normal file
20
models/folder_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetFolderByPath(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
//policyRows := sqlmock.NewRows([]string{"id", "name"}).
|
||||
// AddRow(1, "默认上传策略")
|
||||
//mock.ExpectQuery("^SELECT (.+)").WillReturnRows(policyRows)
|
||||
|
||||
folder,_ := GetFolderByPath("/测试/test",1)
|
||||
fmt.Println(folder)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NoError(mock.)
|
||||
}
|
||||
@@ -85,6 +85,11 @@ func (policy *Policy) GeneratePath(uid uint) string {
|
||||
|
||||
// GenerateFileName 生成存储文件名
|
||||
func (policy *Policy) GenerateFileName(uid uint, origin string) string {
|
||||
// 未开启自动重命名时,直接返回原始文件名
|
||||
if !policy.AutoRename {
|
||||
return origin
|
||||
}
|
||||
|
||||
fileRule := policy.FileNameRule
|
||||
|
||||
replaceTable := map[string]string{
|
||||
|
||||
@@ -40,7 +40,7 @@ type User struct {
|
||||
Options string `json:"-",gorm:"size:4096"`
|
||||
|
||||
// 关联模型
|
||||
Group Group
|
||||
Group Group `gorm:"association_autoupdate:false"`
|
||||
Policy Policy `gorm:"PRELOAD:false,association_autoupdate:false"`
|
||||
|
||||
// 数据库忽略字段
|
||||
@@ -58,7 +58,7 @@ type UserOption struct {
|
||||
func (user *User) DeductionStorage(size uint64) bool {
|
||||
if size <= user.Storage {
|
||||
user.Storage -= size
|
||||
DB.Save(user)
|
||||
DB.Model(user).UpdateColumn("storage", gorm.Expr("storage - ?", size))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -68,7 +68,7 @@ func (user *User) DeductionStorage(size uint64) bool {
|
||||
func (user *User) IncreaseStorage(size uint64) bool {
|
||||
if size <= user.GetRemainingCapacity() {
|
||||
user.Storage += size
|
||||
DB.Save(user)
|
||||
DB.Model(user).UpdateColumn("storage", gorm.Expr("storage + ?", size))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user