func Download(url string) (string, error) { ext := strings.ToLower(path.Ext(url)) filename := helper.MD5(time.Now().String()) + ext ipath := "./file/" + "download/" //time.Now().Format("03/04/") if !helper.Exist(ipath) { os.MkdirAll(ipath, 0644) } path := ipath + filename if e := helper.GetFile(url, path, "default", "http://www.mzitu.com/"); e == nil { filehash, _ := helper.Filehash(path, nil) fname := helper.Encrypt_hash(filehash+"1", nil) if ext == "" { ext = ".jpg" } opath := "./file/" + time.Now().Format("03/04/") if !helper.Exist(opath) { os.MkdirAll(opath, 0644) } finalpath := opath + fname + ext fmt.Println("path:", path) fmt.Println("finalpath:", finalpath) if e := helper.MoveFile(path, finalpath); e == nil { return finalpath, e } } else { return "", errors.New("下载错误!") } return "", errors.New("Download函数出现错误!") }
func initData() { //用户等级划分:正数是普通用户,负数是管理员各种等级划分,为0则尚未注册 if usr, err := GetUserByRole(-1000); usr == nil && err == nil { if id, err := AddUser("*****@*****.**", "root", "root", "root", helper.Encrypt_hash("rootpass", nil), -1000); err == nil { fmt.Println("Default Email:[email protected] ,Username:root ,Password:rootpass Userid:", id) } else { fmt.Print("create root got errors:", err) } } fmt.Println("The Mzr system has started!") }
func (self *SignupHandler) Post() { self.TplNames = "signup.html" flash := beego.NewFlash() email := strings.ToLower(self.GetString("email")) username := strings.ToLower(self.GetString("username")) password := self.GetString("password") repassword := self.GetString("repassword") if password == "" { flash.Error("密码为空~") flash.Store(&self.Controller) return } if password != repassword { flash.Error("两次密码不匹配~") flash.Store(&self.Controller) return } if helper.CheckPassword(password) == false { flash.Error("密码含有非法字符或密码过短(至少4~30位密码)!") flash.Store(&self.Controller) return } if username == "" { flash.Error("用户名是为永久性设定,不能少于4个字或多于30个字,请慎重考虑,不能为空~") flash.Store(&self.Controller) return } if helper.CheckUsername(username) == false { flash.Error("用户名是为永久性设定,不能少于4个字或多于30个字,请慎重考虑,不能为空~") flash.Store(&self.Controller) return } if helper.CheckEmail(email) == false { flash.Error("Email格式不合符规格~") flash.Store(&self.Controller) return } if usrinfo, err := model.GetUserByEmail(email); usrinfo != nil { flash.Error("此账号不能使用~") flash.Store(&self.Controller) return } else if err != nil { flash.Error("检索账号期间出错~") flash.Store(&self.Controller) return } if usrid, err := model.AddUser(email, username, "", "", helper.Encrypt_hash(password, nil), 1); err != nil { flash.Error("用户注册信息写入数据库时发生错误~") flash.Store(&self.Controller) return } else { if usrinfo, err := model.GetUser(usrid); err == nil { //注册账号成功,以下自动登录并设置session self.SetSession("userid", usrid) self.SetSession("username", usrinfo.Username) self.SetSession("userrole", usrinfo.Role) self.SetSession("useremail", usrinfo.Email) self.SetSession("usercontent", usrinfo.Content) flash.Notice("账号登录成功~") flash.Store(&self.Controller) //session 写入后直接跳到首页 self.Redirect("/", 302) } else { flash.Notice("注册账号成功,请手动登录~") flash.Store(&self.Controller) //注册成功后直接跳转到登录页 self.Redirect("/signin/", 302) } } }
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 } } }
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 } }
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" } } }