コード例 #1
0
ファイル: RApi.go プロジェクト: JessonChan/toropress
func (self *RApi) Post() {
	switch {
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-about-gallery-edit/([0-9]+)$"):
		//# Gallery 圖片编辑 POST状态 ,主要是爲了設置url
		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.WriteString(outtimesz)
		} else {
			inputs := self.Input()
			url := inputs.Get("url")
			//圖片ID
			editmid, _ := self.GetInt(":editmid")
			img := models.GetFile(editmid)

			if e := models.SetFile(editmid, img.Pid, img.Ctype, img.Filename, img.Content, img.Hash, img.Location, url, img.Size); e != nil {
				self.Data["MsgErr"] = "设置图片链接失败!"
			} else {
				self.Data["MsgErr"] = "设置图片链接成功!"
			}

			self.TplNames = "root/gallery_editurl.html"
			self.Render()
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-about-topic-edit/([0-9]+)$"):
		//ABOUT内容编辑 POST状态
		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid, nid int64 = 1, 1
			file_location := ""
			ftitle := self.GetString("ftitle")
			stitle := self.GetString("stitle")
			content := self.GetString("content")
			uid, _ := self.GetSession("userid").(int64)
			tid, _ := self.GetInt(":tid")

			file, handler, e := self.GetFile("image")

			if handler != nil && e == nil {

				if tid != 0 {
					tp := models.GetTopic(tid)
					if tp.Attachment != "" {
						file_location = tp.Attachment
					}
				} else {
					self.Data["MsgErr"] = "你编辑的内容不存在!"
				}
				ext := "." + strings.Split(handler.Filename, ".")[1]
				filename := helper.MD5(time.Now().String()) + ext

				path := "/archives/upload/" + time.Now().Format("2006/01/02/")

				os.MkdirAll("."+path, 0644)
				path = path + filename
				f, err := os.OpenFile("."+path, os.O_WRONLY|os.O_CREATE, 0644)
				defer f.Close()
				if err != nil {
					self.Data["MsgErr"] = "无法打开服务端文件存储路径!"
				} else {
					io.Copy(f, file)
					defer file.Close()
					input_file := "." + path
					output_file := "." + path
					output_size := "232x135"
					output_align := "center"
					helper.Thumbnail(input_file, output_file, output_size, output_align, "white")

					//若文件存在则删除,不存在就当忽略处理
					if file_location != "" {
						if helper.Exist("." + file_location) {
							if err := os.Remove("." + file_location); err != nil {
								self.Data["MsgErr"] = "删除旧文件错误!"
							}
						}
					}
					file_location = path

				}
			}

			if ftitle != "" && nid != 0 && content != "" {
				//保存编辑
				if e := models.SetTopic(tid, cid, nid, uid, 1, ftitle, content, stitle, file_location); e != nil {
					self.Data["MsgErr"] = "你提交的修改保存失败,无法写入数据库!"

				} else {
					self.Data["MsgErr"] = "你提交的修改已保存成功!"
				}
			} else {
				//下面三个为基本诉求
				msg := ""
				if ftitle == "" {
					msg = msg + "标题不能为空!"
				}
				if nid == 0 {
					msg = msg + "分类不正确!"
				}
				if content == "" {
					msg = msg + "内容不能为空!"
				}
				self.Data["MsgErr"] = msg
			}
			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-about-node-edit/([0-9]+)$"):
		/// about 节点编辑 POST状态

		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 1
			title := self.GetString("title")
			content := self.GetString("content")
			nid, _ := self.GetInt(":nid")
			//编辑NODE
			if title != "" && nid != 0 {
				if e := models.SetNode(nid, title, content, cid, 1); e != nil {

					self.Data["MsgErr"] = "内容分类无法保存到数据库!"
				} else {

					self.Data["MsgErr"] = "内容分类成功保存到数据库!"
				}

				if content == "" {
					self.Data["MsgErr"] = ""
				}
				self.Data["MsgErr"] = "内容分类“" + title + "”保存成功!"
			} else {
				self.Data["MsgErr"] = "内容分类标题不能为空!"
			}

			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-services-node-edit/([0-9]+)$"):
		/// services 节点编辑 POST状态

		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 3
			title := self.GetString("title")
			content := self.GetString("content")
			nid, _ := self.GetInt(":nid")
			//编辑NODE
			if title != "" && nid != 0 {
				if e := models.SetNode(nid, title, content, cid, 1); e != nil {

					self.Data["MsgErr"] = "内容分类无法保存到数据库!"
				} else {

					self.Data["MsgErr"] = "内容分类成功保存到数据库!"
				}

				if content == "" {
					self.Data["MsgErr"] = ""
				}
				self.Data["MsgErr"] = "内容分类“" + title + "”保存成功!"
			} else {
				self.Data["MsgErr"] = "内容分类标题不能为空!"
			}

			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-services-topic-edit/([0-9]+)$"):
		// services 内容编辑 POST状态
		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 3
			file_location := ""
			ftitle := self.GetString("ftitle")
			stitle := self.GetString("stitle")
			content := self.GetString("content")
			uid, _ := self.GetSession("userid").(int64)
			tid, _ := self.GetInt(":tid")
			nid, _ := self.GetInt("nodeid")
			file, handler, e := self.GetFile("image")

			if handler != nil && e == nil {

				if tid != 0 {
					tp := models.GetTopic(tid)
					if tp.Attachment != "" {
						file_location = tp.Attachment
					}
				} else {
					self.Data["MsgErr"] = "你编辑的内容不存在!"
				}
				ext := "." + strings.Split(handler.Filename, ".")[1]
				filename := helper.MD5(time.Now().String()) + ext

				path := "/archives/upload/" + time.Now().Format("2006/01/02/")

				os.MkdirAll("."+path, 0644)
				path = path + filename
				f, err := os.OpenFile("."+path, os.O_WRONLY|os.O_CREATE, 0644)
				defer f.Close()
				if err != nil {
					self.Data["MsgErr"] = "无法打开服务端文件存储路径!"
				} else {
					io.Copy(f, file)
					defer file.Close()
					input_file := "." + path
					output_file := "." + path
					output_size := "211x134"
					output_align := "center"
					helper.Thumbnail(input_file, output_file, output_size, output_align, "white")

					//若文件存在则删除,不存在就当忽略处理
					if file_location != "" {
						if helper.Exist("." + file_location) {
							if err := os.Remove("." + file_location); err != nil {
								self.Data["MsgErr"] = "删除旧文件错误!"
							}
						}
					}
					file_location = path

				}
			}

			if ftitle != "" && nid != 0 && content != "" {
				//保存编辑
				if e := models.SetTopic(tid, cid, nid, uid, 1, ftitle, content, stitle, file_location); e != nil {
					self.Data["MsgErr"] = "你提交的修改保存失败,无法写入数据库!"

				} else {
					self.Data["MsgErr"] = "你提交的修改已保存成功!"
				}
			} else {
				//下面三个为基本诉求
				msg := ""
				if ftitle == "" {
					msg = msg + "标题不能为空!"
				}
				if nid == 0 {
					msg = msg + "分类不正确!"
				}
				if content == "" {
					msg = msg + "内容不能为空!"
				}
				self.Data["MsgErr"] = msg
			}
			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-crafts-node-edit/([0-9]+)$"):
		/// crafts 节点编辑 POST状态

		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 4
			title := self.GetString("title")
			content := self.GetString("content")
			nid, _ := self.GetInt(":nid")
			//编辑NODE
			if title != "" && nid != 0 {
				if e := models.SetNode(nid, title, content, cid, 1); e != nil {

					self.Data["MsgErr"] = "内容分类无法保存到数据库!"
				} else {

					self.Data["MsgErr"] = "内容分类成功保存到数据库!"
				}

				if content == "" {
					self.Data["MsgErr"] = ""
				}
				self.Data["MsgErr"] = "内容分类“" + title + "”保存成功!"
			} else {
				self.Data["MsgErr"] = "内容分类标题不能为空!"
			}

			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-crafts-topic-edit/([0-9]+)$"):
		// crafts 内容编辑 POST状态
		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 4
			file_location := ""
			ftitle := self.GetString("ftitle")
			stitle := self.GetString("stitle")
			content := self.GetString("content")
			uid, _ := self.GetSession("userid").(int64)
			tid, _ := self.GetInt(":tid")
			nid, _ := self.GetInt("nodeid")
			file, handler, e := self.GetFile("image")

			if handler != nil && e == nil {

				if tid != 0 {
					tp := models.GetTopic(tid)
					if tp.Attachment != "" {
						file_location = tp.Attachment
					}
				} else {
					self.Data["MsgErr"] = "你编辑的内容不存在!"
				}
				ext := "." + strings.Split(handler.Filename, ".")[1]
				filename := helper.MD5(time.Now().String()) + ext

				path := "/archives/upload/" + time.Now().Format("2006/01/02/")

				os.MkdirAll("."+path, 0644)
				path = path + filename
				f, err := os.OpenFile("."+path, os.O_WRONLY|os.O_CREATE, 0644)
				defer f.Close()
				if err != nil {
					self.Data["MsgErr"] = "无法打开服务端文件存储路径!"
				} else {
					io.Copy(f, file)
					defer file.Close()
					input_file := "." + path
					output_file := "." + path
					output_size := "534"
					output_align := "center"
					helper.Thumbnail(input_file, output_file, output_size, output_align, "black")

					//若文件存在则删除,不存在就当忽略处理
					if file_location != "" {
						if helper.Exist("." + file_location) {
							if err := os.Remove("." + file_location); err != nil {
								self.Data["MsgErr"] = "删除旧文件错误!"
							}
						}
					}
					file_location = path

				}
			}

			if ftitle != "" && nid != 0 && content != "" {
				//保存编辑
				if e := models.SetTopic(tid, cid, nid, uid, 0, ftitle, content, stitle, file_location); e != nil {
					self.Data["MsgErr"] = "你提交的修改保存失败,无法写入数据库!"

				} else {
					self.Data["MsgErr"] = "你提交的修改已保存成功!"
				}
			} else {
				//下面三个为基本诉求
				msg := ""
				if ftitle == "" {
					msg = msg + "标题不能为空!"
				}
				if nid == 0 {
					msg = msg + "分类不正确!"
				}
				if content == "" {
					msg = msg + "内容不能为空!"
				}
				self.Data["MsgErr"] = msg
			}
			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-information-topic-edit/([0-9]+)$"):
		// information 内容编辑 POST状态
		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 5
			file_location := ""
			ftitle := self.GetString("ftitle")
			stitle := self.GetString("stitle")
			content := self.GetString("content")
			uid, _ := self.GetSession("userid").(int64)
			tid, _ := self.GetInt(":tid")
			nid, _ := self.GetInt("nodeid")
			file, handler, e := self.GetFile("image")

			if handler != nil && e == nil {

				if tid != 0 {
					tp := models.GetTopic(tid)
					if tp.Attachment != "" {
						file_location = tp.Attachment
					}
				} else {
					self.Data["MsgErr"] = "你编辑的内容不存在!"
				}
				ext := "." + strings.Split(handler.Filename, ".")[1]
				filename := helper.MD5(time.Now().String()) + ext

				path := "/archives/upload/" + time.Now().Format("2006/01/02/")

				os.MkdirAll("."+path, 0644)
				path = path + filename
				f, err := os.OpenFile("."+path, os.O_WRONLY|os.O_CREATE, 0644)
				defer f.Close()
				if err != nil {
					self.Data["MsgErr"] = "无法打开服务端文件存储路径!"
				} else {
					io.Copy(f, file)
					defer file.Close()
					input_file := "." + path
					output_file := "." + path
					output_size := "534"
					output_align := "center"
					helper.Thumbnail(input_file, output_file, output_size, output_align, "black")

					//若文件存在则删除,不存在就当忽略处理
					if file_location != "" {
						if helper.Exist("." + file_location) {
							if err := os.Remove("." + file_location); err != nil {
								self.Data["MsgErr"] = "删除旧文件错误!"
							}
						}
					}
					file_location = path

				}
			}

			if ftitle != "" && nid != 0 && content != "" {
				//保存编辑
				if e := models.SetTopic(tid, cid, nid, uid, 0, ftitle, content, stitle, file_location); e != nil {
					self.Data["MsgErr"] = "你提交的修改保存失败,无法写入数据库!"

				} else {
					self.Data["MsgErr"] = "你提交的修改已保存成功!"
				}
			} else {
				//下面三个为基本诉求
				msg := ""
				if ftitle == "" {
					msg = msg + "标题不能为空!"
				}
				if nid == 0 {
					msg = msg + "分类不正确!"
				}
				if content == "" {
					msg = msg + "内容不能为空!"
				}
				self.Data["MsgErr"] = msg
			}
			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-information-node-edit/([0-9]+)$"):
		// information 节点编辑 POST状态

		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 5
			title := self.GetString("title")
			content := self.GetString("content")
			nid, _ := self.GetInt(":nid")
			//编辑NODE
			if title != "" && nid != 0 {
				if e := models.SetNode(nid, title, content, cid, 1); e != nil {

					self.Data["MsgErr"] = "内容分类无法保存到数据库!"
				} else {

					self.Data["MsgErr"] = "内容分类成功保存到数据库!"
				}

				if content == "" {
					self.Data["MsgErr"] = ""
				}
				self.Data["MsgErr"] = "内容分类“" + title + "”保存成功!"
			} else {
				self.Data["MsgErr"] = "内容分类标题不能为空!"
			}

			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-contact-node-edit/([0-9]+)$"):
		//contact 节点编辑 POST状态

		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 6
			title := self.GetString("title")
			content := self.GetString("content")
			nid, _ := self.GetInt(":nid")
			//编辑NODE
			if title != "" && nid != 0 {
				if e := models.SetNode(nid, title, content, cid, 1); e != nil {

					self.Data["MsgErr"] = "内容分类无法保存到数据库!"
				} else {

					self.Data["MsgErr"] = "内容分类成功保存到数据库!"
				}

				if content == "" {
					self.Data["MsgErr"] = ""
				}
				self.Data["MsgErr"] = "内容分类“" + title + "”保存成功!"
			} else {
				self.Data["MsgErr"] = "内容分类标题不能为空!"
			}

			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	case helper.Rex(self.Ctx.Request.RequestURI, "^/root-contact-topic-edit/([0-9]+)$"):
		//CONTACT内容编辑 POST状态
		if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
			self.Ctx.Redirect(302, "/root-login")
		} else {
			var cid int64 = 6
			file_location := ""
			ftitle := self.GetString("title")
			content := self.GetString("content")
			uid, _ := self.GetSession("userid").(int64)
			tid, _ := self.GetInt(":tid")
			nid, _ := self.GetInt("nodeid")

			if ftitle != "" && nid != 0 && content != "" {
				//保存编辑
				if e := models.SetTopic(tid, cid, nid, uid, 0, ftitle, content, "", file_location); e != nil {
					self.Data["MsgErr"] = "你提交的修改保存失败,无法写入数据库!"

				} else {
					self.Data["MsgErr"] = "你提交的修改已保存成功!"
				}
			} else {
				//下面三个为基本诉求
				msg := ""
				if ftitle == "" {
					msg = msg + "标题不能为空!"
				}
				if nid == 0 {
					msg = msg + "分类不正确!"
				}
				if content == "" {
					msg = msg + "内容不能为空!"
				}
				self.Data["MsgErr"] = msg
			}
			self.SetSession("MsgErr", self.Data["MsgErr"])
			self.Redirect(self.Ctx.Request.RequestURI, 302)
		}
	}
}
コード例 #2
0
func (self *RUploaderHandler) Post() {
	/*
		//TODO: Validate the file type

	*/

	if sess_role, _ := self.GetSession("userrole").(int64); sess_role != -1000 {
		_, handler, _ := self.GetFile("uploadfile")

		if handler != nil {
			self.Ctx.WriteString(outtimes + "上传“ " + handler.Filename + " ”失败,请你重新登录,现已超时操作!")
		} else {
			self.Ctx.WriteString(outtimes + "请你重新登录,现已超时操作!")
		}
	} else {
		targetFolder := "/archives/upload/"

		file, handler, e := self.GetFile("uploadfile")
		pid, _ := self.GetInt("pid")

		if e != nil {
			self.Data["MsgErr"] = "0"
		} else {

			if handler != nil {
				ext := "." + strings.Split(handler.Filename, ".")[1]
				filename := helper.MD5(time.Now().String()) + ext

				path := targetFolder + time.Now().Format("2006/01/02/")

				os.MkdirAll("."+path, 0644)
				path = path + filename
				f, err := os.OpenFile("."+path, os.O_WRONLY|os.O_CREATE, 0644)
				defer f.Close()

				if err != nil {
					self.Data["MsgErr"] = "0"
				} else {
					io.Copy(f, file)
					defer file.Close()
					input_file := "." + path
					output_file := "." + path
					output_size := "534"
					output_align := "center"
					background := "black"
					helper.Thumbnail(input_file, output_file, output_size, output_align, background)
					hash, _ := helper.Filehash(output_file)
					fileInfo, err := os.Stat(output_file)
					var fsize int64 = 0
					if err == nil {
						fsize = fileInfo.Size() / 1024
					}

					self.Data["MsgErr"] = "<img src=\"" + path + "\" alt=\"" + string(hash) + "\" />"

					models.SetFile(0, pid, 0, handler.Filename, "", string(hash), path, "", fsize)
				}

			} else {
				self.Data["MsgErr"] = "0"
			}
		}

		self.Ctx.WriteString(self.Data["MsgErr"].(string))
	}
}