diff --git a/build/service/shutdown.sh b/build/service/shutdown.sh index 6eb96a3..f36c684 100755 --- a/build/service/shutdown.sh +++ b/build/service/shutdown.sh @@ -11,5 +11,5 @@ then else kill -9 $EDASPID echo $EXE_PATH - echo 'Shutdown successfully.' + echo 'Shutdown tank successfully.' fi \ No newline at end of file diff --git a/build/service/startup.sh b/build/service/startup.sh index cc644ed..cea85b2 100755 --- a/build/service/startup.sh +++ b/build/service/startup.sh @@ -8,6 +8,7 @@ EXE_PATH=$TANK_DIR/tank if [ -f "$EXE_PATH" ]; then nohup $EXE_PATH >/dev/null 2>&1 & + echo 'Start tank successfully!' else echo 'Cannot find $EXE_PATH.' exit 1 diff --git a/rest/install.go b/rest/install.go index fc08484..855e23a 100644 --- a/rest/install.go +++ b/rest/install.go @@ -98,7 +98,7 @@ func InstallDatabase() { 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) if db.Error != nil { LogPanic(db.Error) @@ -118,6 +118,7 @@ func InstallDatabase() { user.Email = CONFIG.AdminEmail user.Phone = "" user.Gender = USER_GENDER_UNKNOWN + user.SizeLimit = -1 user.Status = USER_STATUS_OK db.Create(user) diff --git a/rest/matter_service.go b/rest/matter_service.go index 006ee27..626208b 100644 --- a/rest/matter_service.go +++ b/rest/matter_service.go @@ -92,6 +92,13 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string, written, err := io.Copy(distFile, file) 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) //如果有同名的文件,那么我们直接覆盖同名文件。 diff --git a/rest/user_controller.go b/rest/user_controller.go index e592667..14e0376 100644 --- a/rest/user_controller.go +++ b/rest/user_controller.go @@ -93,16 +93,16 @@ func (this *UserController) Create(writer http.ResponseWriter, request *http.Req username := request.FormValue("username") if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, username); !m { - return this.Error(`用户名必填,且只能包含字母,数字和'_''`) + panic(`用户名必填,且只能包含字母,数字和'_''`) } password := request.FormValue("password") if len(password) < 6 { - return this.Error(`密码长度至少为6位`) + panic(`密码长度至少为6位`) } email := request.FormValue("email") if email == "" { - return this.Error("邮箱必填!") + panic("邮箱必填!") } phone := request.FormValue("phone") @@ -110,24 +110,38 @@ func (this *UserController) Create(writer http.ResponseWriter, request *http.Req role := request.FormValue("role") 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 { - return this.Error(username + "已经被其他用户占用。") + panic(username + "已经被其他用户占用。") } //判断邮箱重名 if this.userDao.CountByEmail(email) > 0 { - return this.Error(email + "已经被其他用户占用。") + panic(email + "已经被其他用户占用。") } user := &User{ - Role: GetRole(role), - Username: username, - Password: GetBcrypt(password), - Email: email, - Phone: phone, - Gender: gender, - City: city, - Status: USER_STATUS_OK, + Role: GetRole(role), + Username: username, + Password: GetBcrypt(password), + Email: email, + Phone: phone, + Gender: gender, + City: city, + SizeLimit: sizeLimit, + Status: USER_STATUS_OK, } user = this.userDao.Create(user) @@ -145,13 +159,29 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque city := request.FormValue("city") 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 { return this.Error(RESULT_CODE_UNAUTHORIZED) } } - user := this.userDao.CheckByUuid(uuid) user.AvatarUrl = avatarUrl user.Phone = phone user.Gender = GetGender(gender) diff --git a/rest/user_model.go b/rest/user_model.go index aa0f725..0054745 100644 --- a/rest/user_model.go +++ b/rest/user_model.go @@ -36,6 +36,7 @@ type User struct { AvatarUrl string `json:"avatarUrl"` LastIp string `json:"lastIp"` LastTime time.Time `json:"lastTime"` + SizeLimit int64 `json:"sizeLimit"` Status string `json:"status"` } diff --git a/rest/util_string.go b/rest/util_string.go new file mode 100644 index 0000000..7e24796 --- /dev/null +++ b/rest/util_string.go @@ -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]) +}