示例#1
0
文件: focus.go 项目: blackmady/GoCMS
//添加焦点图
func (c Focus) Add(focus *models.Focus) revel.Result {

	if c.Request.Method == "GET" {

		title := "添加焦点图--GoCMS管理系统"

		FocusCate := new(models.FocusCate)
		Cate_list := FocusCate.GetCateList()

		c.Render(title, Cate_list)
		return c.RenderTemplate("Content/Focus/Add.html")

	} else {

		var cid string = c.Params.Get("cid")
		if len(cid) > 0 {

			Cid, err := strconv.ParseInt(cid, 10, 64)
			if err != nil {
				revel.WARN.Println(err)
			}

			focus.Cid = Cid

			focusCate := new(models.FocusCate)
			focusCate_info := focusCate.GetById(Cid)

			if focusCate_info.Id <= 0 {
				c.Flash.Error("焦点图分类错误!")
				c.Flash.Out["url"] = "/Focus/Add/"
				return c.Redirect("/Message/")
			}

			//****************************************
			//图片

			//判断是否是系统的分隔符
			separator := "/"
			if os.IsPathSeparator('\\') {
				separator = "\\"
			} else {
				separator = "/"
			}

			//接收上传文件
			imgFile, header, err := c.Request.FormFile("img")
			if err != nil {
				c.Flash.Error("图片上传错误!")
				c.Flash.Out["url"] = "/Focus/Add/"
				return c.Redirect("/Message/")
			}
			defer imgFile.Close()

			//读取文件数据
			fileData, _ := ioutil.ReadAll(imgFile)

			if len(fileData) >= 1024*1024*2 {
				c.Flash.Error("你上传大小为" + utils.FileSize(len(fileData)) + ",文件应小于2M!")
				c.Flash.Out["url"] = "/Focus/Add/"
				return c.Redirect("/Message/")
			}

			basepath, _ := filepath.Abs("")
			config_file := (revel.BasePath + "/conf/config.conf")
			config_file = strings.Replace(config_file, "/", separator, -1)
			config_conf, _ := config.ReadDefault(config_file)

			//上传文件目录
			upload_dir, _ := config_conf.String("upload", "upload.dir")
			//允许上传的后缀名
			titlesuffix, _ := config_conf.String("upload", "upload.titlesuffix")

			//文件类型检测
			if !strings.Contains(titlesuffix, path.Ext(header.Filename)) {
				c.Flash.Error("文件只支持图片!")
				c.Flash.Out["url"] = "/Focus/Add/"
				return c.Redirect("/Message/")
			}

			//前台网站调用目录
			web_save_path := fmt.Sprintf("/%s/focus/%s", upload_dir, time.Now().Format("2006/01/02/"))

			//文件保存目录
			save_path := fmt.Sprintf("%s/www/%s/focus/%s", basepath, upload_dir, time.Now().Format("2006/01/02/"))
			//字符串替换 /替换为系统分隔符
			save_path = strings.Replace(save_path, "/", separator, -1)

			//创建目录
			err = os.MkdirAll(save_path, os.ModePerm)
			if err != nil {
				c.Flash.Error("创建目录失败!")
				c.Flash.Out["url"] = "/Focus/Add/"
				return c.Redirect("/Message/")
			}

			//新文件名
			rand.Seed(time.Now().UnixNano())
			rand_num := rand.Intn(99999)

			//原图
			new_file_name := time.Now().Format("20060102150404") + strconv.Itoa(rand_num) + path.Ext(header.Filename)

			//保存文件
			old_img := save_path + new_file_name

			err = ioutil.WriteFile(old_img, fileData, os.ModePerm)
			if err != nil {
				revel.WARN.Println(err)
				c.Flash.Error("保存图片失败!")
				c.Flash.Out["url"] = "/Focus/Add/"
				return c.Redirect("/Message/")
			}

			thumb_name := time.Now().Format("20060102150404") + strconv.Itoa(rand_num) + "_thumb" + path.Ext(header.Filename)

			//内容显示图片
			web_url_img := web_save_path + thumb_name

			//400宽度图片生成
			thumb := save_path + thumb_name
			thumb_width := strconv.Itoa(int(focusCate_info.Width)) + "x" + strconv.Itoa(int(focusCate_info.Height))
			utils.Resize(old_img, thumb, thumb_width, "center", "white")

			focus.Img = web_url_img

			//***************************************

		} else {
			c.Flash.Error("请选择所属分类!")
			c.Flash.Out["url"] = "/Focus/Add/"
			return c.Redirect("/Message/")
		}

		UserID := utils.GetSession("UserID", c.Session)

		if len(UserID) > 0 {
			UserID, err := strconv.ParseInt(UserID, 10, 64)
			if err != nil {
				revel.WARN.Println(err)
			}
			focus.Aid = UserID
		}

		var title string = c.Params.Get("title")
		if len(title) > 0 {
			focus.Title = title
		} else {
			c.Flash.Error("请输入标题!")
			c.Flash.Out["url"] = "/Focus/Add/"
			return c.Redirect("/Message/")
		}

		var url string = c.Params.Get("url")
		if len(url) > 0 {
			focus.Url = url
		} else {
			c.Flash.Error("请输入地址!")
			c.Flash.Out["url"] = "/Focus/Add/"
			return c.Redirect("/Message/")
		}

		var content string = c.Params.Get("content")
		if len(content) > 0 {
			focus.Content = content
		} else {
			c.Flash.Error("请输入摘要!")
			c.Flash.Out["url"] = "/Focus/Add/"
			return c.Redirect("/Message/")
		}

		var order string = c.Params.Get("order")
		if len(order) > 0 {

			Order, err := strconv.ParseInt(order, 10, 64)
			if err != nil {
				revel.WARN.Println(err)
			}

			focus.Order = Order
		} else {
			focus.Order = 0
		}

		var status string = c.Params.Get("status")
		Status, err := strconv.ParseInt(status, 10, 64)
		if err != nil {
			revel.WARN.Println(err)
		}

		focus.Status = Status

		if focus.Save() {
			c.Flash.Success("添加焦点图成功")
			c.Flash.Out["url"] = "/Focus/"
			return c.Redirect("/Message/")
		} else {
			c.Flash.Error("添加焦点图失败")
			c.Flash.Out["url"] = "/Focus/Add/"
			return c.Redirect("/Message/")
		}
	}
}
示例#2
0
//内容发布标题缩略图
func (c *Kindeditor) TitleImage(upload *models.Upload) revel.Result {
	data := make(map[string]interface{})

	//判断是否是系统的分隔符
	separator := "/"
	if os.IsPathSeparator('\\') {
		separator = "\\"
	} else {
		separator = "/"
	}

	//接收上传文件
	imgFile, header, err := c.Request.FormFile("imgFile")
	if err != nil {
		data["error"] = 1
		data["message"] = err.Error()

		return c.RenderJson(data)
	}
	defer imgFile.Close()

	//读取文件数据
	fileData, _ := ioutil.ReadAll(imgFile)

	if len(fileData) >= 1024*1024*2 {
		data["error"] = 1
		data["message"] = "你上传大小为" + utils.FileSize(len(fileData)) + ",文件应小于2M!"

		return c.RenderJson(data)
	}

	basepath, _ := filepath.Abs("")
	config_file := (revel.BasePath + "/conf/config.conf")
	config_file = strings.Replace(config_file, "/", separator, -1)
	config_conf, _ := config.ReadDefault(config_file)

	//上传文件目录
	upload_dir, _ := config_conf.String("upload", "upload.dir")
	//允许上传的后缀名
	titlesuffix, _ := config_conf.String("upload", "upload.titlesuffix")

	//前台网站地址
	sitedomain, _ := config_conf.String("website", "website.sitedomain")

	//文件类型检测
	if !strings.Contains(titlesuffix, path.Ext(header.Filename)) {
		data["error"] = 1
		data["message"] = "文件只支持Office文件,图片和rar存档!"

		return c.RenderJson(data)
	}

	//前台网站调用目录
	web_save_path := fmt.Sprintf("/%s/article/%s", upload_dir, time.Now().Format("2006/01/02/"))

	//文件保存目录
	save_path := fmt.Sprintf("%s/www/%s/article/%s", basepath, upload_dir, time.Now().Format("2006/01/02/"))
	//字符串替换 /替换为系统分隔符
	save_path = strings.Replace(save_path, "/", separator, -1)

	//创建目录
	err = os.MkdirAll(save_path, os.ModePerm)
	if err != nil {
		revel.WARN.Println(err)
		data["error"] = 1
		data["message"] = "创建目录失败!"

		return c.RenderJson(data)
	}

	//新文件名
	rand.Seed(time.Now().UnixNano())
	rand_num := rand.Intn(99999)

	//原图
	new_file_name := time.Now().Format("20060102150404") + strconv.Itoa(rand_num) + path.Ext(header.Filename)

	//保存文件
	old_img := save_path + new_file_name

	err = ioutil.WriteFile(old_img, fileData, os.ModePerm)
	if err != nil {
		revel.WARN.Println(err)

		data["error"] = 1
		data["message"] = "保存图片失败!"
		return c.RenderJson(data)
	}

	//*******************图片处理****************

	//缩略图400
	thumb_name := time.Now().Format("20060102150404") + strconv.Itoa(rand_num) + "_100" + path.Ext(header.Filename)

	//内容显示图片
	web_url := web_save_path + thumb_name

	//400宽度图片生成
	thumb_135 := save_path + thumb_name
	utils.Resize(old_img, thumb_135, "100x70", "center", "white")

	//*******************图片处理****************

	data["error"] = 0
	data["url"] = sitedomain + web_url

	return c.RenderJson(data)
}
示例#3
0
//添加来源
func (c Copyfrom) Add(copyfrom *models.Copyfrom) revel.Result {

	if c.Request.Method == "GET" {
		title := "添加来源--GoCMS管理系统"

		c.Render(title)
		return c.RenderTemplate("Extend/Copyfrom/Add.html")
	} else {

		//接收上传文件
		thumb, header, err := c.Request.FormFile("thumb")
		if err != nil {
			//来源logo
			copyfrom.Thumb = ""
		} else {
			defer thumb.Close()

			//判断是否是系统的分隔符
			separator := "/"
			if os.IsPathSeparator('\\') {
				separator = "\\"
			} else {
				separator = "/"
			}

			fileData, _ := ioutil.ReadAll(thumb)

			if len(fileData) >= 1024*1024*2 {
				c.Flash.Error("你上传大小为" + utils.FileSize(len(fileData)) + ",文件应小于2M!")
				c.Flash.Out["url"] = "/Copyfrom/Add/"
				return c.Redirect("/Message/")
			}

			basepath, _ := filepath.Abs("")
			config_file := (revel.BasePath + "/conf/config.conf")
			config_file = strings.Replace(config_file, "/", separator, -1)
			config_conf, _ := config.ReadDefault(config_file)

			//上传文件目录
			upload_dir, _ := config_conf.String("upload", "upload.dir")
			//允许上传的后缀名
			filesuffix, _ := config_conf.String("upload", "upload.filesuffix")

			//文件类型检测
			if !strings.Contains(filesuffix, path.Ext(header.Filename)) {
				c.Flash.Error("文件只支持图片!")
				c.Flash.Out["url"] = "/Copyfrom/Add/"
				return c.Redirect("/Message/")
			}

			//文件保存目录
			web_save_path := fmt.Sprintf("/%s/copyfrom/%s", upload_dir, time.Now().Format("2006/01/02/"))
			save_path := fmt.Sprintf("%s/www/%s/copyfrom/%s", basepath, upload_dir, time.Now().Format("2006/01/02/"))
			//字符串替换 /替换为系统分隔符
			save_path = strings.Replace(save_path, "/", separator, -1)

			//新文件名
			rand.Seed(time.Now().UnixNano())
			rand_num := rand.Intn(99999)
			new_file_name := time.Now().Format("20060102150404") + strconv.Itoa(rand_num) + path.Ext(header.Filename)

			//创建目录
			error := os.MkdirAll(save_path, os.ModePerm)
			if error != nil {
				c.Flash.Error(error.Error())
				c.Flash.Out["url"] = "/Copyfrom/Add/"
				return c.Redirect("/Message/")
			}

			//保存文件
			file_dir := save_path + new_file_name
			save_url := web_save_path + new_file_name

			e := ioutil.WriteFile(file_dir, fileData, os.ModePerm)
			if e != nil {
				c.Flash.Error(e.Error())
				c.Flash.Out["url"] = "/Copyfrom/Add/"
				return c.Redirect("/Message/")
			}

			//来源logo
			copyfrom.Thumb = save_url
		}

		//来源名称
		var sitename string = c.Params.Get("sitename")
		if len(sitename) > 0 {
			copyfrom.Sitename = sitename
		} else {
			c.Flash.Error("请输入来源名称!")
			c.Flash.Out["url"] = "/Copyfrom/Add/"
			return c.Redirect("/Message/")
		}

		//来源链接
		var siteurl string = c.Params.Get("siteurl")
		if len(siteurl) > 0 {
			copyfrom.Siteurl = siteurl
		} else {
			c.Flash.Error("请输入来源链接!")
			c.Flash.Out["url"] = "/Copyfrom/Add/"
			return c.Redirect("/Message/")
		}

		if copyfrom.Save() {

			//******************************************
			//管理员日志
			if UserID, ok := c.Session["UserID"]; ok {
				UserID, err := strconv.ParseInt(UserID, 10, 64)
				if err != nil {
					revel.WARN.Println(err)
				}

				admin := new(models.Admin)
				admin_info := admin.GetById(UserID)

				logs := new(models.Logs)
				desc := "添加来源:" + sitename + "|^|来源管理"
				logs.Save(admin_info, c.Controller, desc)
			}

			//*****************************************

			c.Flash.Success("添加来源成功!")
			c.Flash.Out["url"] = "/Copyfrom/"
			return c.Redirect("/Message/")
		} else {
			c.Flash.Error("添加来源失败")
			c.Flash.Out["url"] = "/Copyfrom/Add/"
			return c.Redirect("/Message/")
		}

	}
}