Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
hexbee
Cloudreve
提交
20ea86ea
C
Cloudreve
项目概览
hexbee
/
Cloudreve
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Cloudreve
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
20ea86ea
编写于
11月 19, 2019
作者:
H
HFO4
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix: test failed due to `Policy.AutoName`
上级
88a543ef
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
49 addition
and
1 deletion
+49
-1
models/file.go
models/file.go
+7
-0
models/file_test.go
models/file_test.go
+18
-0
models/policy_test.go
models/policy_test.go
+3
-1
pkg/filesystem/filesystem.go
pkg/filesystem/filesystem.go
+11
-0
pkg/filesystem/hooks.go
pkg/filesystem/hooks.go
+10
-0
未找到文件。
models/file.go
浏览文件 @
20ea86ea
...
...
@@ -15,3 +15,10 @@ type File struct {
PolicyID
uint
Dir
string
`gorm:"size:65536"`
}
// GetFileByPathAndName 给定路径、文件名、用户ID,查找文件
func
GetFileByPathAndName
(
path
string
,
name
string
,
uid
uint
)
(
File
,
error
)
{
var
file
File
result
:=
DB
.
Where
(
"user_id = ? AND dir = ? AND name=?"
,
uid
,
path
,
name
)
.
Find
(
&
file
)
return
file
,
result
.
Error
}
models/file_test.go
0 → 100644
浏览文件 @
20ea86ea
package
model
import
(
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"testing"
)
func
TestGetFileByPathAndName
(
t
*
testing
.
T
)
{
asserts
:=
assert
.
New
(
t
)
fileRows
:=
sqlmock
.
NewRows
([]
string
{
"id"
,
"name"
})
.
AddRow
(
1
,
"1.cia"
)
mock
.
ExpectQuery
(
"SELECT(.+)"
)
.
WillReturnRows
(
fileRows
)
file
,
_
:=
GetFileByPathAndName
(
"/"
,
"1.cia"
,
1
)
asserts
.
Equal
(
"1.cia"
,
file
.
Name
)
asserts
.
NoError
(
mock
.
ExpectationsWereMet
())
}
models/policy_test.go
浏览文件 @
20ea86ea
...
...
@@ -76,7 +76,9 @@ func TestPolicy_GeneratePath(t *testing.T) {
func
TestPolicy_GenerateFileName
(
t
*
testing
.
T
)
{
asserts
:=
assert
.
New
(
t
)
testPolicy
:=
Policy
{}
testPolicy
:=
Policy
{
AutoRename
:
true
,
}
testPolicy
.
FileNameRule
=
"{randomkey16}"
asserts
.
Len
(
testPolicy
.
GenerateFileName
(
1
,
"123.txt"
),
16
)
...
...
pkg/filesystem/filesystem.go
浏览文件 @
20ea86ea
...
...
@@ -7,6 +7,7 @@ import (
"github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
"io"
"path"
"path/filepath"
)
...
...
@@ -165,3 +166,13 @@ func (fs *FileSystem) IsPathExist(path string) bool {
_
,
err
:=
model
.
GetFolderByPath
(
path
,
fs
.
User
.
ID
)
return
err
==
nil
}
// IsFileExist 返回给定路径的文件是否存在
func
(
fs
*
FileSystem
)
IsFileExist
(
fullPath
string
)
bool
{
basePath
:=
path
.
Dir
(
fullPath
)
fileName
:=
path
.
Base
(
fullPath
)
_
,
err
:=
model
.
GetFileByPathAndName
(
basePath
,
fileName
,
fs
.
User
.
ID
)
return
err
==
nil
}
pkg/filesystem/hooks.go
浏览文件 @
20ea86ea
...
...
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"github.com/HFO4/cloudreve/pkg/util"
"path"
)
// GenericBeforeUpload 通用上传前处理钩子,包含数据库操作
...
...
@@ -61,5 +62,14 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
if
!
fs
.
IsPathExist
(
virtualPath
)
{
return
errors
.
New
(
"路径
\"
"
+
virtualPath
+
"
\"
不存在"
)
}
// 检查文件是否存在
if
fs
.
IsFileExist
(
path
.
Join
(
virtualPath
,
ctx
.
Value
(
FileCtx
)
.
(
FileHeader
)
.
GetFileName
(),
))
{
return
errors
.
New
(
"同名文件已存在"
)
}
return
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录