func SetRecordforImageOnPost(tid int64, uid int64) { if tc, err := GetTopic(tid); err == nil { tpfiles, imgslist, delfiles := []string{}, []string{}, []string{} //获取成功发布后话题的本地图片集合:tpfiles if imgs, num := helper.GetImages(tc.Content); num > 0 { //记录已使用的图片集 for _, v := range imgs { if helper.IsLocal(v) { tpfiles = append(tpfiles, v) //已使用的本地图片集合 //再把已使用的图片到image表中进行对比,把表中ctype改为1标识为已使用 if e := SetImageByLocationWithUid(v, uid, tid, 1); e != nil { fmt.Println("model.SetImageCtypeByLocaltion出现错误:", e) } } } } //获取image表中同一用户ctype为-1的图片集合:imgslist if imgs2, err := GetImagesByCtypeWithUid(-1, uid); err == nil { if len(*imgs2) > 0 { for _, v := range *imgs2 { imgslist = append(imgslist, v.Location) } } } delfiles = helper.DifferenceSets(imgslist, tpfiles) //用标识为-1的图集减去正式使用的图集等于没使用的图集 for _, v := range delfiles { if helper.Exist(helper.Url2local(v)) { if e := os.Remove(helper.Url2local(v)); e != nil { fmt.Println("删除未使用文件", v, "时候出现错误:", e) } } //如果本地同时也存在banner缓存文件,则一同删除 if p := helper.Url2local(helper.SetSuffix(v, "_banner.jpg")); helper.Exist(p) { if e := os.Remove(p); e != nil { fmt.Println("SetRecordforImageOnEdit删除未使用文件之banner", p, "时出现错误:", e) } } //删除image表中已经被删除文件的记录 if e := DelImageByLocation(v); e != nil { fmt.Println("删除未使用文件", v, "的数据记录时候出现错误:", e) } } } }
func AddImage(path string, pid int64, ctype int64, uid int64) (int64, error) { fg, err := helper.GetImagePha(helper.Url2local(path)) if err != nil { return -1, err } img := new(Image) img.Ctype = ctype img.Uid = uid img.Created = time.Now() img.Location = helper.Local2url(path) img.Fingerprint = fg img.Pid = pid return Engine.Insert(img) }
func main() { urls := []string{} //循环所有页面查找所有图片的网页链接 for i := 1; i <= 50; i++ { x, _ := goquery.NewDocument("http://www.mzitu.com/page/" + strconv.Itoa(i)) x.Find(".imageLink").Each(func(idx int, s *goquery.Selection) { v, b := s.Attr("href") if b == true { urls = append(urls, v) } }) } //遍历所有图片网址 提取图片URL后保存到数据库 for k, v := range urls { fmt.Println("<url #[", k, "]# url>") SelfPage(v) //单独处理网页 } //读取图片集合并下载 if imgs, e := GetSpiderData(0, 0, "id"); e == nil { j := int64(0) for k, v := range *imgs { fmt.Println("#>", k, ":", v.Url) if fpath, err := Download(v.Url); err == nil { fmt.Println("fpath:", fpath) fmt.Println(helper.Local2url(fpath)) if helper.Exist(fpath) { if thumbnails, thumbnailslarge, thumbnailsmedium, thumbnailssmall, e := helper.MakeThumbnails(helper.Local2url(fpath)); thumbnails != "" && thumbnailslarge != "" && thumbnailsmedium != "" && thumbnailssmall != "" && e == nil { j += 1 title := "性感系 " + v.Title + " 美女季/第" + fmt.Sprint(time.Now().Format("0102-150405")) + "期" cid := int64(1) nid := int64(4) //2 uid := int64(1) role := int64(-1000) if 1899 < j <= 3541 { model.DelTopic(j, uid, role) id, err := UpdateTopic(j, title, "<p><img src=\""+helper.Local2url(fpath)+"\" alt=\""+v.Title+"\"/></p>", thumbnails, thumbnailslarge, thumbnailsmedium, thumbnailssmall, cid, nid, uid) if err != nil { fmt.Println("###################发布话题", id, "出错####################", err) } } else { id, err := AddTopic(title, "<p><img src=\""+helper.Local2url(fpath)+"\" alt=\""+v.Title+"\"/></p>", thumbnails, thumbnailslarge, thumbnailsmedium, thumbnailssmall, cid, nid, uid) if err != nil { fmt.Println("###################发布话题", id, "出错####################", err) } } } else { fmt.Println("@@@@@@@@@@@@@@@处理缩略图出错@@@@@@@@@@@@@@@@", err) os.Remove(fpath) os.Remove(helper.Url2local(helper.SetSuffix(fpath, "_large.jpg"))) os.Remove(helper.Url2local(helper.SetSuffix(fpath, "_medium.jpg"))) os.Remove(helper.Url2local(helper.SetSuffix(fpath, "_small.jpg"))) } } } } } }
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 DelTopic(id int64, uid int64, role int64) error { allow := false if role < 0 { allow = true } topic := new(Topic) if has, err := Engine.Id(id).Get(topic); has == true && err == nil { if topic.Uid == uid || allow { //检查附件字段并尝试删除文件 if topic.Attachment != "" { if p := helper.Url2local(topic.Attachment); helper.Exist(p) { //验证是否管理员权限 if allow { if err := os.Remove(p); err != nil { //可以输出错误,但不要反回错误,以免陷入死循环无法删掉 fmt.Println("ROOT DEL TOPIC Attachment, TOPIC ID:", id, ",ERR:", err) } } else { //检查用户对文件的所有权 if helper.VerifyUserfile(p, strconv.Itoa(int(uid))) { if err := os.Remove(p); err != nil { fmt.Println("DEL TOPIC Attachment, TOPIC ID:", id, ",ERR:", err) } } } } } //检查内容字段并尝试删除文件 if topic.Content != "" { //若内容中存在图片则开始尝试删除图片 delfiles_local := []string{} if m, n := helper.GetImages(topic.Content); n > 0 { for _, v := range m { if helper.IsLocal(v) { delfiles_local = append(delfiles_local, v) //如果本地同时也存在banner缓存文件,则加入旧图集合中,等待后面一次性删除 if p := helper.Url2local(helper.SetSuffix(v, "_banner.jpg")); helper.Exist(p) { delfiles_local = append(delfiles_local, p) } if p := helper.Url2local(helper.SetSuffix(v, "_large.jpg")); helper.Exist(p) { delfiles_local = append(delfiles_local, p) } if p := helper.Url2local(helper.SetSuffix(v, "_medium.jpg")); helper.Exist(p) { delfiles_local = append(delfiles_local, p) } if p := helper.Url2local(helper.SetSuffix(v, "_small.jpg")); helper.Exist(p) { delfiles_local = append(delfiles_local, p) } } } for k, v := range delfiles_local { if p := helper.Url2local(v); helper.Exist(p) { //如若文件存在,则处理,否则忽略 //先行判断是否缩略图 如果不是则执行删除image表记录的操作 因为缩略图是没有存到image表记录里面的 isThumbnails := bool(true) //false代表不是缩略图 true代表是缩略图 if (!strings.HasSuffix(v, "_large.jpg")) && (!strings.HasSuffix(v, "_medium.jpg")) && (!strings.HasSuffix(v, "_small.jpg")) { isThumbnails = false } //验证是否管理员权限 if allow { if err := os.Remove(p); err != nil { fmt.Println("#", k, ",ROOT DEL FILE ERROR:", err) } //删除image表中已经被删除文件的记录 if !isThumbnails { if e := DelImageByLocation(v); e != nil { fmt.Println("DelImageByLocation删除未使用文件", v, "的数据记录时候出现错误:", e) } } } else { //检查用户对文件的所有权 if helper.VerifyUserfile(p, strconv.Itoa(int(uid))) { if err := os.Remove(p); err != nil { fmt.Println("#", k, ",DEL FILE ERROR:", err) } //删除image表中已经被删除文件的记录 if !isThumbnails { if e := DelImageByLocation(v); e != nil { fmt.Println("v:", v) fmt.Println("DelImageByLocation删除未使用文件", v, "的数据记录时候出现错误:", e) } } } } } } } } //不管实际路径中是否存在文件均删除该数据库记录,以免数据库记录陷入死循环无法删掉 if topic.Id == id { if row, err := Engine.Id(id).Delete(new(Topic)); err != nil || row == 0 { fmt.Println("row:::", row) fmt.Println("删除话题错误:", err) //此时居然为空! return errors.New("删除话题错误!") //错误还要我自己构造?! } else { return nil } } } return errors.New("你无权删除此话题:" + strconv.Itoa(int(id))) } return errors.New("无法删除不存在的TOPIC ID:" + strconv.Itoa(int(id))) }
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" } } }