Add a new feature of limitSize.

This commit is contained in:
zicla 2017-12-29 18:44:10 +08:00
parent 0720ae5ed6
commit 68c7da1f58
7 changed files with 93 additions and 17 deletions

View File

@ -11,5 +11,5 @@ then
else else
kill -9 $EDASPID kill -9 $EDASPID
echo $EXE_PATH echo $EXE_PATH
echo 'Shutdown successfully.' echo 'Shutdown tank successfully.'
fi fi

View File

@ -8,6 +8,7 @@ EXE_PATH=$TANK_DIR/tank
if [ -f "$EXE_PATH" ]; then if [ -f "$EXE_PATH" ]; then
nohup $EXE_PATH >/dev/null 2>&1 & nohup $EXE_PATH >/dev/null 2>&1 &
echo 'Start tank successfully!'
else else
echo 'Cannot find $EXE_PATH.' echo 'Cannot find $EXE_PATH.'
exit 1 exit 1

View File

@ -98,7 +98,7 @@ func InstallDatabase() {
LogPanic("超级管理员邮箱必填!") LogPanic("超级管理员邮箱必填!")
} }
createUser := "CREATE TABLE `tank10_user` (`uuid` char(36) NOT NULL,`role` varchar(45) DEFAULT 'USER',`username` varchar(255) DEFAULT NULL COMMENT '昵称',`password` varchar(255) DEFAULT NULL COMMENT '密码',`email` varchar(45) DEFAULT NULL COMMENT '邮箱',`phone` varchar(45) DEFAULT NULL COMMENT '电话',`gender` varchar(45) DEFAULT 'UNKNOWN' COMMENT '性别,默认未知',`city` varchar(45) DEFAULT NULL COMMENT '城市',`avatar_url` varchar(255) DEFAULT NULL COMMENT '头像链接',`last_time` datetime DEFAULT NULL COMMENT '上次登录使劲按',`last_ip` varchar(45) DEFAULT NULL,`status` varchar(45) DEFAULT 'OK',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表描述';" createUser := "CREATE TABLE `tank10_user` (`uuid` char(36) NOT NULL,`role` varchar(45) DEFAULT 'USER',`username` varchar(255) DEFAULT NULL COMMENT '昵称',`password` varchar(255) DEFAULT NULL COMMENT '密码',`email` varchar(45) DEFAULT NULL COMMENT '邮箱',`phone` varchar(45) DEFAULT NULL COMMENT '电话',`gender` varchar(45) DEFAULT 'UNKNOWN' COMMENT '性别,默认未知',`city` varchar(45) DEFAULT NULL COMMENT '城市',`avatar_url` varchar(255) DEFAULT NULL COMMENT '头像链接',`last_time` datetime DEFAULT NULL COMMENT '上次登录使劲按',`last_ip` varchar(45) DEFAULT NULL,`size_limit` int(11) DEFAULT '-1' COMMENT '该账号上传文件的大小限制单位byte。<0 表示不设限制',`status` varchar(45) DEFAULT 'OK',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表描述';"
db = db.Exec(createUser) db = db.Exec(createUser)
if db.Error != nil { if db.Error != nil {
LogPanic(db.Error) LogPanic(db.Error)
@ -118,6 +118,7 @@ func InstallDatabase() {
user.Email = CONFIG.AdminEmail user.Email = CONFIG.AdminEmail
user.Phone = "" user.Phone = ""
user.Gender = USER_GENDER_UNKNOWN user.Gender = USER_GENDER_UNKNOWN
user.SizeLimit = -1
user.Status = USER_STATUS_OK user.Status = USER_STATUS_OK
db.Create(user) db.Create(user)

View File

@ -92,6 +92,13 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string,
written, err := io.Copy(distFile, file) written, err := io.Copy(distFile, file)
this.PanicError(err) this.PanicError(err)
//判断用户自身上传大小的限制。
if user.SizeLimit >= 0 {
if written > user.SizeLimit {
panic("您最大只能上传" + HumanFileSize(user.SizeLimit, false) + "的文件")
}
}
//查找文件夹下面是否有同名文件。 //查找文件夹下面是否有同名文件。
matters := this.matterDao.ListByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, false, filename) matters := this.matterDao.ListByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, false, filename)
//如果有同名的文件,那么我们直接覆盖同名文件。 //如果有同名的文件,那么我们直接覆盖同名文件。

View File

@ -93,16 +93,16 @@ func (this *UserController) Create(writer http.ResponseWriter, request *http.Req
username := request.FormValue("username") username := request.FormValue("username")
if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, username); !m { if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, username); !m {
return this.Error(`用户名必填,且只能包含字母,数字和'_''`) panic(`用户名必填,且只能包含字母,数字和'_''`)
} }
password := request.FormValue("password") password := request.FormValue("password")
if len(password) < 6 { if len(password) < 6 {
return this.Error(`密码长度至少为6位`) panic(`密码长度至少为6位`)
} }
email := request.FormValue("email") email := request.FormValue("email")
if email == "" { if email == "" {
return this.Error("邮箱必填!") panic("邮箱必填!")
} }
phone := request.FormValue("phone") phone := request.FormValue("phone")
@ -110,24 +110,38 @@ func (this *UserController) Create(writer http.ResponseWriter, request *http.Req
role := request.FormValue("role") role := request.FormValue("role")
city := request.FormValue("city") city := request.FormValue("city")
//判断用户上传大小限制。
sizeLimitStr := request.FormValue("sizeLimit")
var sizeLimit int64 = 0
if sizeLimitStr == "" {
panic("用户上传限制必填!")
} else {
intsizeLimit, err := strconv.Atoi(sizeLimitStr)
if err != nil {
this.PanicError(err)
}
sizeLimit = int64(intsizeLimit)
}
//判断重名。 //判断重名。
if this.userDao.CountByUsername(username) > 0 { if this.userDao.CountByUsername(username) > 0 {
return this.Error(username + "已经被其他用户占用。") panic(username + "已经被其他用户占用。")
} }
//判断邮箱重名 //判断邮箱重名
if this.userDao.CountByEmail(email) > 0 { if this.userDao.CountByEmail(email) > 0 {
return this.Error(email + "已经被其他用户占用。") panic(email + "已经被其他用户占用。")
} }
user := &User{ user := &User{
Role: GetRole(role), Role: GetRole(role),
Username: username, Username: username,
Password: GetBcrypt(password), Password: GetBcrypt(password),
Email: email, Email: email,
Phone: phone, Phone: phone,
Gender: gender, Gender: gender,
City: city, City: city,
Status: USER_STATUS_OK, SizeLimit: sizeLimit,
Status: USER_STATUS_OK,
} }
user = this.userDao.Create(user) user = this.userDao.Create(user)
@ -145,13 +159,29 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque
city := request.FormValue("city") city := request.FormValue("city")
currentUser := this.checkUser(writer, request) currentUser := this.checkUser(writer, request)
if currentUser.Role != USER_ROLE_ADMINISTRATOR { user := this.userDao.CheckByUuid(uuid)
if currentUser.Role == USER_ROLE_ADMINISTRATOR {
//只有管理员可以改变用户上传的大小
//判断用户上传大小限制。
sizeLimitStr := request.FormValue("sizeLimit")
var sizeLimit int64 = 0
if sizeLimitStr == "" {
panic("用户上传限制必填!")
} else {
intsizeLimit, err := strconv.Atoi(sizeLimitStr)
if err != nil {
this.PanicError(err)
}
sizeLimit = int64(intsizeLimit)
}
user.SizeLimit = sizeLimit
} else {
if currentUser.Uuid != uuid { if currentUser.Uuid != uuid {
return this.Error(RESULT_CODE_UNAUTHORIZED) return this.Error(RESULT_CODE_UNAUTHORIZED)
} }
} }
user := this.userDao.CheckByUuid(uuid)
user.AvatarUrl = avatarUrl user.AvatarUrl = avatarUrl
user.Phone = phone user.Phone = phone
user.Gender = GetGender(gender) user.Gender = GetGender(gender)

View File

@ -36,6 +36,7 @@ type User struct {
AvatarUrl string `json:"avatarUrl"` AvatarUrl string `json:"avatarUrl"`
LastIp string `json:"lastIp"` LastIp string `json:"lastIp"`
LastTime time.Time `json:"lastTime"` LastTime time.Time `json:"lastTime"`
SizeLimit int64 `json:"sizeLimit"`
Status string `json:"status"` Status string `json:"status"`
} }

36
rest/util_string.go Normal file
View File

@ -0,0 +1,36 @@
package rest
import (
"fmt"
"strconv"
)
//把一个大小转变成方便读的格式
//human readable file size
func HumanFileSize(bytes int64, si bool) string {
var thresh int64 = 1000
if si {
thresh = 1024
}
if bytes < 0 {
bytes = 0
}
if bytes < thresh {
return fmt.Sprintf("%dB", bytes)
}
var units = []string{"kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
if si {
units = []string{"KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
}
var u = 0
var tmp = float64(bytes)
var standard = float64(thresh)
for tmp >= standard && u < len(units)-1 {
tmp /= float64(standard)
u++
}
numStr := strconv.FormatFloat(tmp, 'f', 1, 64)
return fmt.Sprintf("%s%s", numStr, units[u])
}