Example #1
0
func (self *Settings) Avatar() {

	flash := beego.NewFlash()
	self.TplNames = "settings.html"

	targetFolder := "/file/"
	file, handler, e := self.GetFile("avatar")
	uid := self.GetSession("userid").(int64)

	if e != nil {
		flash.Error("SettingsHandler获取文件错误1," + fmt.Sprint(e))
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	} else {

		if handler != nil {

			ext := strings.ToLower(path.Ext(handler.Filename))
			filename := helper.MD5(time.Now().String()) + ext

			ipath := targetFolder + time.Now().Format("03/04/")
			//ipath := targetFolder + helper.FixedpathByNumber(2, 2)
			os.MkdirAll("."+ipath, 0644)
			path := ipath + filename
			f, err := os.OpenFile("."+path, os.O_WRONLY|os.O_CREATE, 0644)

			if err != nil {
				flash.Error("SettingsHandler获取文件错误2!")
				flash.Store(&self.Controller)

				self.Redirect("/settings/", 302)
				return
			} else {
				io.Copy(f, file)
				defer file.Close()
				defer f.Close()
				input_file := "." + path
				output_file := "." + path
				output_size := "72x72"
				output_align := "center"
				background := "#f0f0f0"
				newpath := ""
				//所有上传的图片都会被缩略处理
				if err := helper.Thumbnail("crop", input_file, output_file, output_size, output_align, background); err != nil {

					flash.Error(fmt.Sprint(err))
					flash.Store(&self.Controller)

					if e := os.Remove(path); e != nil {
						fmt.Println("SettingsHandler清除残余文件出错:", e)
					}

					self.Redirect("/settings/", 302)
					return
				} else {
					f.Close() //手动关闭  不然下面会导致重命名文件出错~

					//所有文件以该加密方式哈希生成文件名  从而实现针对到用户个体的文件权限识别
					filehash, _ := helper.Filehash(helper.Url2local(path), nil)

					fname := helper.Encrypt_hash(filehash+strconv.Itoa(int(uid)), nil)

					newpath = ipath + fname + ext

					if err := os.Rename(helper.Url2local(path), helper.Url2local(newpath)); err != nil {
						fmt.Println("重命名文件出错:", err)
					}

					//文件权限校验 通过说明文件上传转换过程中没发生错误
					//首先读取被操作文件的hash值 和 用户请求中的文件hash值  以及 用户当前id的string类型  进行验证

					if fhashed, _ := helper.Filehash(helper.Url2local(newpath), nil); helper.Validate_hash(fname, fhashed+strconv.Itoa(int(uid))) {

						//收到的头像图片存储都设置ctype为 10 与其他图片类型区分开
						if _, err := model.AddImage(helper.Url2local(newpath), 0, 10, uid); err != nil {
							fmt.Print("model.AddImage:", err)
						}

						usr, _ := model.GetUser(uid)
						if usr.Avatar != "" {
							os.Remove(helper.Url2local(usr.Avatar))
						}
						if usr.AvatarLarge != "" {
							os.Remove(helper.Url2local(usr.AvatarLarge))
						}
						if usr.AvatarMedium != "" {
							os.Remove(helper.Url2local(usr.AvatarMedium))
						}
						if usr.AvatarSmall != "" {
							os.Remove(helper.Url2local(usr.AvatarSmall))
						}
						usr.Avatar = newpath
						usr.AvatarLarge = newpath
						usr.AvatarMedium = newpath
						usr.AvatarSmall = newpath
						model.PutUser(uid, usr)

						//hash, _ := utils.Filehash(output_file)
						//fileInfo, err := os.Stat(output_file)
						//var fsize int64 = 0
						//if err == nil {
						//	fsize = fileInfo.Size() / 1024
						//}

						flash.Notice("成功设置头像为:", handler.Filename)
						flash.Store(&self.Controller)
						self.Redirect("/settings/", 302)
						return
					} else {

						flash.Error("SettingsHandler图片添加水印失败!")
						flash.Store(&self.Controller)

						if e := os.Remove(helper.Url2local(newpath)); e != nil {
							fmt.Println("SettingsHandler清除错误水印遗留文件", newpath, "出错:", e)
						}

						self.Redirect("/settings/", 302)
						return
					}

				}

			}

		} else {

			flash.Error("SettingsHandler获取文件错误3!")
			flash.Store(&self.Controller)

			self.Redirect("/settings/", 302)
			return
		}
	}
}
Example #2
0
func (self *UploaderHandler) Post() {

	flash := beego.NewFlash()
	targetFolder := "/file/"
	self.TplNames = "editor-tinymce-ajax-result.html"
	file, handler, e := self.GetFile("userfile")

	uid := int64(0)
	if self.GetSession("userid") != nil {
		uid = self.GetSession("userid").(int64)
	} else {
		flash.Error("UploaderHandler获取UID错误0!")
		flash.Store(&self.Controller)

		self.Data["result"] = "UploaderHandler获取UID错误0!"
		self.Data["resultcode"] = "failed"
		return
	}

	if e != nil {
		fmt.Println("UploaderHandler获取文件错误1!")
		flash.Error("UploaderHandler获取文件错误1!")
		flash.Store(&self.Controller)

		self.TplNames = "editor-tinymce-ajax-result.html"
		self.Data["result"] = " "
		self.Data["resultcode"] = "failed"
	} else {

		if handler != nil {

			ext := strings.ToLower(path.Ext(handler.Filename))
			filename := helper.MD5(time.Now().String()) + ext

			ipath := targetFolder + time.Now().Format("03/04/")
			//ipath := targetFolder + helper.FixedpathByNumber(2, 2)
			os.MkdirAll("."+ipath, 0644)
			path := ipath + filename
			f, err := os.OpenFile("."+path, os.O_WRONLY|os.O_CREATE, 0644)

			if err != nil {

				fmt.Println("UploaderHandler获取文件错误2!")
				flash.Error("UploaderHandler获取文件错误2!")
				flash.Store(&self.Controller)

				self.TplNames = "editor-tinymce-ajax-result.html"
				self.Data["result"] = " "
				self.Data["resultcode"] = "failed"
			} else {
				io.Copy(f, file)
				defer file.Close()
				defer f.Close()
				input_file := "." + path
				output_file := "." + path
				output_size := "696"
				output_align := "center"
				background := "white"
				newpath := ""
				//所有上传的图片都会被缩略处理
				if err := helper.Thumbnail("resize", input_file, output_file, output_size, output_align, background); err != nil {

					fmt.Println("UploaderHandler生成缩略图出错:", err)
					flash.Error(fmt.Sprint(err))
					flash.Store(&self.Controller)

					if e := os.Remove(helper.Url2local(path)); e != nil {
						fmt.Println("UploaderHandler清除残余文件出错:", e)
					}
					self.TplNames = "editor-tinymce-ajax-result.html"
					self.Data["result"] = err
					self.Data["resultcode"] = "failed"
				} else {
					f.Close() //手动关闭  不然下面会导致重命名文件出错~
					watermark_file := helper.Url2local(helper.GetTheme()) + "/static/mzr/img/watermark.png"

					if e := helper.Watermark(watermark_file, input_file, output_file, "SouthEast"); e == nil {
						//所有文件以该加密方式哈希生成文件名  从而实现针对到用户个体的文件权限识别
						filehash, _ := helper.Filehash(helper.Url2local(path), nil)

						fname := helper.Encrypt_hash(filehash+strconv.Itoa(int(uid)), nil)

						newpath = ipath + fname + ext

						if err := os.Rename(helper.Url2local(path), helper.Url2local(newpath)); err != nil {
							fmt.Println("重命名文件出错:", err)
						}

						//文件权限校验 通过说明文件上传转换过程中没发生错误
						//首先读取被操作文件的hash值 和 用户请求中的文件hash值  以及 用户当前id的string类型  进行验证

						if fhashed, _ := helper.Filehash(helper.Url2local(newpath), nil); helper.Validate_hash(fname, fhashed+strconv.Itoa(int(uid))) {

							//用户上传图片的记录
							//ctype为0表示没上传文件
							//收到的图片存储都设置ctype为 -1  证明用户上传了文件,但尚未正式使用
							//当用户edit话题或new话题,在进行posting的时候,检查image表,如存在同样文件,则顺手修改ctype为1 表示该文件正在使用
							//并修改相关tid uid等等信息进image表 留待以后或许有用~
							if _, err := model.AddImage(helper.Url2local(newpath), 0, -1, uid); err != nil {
								fmt.Print("model.AddImage:", err)
							}

							self.TplNames = "editor-tinymce-ajax-result.html"
							self.Data["result"] = "file_uploaded"
							self.Data["resultcode"] = "ok"
							self.Data["file_name"] = newpath
						} else {

							fmt.Println("UploaderHandler校验图片不正确!")
							flash.Error("UploaderHandler校验图片不正确!")
							flash.Store(&self.Controller)

							self.TplNames = "editor-tinymce-ajax-result.html"
							self.Data["result"] = " "
							self.Data["resultcode"] = "failed"

							if e := os.Remove(helper.Url2local(newpath)); e != nil {
								fmt.Println("UploaderHandler清除错误文件", newpath, "出错:", e)
							}
						}

						//hash, _ := utils.Filehash(output_file)
						//fileInfo, err := os.Stat(output_file)
						//var fsize int64 = 0
						//if err == nil {
						//	fsize = fileInfo.Size() / 1024
						//}
					} else {
						fmt.Println("UploaderHandler 图片添加水印失败!")
						flash.Error("UploaderHandler 图片添加水印失败!")
						flash.Store(&self.Controller)

						self.TplNames = "editor-tinymce-ajax-result.html"
						self.Data["result"] = " "
						self.Data["resultcode"] = "failed"

						if e := os.Remove(helper.Url2local(newpath)); e != nil {
							fmt.Println("UploaderHandler清除错误水印遗留文件", newpath, "出错:", e)
						}
					}

				}

			}

		} else {

			fmt.Println("UploaderHandler获取文件错误3!")
			flash.Error("UploaderHandler获取文件错误3!")
			flash.Store(&self.Controller)

			self.TplNames = "editor-tinymce-ajax-result.html"
			self.Data["result"] = " "
			self.Data["resultcode"] = "failed"
		}
	}

}
Example #3
0
func (self *Settings) Password() {

	flash := beego.NewFlash()
	self.TplNames = "settings.html"

	sess_userid, _ := self.GetSession("userid").(int64)
	curpass := self.GetString("curpass")
	newpassword := self.GetString("password")
	newrepassword := self.GetString("repassword")

	if curpass == "" {
		flash.Error("当前密码不能为空!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if newpassword == "" {
		flash.Error("设置密码不能为空!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if newrepassword == "" {
		flash.Error("重验设置密码不能为空!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if newpassword != newrepassword {
		flash.Error("两次密码不一致!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if helper.CheckPassword(curpass) == false {
		flash.Error("当前密码含有非法字符或当前密码过短(至少4~30位密码)!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return

	}

	if helper.CheckPassword(newpassword) == false {
		flash.Error("设置密码含有非法字符或设置密码过短(至少4~30位密码)!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return

	}

	if usrinfo, err := model.GetUser(sess_userid); usrinfo != nil && err == nil {

		if helper.Validate_hash(usrinfo.Password, curpass) {
			usrinfo.Password = helper.Encrypt_hash(newpassword, nil)

			if _, err := model.PutUser(usrinfo.Id, usrinfo); err == nil {
				flash.Notice("设置密码成功~")
			} else {
				flash.Error("设置密码失败~")
			}
			flash.Store(&self.Controller)

			self.Redirect("/settings/", 302)
			return
		} else {

			flash.Error("密码无法通过校验~")
			flash.Store(&self.Controller)

			self.Redirect("/settings/", 302)
			return
		}
	} else {

		flash.Error("该账号不存在~")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

}
Example #4
0
func (self *SigninHandler) Post() {
	self.TplNames = "signin.html"

	flash := beego.NewFlash()
	email := self.GetString("email")
	password := self.GetString("password")
	remember := self.GetString("remember")
	rememberbyform := self.GetString("remember")

	if email == "" {
		flash.Error("EMAIL为空~")
		flash.Store(&self.Controller)

		return

	}

	if password == "" {
		flash.Error("密码为空~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckEmail(email) == false {
		flash.Error("Email格式不合符规格~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckPassword(password) == false {
		flash.Error("密码含有非法字符或密码过短(至少4~30位密码)!")
		flash.Store(&self.Controller)

		return

	}

	if usrinfo, err := model.GetUserByEmail(email); usrinfo != nil && err == nil {

		if helper.Validate_hash(usrinfo.Password, password) {

			//登录成功设置session
			self.SetSession("userid", usrinfo.Id)
			self.SetSession("username", usrinfo.Username)
			self.SetSession("userrole", usrinfo.Role)
			self.SetSession("useremail", usrinfo.Email)
			self.SetSession("usercontent", usrinfo.Content)

			//设置cookie
			if remember == "on" && rememberbyform == "on" {

				self.Ctx.SetCookie("remember", "on", 31536000, "/")
			} else {

				self.Ctx.SetCookie("remember", "off", 31536000, "/")
			}
			self.Redirect("/", 302)
		} else {

			flash.Error("密码无法通过校验~")
			flash.Store(&self.Controller)
			return
		}
	} else {

		flash.Error("该账号不存在~")
		flash.Store(&self.Controller)
		return
	}
}