Example #1
0
File: mzs.go Project: rose312/mzr
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函数出现错误!")
}
Example #2
0
File: model.go Project: rose312/mzr
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)
			}

		}
	}
}
Example #3
0
File: mzs.go Project: rose312/mzr
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")))

					}
				}

			}
		}

	}

}
Example #4
0
File: model.go Project: rose312/mzr
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)))
}