Ejemplo n.º 1
0
Archivo: model.go Proyecto: 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)
			}

		}
	}
}
Ejemplo n.º 2
0
Archivo: model.go Proyecto: 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)))
}