예제 #1
0
//GetBkimg url: /user/bkimg/get
func GetBkimg(cookie UserCookie, config Config, logger *log.Logger, r render.Render) {
	if !cookie.Validate() {
		r.JSON(http.StatusOK, J{"data": nil})
		logger.Info("Fail to auth whith cookie:", cookie)
		return
	}

	db, err := gorm.Open(config.DB.Type, config.DB.Uri)
	if err != nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil, "err": J{"code": 201, "msg": "database open error."}})
		return
	}
	var BkimgName string

	row := db.Table("users").Where(&cookie).Select("bkimg").Row()

	if row == nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil})
		logger.Error(err)
		return
	}

	row.Scan(&BkimgName)
	r.JSON(http.StatusOK, J{"data": BkimgName, "err": nil})

}
예제 #2
0
//handleGetSched URL:/sched/all 获取日程表数据
func handleGetSched(r render.Render, logger *log.Logger, config Config, cookie UserCookie) {

	if !cookie.Validate() {
		logger.Info("Fail to auth whith cookie:", cookie)
		r.JSON(http.StatusOK, J{"data": nil})
		return
	}

	//type表示数据库的类型,如mysql,sqlite3等
	//uri为需要打开的数据库连接,格式为user:password@/dbname?charset=utf8
	//两者都定义在config.ini中
	db, err := gorm.Open(config.DB.Type, config.DB.Uri)
	if err != nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil})
		logger.Error(err)
		return
	}
	defer db.Close()
	var count int
	db.Table("users").Where(&cookie).Count(&count)
	if count == 0 {
		r.JSON(http.StatusOK, J{"data": nil})
		return
	}

	var sched []Sched
	db.Table("scheds").Select("*").Where("user=?", cookie.Name).Find(&sched)

	r.JSON(http.StatusOK, J{"data": sched})

	logger.Info("Schedule items total", len(sched), "in JSON,", "with cookie:", cookie)
}
예제 #3
0
//UploadBkimg url: /user/bkimg/upload
func UploadBkimg(img Bkimg, r render.Render, cookie UserCookie, config Config, logger *log.Logger) {
	//检查cookie的有效性
	if !cookie.Validate() {
		r.Redirect("/", http.StatusUnauthorized)
		logger.Info("Fail to auth whith cookie:", cookie)
		return
	}
	//打开上传文件
	file, err := img.Content.Open()
	if err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}

	//将文件内容全被读出来
	b, err := ioutil.ReadAll(file)
	if err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}

	//检查该图片文件的类型,如果不是图片文件的话那么上传失败,返回。
	_, format, err := image.Decode(bytes.NewReader(b))
	switch err {
	case image.ErrFormat:
		r.Redirect("/", http.StatusOK)
		return
	case nil:
		break
	default:
		r.Redirect("/", http.StatusInternalServerError)
		logger.Info(err.Error())
		return
	}

	//计算文件的md5,作为唯一表示以及文件名。
	fileMd5 := util.Md5(b)
	fileName := fileMd5 + "." + format
	fileFullName := filepath.Join(config.Server.StaticHome, "img/bk", fileName)

	//如果该文件存在那么直接跳到接入数据库
	if fi, _ := os.Stat(fileFullName); fi != nil {
		r.Redirect("/", http.StatusFound)
		logger.Info("file exists:", fileFullName)
		goto CommitToDB
	}
	err = ioutil.WriteFile(fileFullName, b, 0600)
	if err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}

	//将该图片文件的文件名存入users表的bkimg字段中。
CommitToDB:
	db, err := gorm.Open(config.DB.Type, config.DB.Uri)
	if err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}

	if err = db.Table("users").Where(&cookie).Update("bkimg", fileName).Error; err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}
	r.Redirect("/", http.StatusFound)

}