Fix: able to upload empty file

pull/247/head
HFO4 5 years ago
parent 948059ec1c
commit 671d031d00

@ -12,7 +12,7 @@ import (
func TestFileSystem_AddFile(t *testing.T) {
asserts := assert.New(t)
file := local.FileData{
file := local.FileStream{
Size: 5,
Name: "1.txt",
}

@ -14,7 +14,7 @@ import (
func TestGenericBeforeUpload(t *testing.T) {
asserts := assert.New(t)
file := local.FileData{
file := local.FileStream{
Size: 5,
Name: "1.txt",
}

@ -28,12 +28,18 @@ func (fs *FileSystem) ValidateLegalName(ctx context.Context, name string) bool {
if len(name) >= 256 {
return false
}
// 是否为空限制
if len(name) == 0 {
return false
}
return true
}
// ValidateFileSize 验证上传的文件大小是否超出限制
func (fs *FileSystem) ValidateFileSize(ctx context.Context, size uint64) bool {
return size <= fs.User.Policy.MaxSize
return size <= fs.User.Policy.MaxSize && size != 0
}
// ValidateCapacity 验证并扣除用户容量

@ -36,6 +36,7 @@ func TestFileSystem_ValidateLegalName(t *testing.T) {
asserts.False(fs.ValidateLegalName(ctx, "../11.txt"))
asserts.False(fs.ValidateLegalName(ctx, "/11.txt"))
asserts.False(fs.ValidateLegalName(ctx, "\\11.txt"))
asserts.False(fs.ValidateLegalName(ctx, ""))
}
func TestFileSystem_ValidateCapacity(t *testing.T) {
@ -72,6 +73,7 @@ func TestFileSystem_ValidateFileSize(t *testing.T) {
asserts.True(fs.ValidateFileSize(ctx, 5))
asserts.True(fs.ValidateFileSize(ctx, 10))
asserts.False(fs.ValidateFileSize(ctx, 11))
asserts.False(fs.ValidateFileSize(ctx, 0))
}
func TestFileSystem_ValidateExtension(t *testing.T) {

@ -0,0 +1,26 @@
package serializer
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCheckSettingValue(t *testing.T) {
asserts := assert.New(t)
asserts.Equal("", checkSettingValue(map[string]string{}, "key"))
asserts.Equal("123", checkSettingValue(map[string]string{"key": "123"}, "key"))
}
func TestBuildSiteConfig(t *testing.T) {
asserts := assert.New(t)
res := BuildSiteConfig(map[string]string{"not exist": ""})
asserts.Equal("", res.Data.(SiteConfig).SiteName)
res = BuildSiteConfig(map[string]string{"siteName": "123"})
asserts.Equal("123", res.Data.(SiteConfig).SiteName)
res = BuildSiteConfig(map[string]string{"qq_login": "1"})
asserts.Equal(true, res.Data.(SiteConfig).QQLogin)
}

@ -50,7 +50,7 @@ func InitRouter() *gin.Engine {
auth := v3.Group("")
auth.Use(middleware.AuthRequired())
{
// 用户
// 用户
user := auth.Group("User")
{
// 当前登录用户信息
@ -64,6 +64,13 @@ func InitRouter() *gin.Engine {
file.POST("Upload", controllers.FileUploadStream)
}
// 目录
directory := auth.Group("Directory")
{
// 文件上传
directory.PUT("", controllers.Ping)
}
}
}

Loading…
Cancel
Save