//路由 func router() { beego.DirectoryIndex = true beego.SetStaticPath("/test", utils.MergePath("test")) beego.AutoRender = false beego.SetStaticPath("/static", utils.MergePath("static")) beego.SetStaticPath("/upload", utils.MergePath(beego.AppConfig.String("UploadPath"))) //首页 home := &controllers.Home{} beego.AutoRouter(home) beego.Router("/", home, "*:Cn") //登录 beego.Router("/home/login", home, "get,post:Login") beego.Router("/home/logout", home, "get,post:Logout") //后台管理 admin := &controllers.Admin{} beego.Router("/admin", admin, "*:Index") beego.Router("/admin/index", admin, "*:Index") beego.Router("/admin/upload", admin, "post:Upload") //个人信息 profile := &controllers.Profile{} beego.Router("/admin/profile", profile, "get:Index") beego.Router("/admin/profile/password", profile, "get,post:UpdatePassword") beego.Router("/admin/profile/edit", profile, "get,post:UpdateProfile") //账户管理 account := &controllers.Account{} beego.Router("/admin/account", account, "get:Index") beego.Router("/admin/account/create", account, "*:Create") beego.Router("/admin/account/edit", account, "post:Edit") beego.Router("/admin/account/edit/:id:int", account, "get:Edit") beego.Router("/admin/account/delete/:id:int", account, "*:Delete") beego.Router("/admin/account/reset", account, "*:Reset") //文章管理 article := &controllers.Article{} beego.Router("/admin/article", article, "get:Index") beego.Router("/admin/article/create", article, "*:Create") beego.Router("/admin/article/edit", article, "post:Edit") beego.Router("/admin/article/edit/:id:int", article, "get:Edit") beego.Router("/admin/article/delete/:id:int", article, "*:Delete") beego.Router("/admin/article/sequence", article, "*:Sequence") beego.Router("/admin/article/reset", article, "*:Reset") beego.Router("/admin/article/getall", article, "*:GetAll") //图片管理 image := &controllers.Image{} beego.Router("/admin/image/index/:id:int", image, "get:Index") beego.Router("/admin/image/create", image, "post:Create") beego.Router("/admin/image/edit", image, "post:Edit") beego.Router("/admin/image/create/:id:int", image, "get:Create") beego.Router("/admin/image/edit/:id:int", image, "get:Edit") beego.Router("/admin/image/delete/:id:int", image, "*:Delete") beego.Router("/admin/image/sequence", image, "*:Sequence") beego.Router("/admin/image/reset", image, "*:Reset") //频道管理 ch := &controllers.Channel{} beego.Router("/admin/channel", ch, "get:Index") beego.Router("/admin/channel/create", ch, "*:Create") beego.Router("/admin/channel/edit", ch, "post:Edit") beego.Router("/admin/channel/edit/:id:int", ch, "get:Edit") beego.Router("/admin/channel/delete/:id:int", ch, "*:Delete") beego.Router("/admin/channel/sequence", ch, "*:Sequence") beego.Router("/admin/channel/children", ch, "*:Children") beego.Router("/admin/channel/reset", ch, "*:Reset") beego.Router("/admin/channel/getall", ch, "*:GetAll") }
//文件上传 func (this *base) upload(key string) (files []*models.UploadFile, err error) { //处理上传文件 var header *multipart.FileHeader var file multipart.File var f *os.File //根据年月选择文件夹 t := time.Now().Format(time.RFC3339) //文件夹是否存在或创建文件夹 UploadPath := beego.AppConfig.String("UploadPath") folder := utils.MergePath(UploadPath) err = utils.GetDir(folder) if err != nil { return } // //用户文件夹是否存在或创建文件夹 // folder = filepath.Join(folder, strconv.Itoa(int(this.xm.Updator))) // err = utils.GetDir(folder) // if err != nil { // return // } //文件夹是否存在或创建文件夹 UploadPath = path.Join(UploadPath, beego.Substr(t, 0, 7)) folder = path.Join(folder, beego.Substr(t, 0, 7)) err = utils.GetDir(folder) if err != nil { return } fs := this.Ctx.Request.MultipartForm.File[key] n := len(fs) if n == 0 { err = errors.New("files not found") return } for i := 0; i < n; i++ { header = fs[i] file, err = fs[i].Open() if err != nil { return } defer file.Close() //提取原始文件信息 disposition := strings.Split(header.Header.Get("Content-Disposition"), ";") var key, value string for _, v := range disposition { pos := strings.Index(v, "=") if pos > -1 { key = v[:pos] if strings.TrimSpace(key) == "filename" { value = strings.Replace(v[pos+1:], "\"", "", -1) break } } } // filename := filepath.Base(value) //新建文件 UploadPath = path.Join("/", UploadPath, fmt.Sprintf("%d%s", time.Now().UnixNano(), filepath.Ext(filename))) f, err = os.OpenFile(path.Join(folder, fmt.Sprintf("%d%s", time.Now().UnixNano(), filepath.Ext(filename))), os.O_WRONLY|os.O_CREATE, 0666) if err != nil { return } defer f.Close() io.Copy(f, file) upf := new(models.UploadFile) upf.Name = filename upf.Ext = filepath.Ext(filename) upf.Path = UploadPath fi, _ := f.Stat() upf.Size = fi.Size() files = append(files, upf) } return }
func initconfig() { beego.AppPath = utils.AppRoot beego.AppConfigPath = path.Join(beego.AppPath, "conf", "app.conf") beego.ParseConfig() beego.ViewsPath = utils.MergePath(beego.ViewsPath) }