Example #1
0
func (s *Setting) Get() {
	res := mapi.Call(mapi.Setting.ReadGeneral, nil)
	if !res.Status {
		s.RenderError(500, errors.New(res.Error))
		return
	}
	s.Assign("General", res.Data["general"].(*mapi.SettingGeneral))

	themes, err := model.GetThemes()
	if err != nil {
		s.RenderError(500, err)
		return
	}
	s.Assign("Themes", themes)

	res = mapi.Call(mapi.Setting.ReadMedia, nil)
	if !res.Status {
		s.RenderError(500, errors.New(res.Error))
		return
	}
	s.Assign("Media", res.Data["media"].(*mapi.SettingMedia))

	s.Title("Setting")
	s.Render("setting.tmpl")
}
Example #2
0
func (u *Upload) Post() {
	u.Req().ParseMultipartForm(16 << 20)
	meta := &mapi.MediaUploadOption{
		Ctx:      u.Ctx,
		User:     u.AuthUser,
		FormName: "file",
		IsImage:  false,
	}
	if u.Form("type") == "image" {
		meta.IsImage = true
	}
	if u.Form("from") == "editor" {
		meta.FormName = "editormd-image-file"
	}
	res := mapi.Call(mapi.Media.Upload, meta)
	if u.Form("from") == "editor" {
		m := make(map[string]interface{})
		m["success"] = 1
		if !res.Status {
			m["success"] = 0
		}
		m["message"] = res.Error
		m["url"] = ""
		if media, ok := res.Data["media"].(*model.Media); ok {
			m["url"] = "/" + media.FilePath
		}
		u.ServeJson(m)
		return
	}
	u.ServeJson(res)
}
Example #3
0
// auth middleware handler
func AuthHandler() tango.HandlerFunc {
	return func(ctx *tango.Context) {
		auth, ok := ctx.Action().(IAuther)
		if !ok {
			ctx.Next()
			return
		}
		// read token
		token := auth.GetAuthToken(ctx)
		if token != "" {
			result := mapi.Call(mapi.User.Auth, token)
			if result.Status {
				auth.SetAuthUser(result.Data["user"].(*model.User))
				ctx.Next()
				return
			}
		}
		// fail redirect
		if ctx.Req().Method == "GET" {
			if url := auth.FailRedirect(); url != "" {
				ctx.Redirect(url, 302)
				return
			}
		}

		// auth fail , no redirect, to show 401
		ctx.WriteHeader(401)
	}
}
Example #4
0
func (l *Login) Post() {
	// bind form
	form := new(mapi.UserLoginForm)
	if err := l.Bind(form); err != nil {
		l.Assign("Error", err.Error())
		l.Render("login.tmpl")
		return
	}

	// call login mapi
	res := mapi.Call(mapi.User.Login, form)
	if !res.Status {
		l.Assign("Error", res.Error)
		l.Render("login.tmpl")
		return
	}

	// save token to cookie
	token := res.Data["token"].(*model.Token)
	l.Cookies().Set(&http.Cookie{
		Name:     "x-token",
		Value:    token.Token,
		Path:     "/",
		Expires:  time.Unix(token.ExpireTime, 0),
		MaxAge:   int(token.ExpireTime - time.Now().Unix()),
		HttpOnly: true,
	})
	l.Redirect("/admin")
}
Example #5
0
// setting middleware handler
func SettingHandler() tango.HandlerFunc {
	return func(ctx *tango.Context) {
		setter, ok := ctx.Action().(ISetting)
		if !ok {
			ctx.Next()
			return
		}

		// read general data
		res := mapi.Call(mapi.Setting.ReadGeneral, nil)
		if !res.Status {
			panic(errors.New(res.Error))
		}
		generalSettings := res.Data["general"].(*mapi.SettingGeneral)
		setter.SetGeneral(generalSettings)

		// assign general data
		t, ok := ctx.Action().(IRender)
		if ok {
			t.Assign("General", generalSettings)
		}

		ctx.Next()
	}
}
Example #6
0
func (r *Rss) Get() {
	res := mapi.Call(mapi.Article.ListArchive, nil)
	if !res.Status {
		panic(errors.New(res.Error))
		return
	}
	opt := &mapi.RssOption{
		Setting:  r.GetGeneral(),
		Articles: res.Data["articles"].([]*model.Article),
	}

	res = mapi.Call(mapi.Rss.ListRSS, opt)
	rss := res.Data["rss"].(*model.Rss)
	r.WriteHeader(200)
	r.Header().Set("Content-Type", "application/rss+xml;charset=UTF-8")
	r.Write([]byte(rss.String()))
}
Example #7
0
func (s *Sitemap) Get() {
	res := mapi.Call(mapi.Article.ListArchive, nil)
	if !res.Status {
		panic(errors.New(res.Error))
		return
	}
	opt := &mapi.SiteMapOption{
		Setting:  s.GetGeneral(),
		Articles: res.Data["articles"].([]*model.Article),
	}

	res = mapi.Call(mapi.Rss.ListSitemap, opt)
	rss := res.Data["sitemap"].(*model.SiteMapGroup)
	s.WriteHeader(200)
	s.Header().Set("Content-Type", "text/xml; charset=utf-8")
	s.Write([]byte(rss.String()))
}
Example #8
0
func (d *Delete) Post() {
	id := d.FormInt64("id")
	res := mapi.Call(mapi.Article.Remove, id)
	if !res.Status {
		d.RenderError(500, errors.New(res.Error))
		return
	}
	d.Redirect("/admin/article")
}
Example #9
0
func (a *Archive) Get() {
	a.Title("Archive")
	res := mapi.Call(mapi.Article.ListArchive, nil)
	if !res.Status {
		a.RenderError(500, errors.New(res.Error))
		return
	}
	a.Assign("Articles", res.Data["articles"].([]*model.Article))
	a.Render("archive.tmpl")
}
Example #10
0
func (w *Write) postPage() {
	form := new(mapi.PageForm)
	if err := w.Bind(form); err != nil {
		w.ServeJson(mapi.Fail(err))
		return
	}
	form.AuthorId = w.AuthUser.Id
	res := mapi.Call(mapi.Page.Write, form)
	w.ServeJson(res)
}
Example #11
0
func (p *Page) Get() {
	opt := &mapi.PageListOption{
		Page: p.FormInt64("page", 1),
	}
	res := mapi.Call(mapi.Page.List, opt)
	if !res.Status {
		panic(res.Error)
	}
	p.Assign("Pages", res.Data["pages"].([]*model.Page))
	p.Assign("Pager", res.Data["pager"].(*utils.Pager))
	p.Title("Page")
	p.Render("page.tmpl")
}
Example #12
0
func (c *Comment) Post() {
	// only support ajax
	if strings.ToLower(c.Req().Header.Get("X-Requested-With")) != "xmlhttprequest" {
		c.WriteHeader(400)
		return
	}
	form := new(mapi.CommentForm)
	if err := c.Bind(form); err != nil {
		c.ServeJson(mapi.Fail(err))
		return
	}
	form.For = c.Param("from")
	form.ForId = c.ParamInt64("id")
	// filter comment data
	if res := mapi.Call(mapi.Comment.Filter, form); !res.Status {
		c.ServeJson(res)
		return
	}
	// save comment
	res := mapi.Call(mapi.Comment.Save, form)
	c.ServeJson(res)
}
Example #13
0
func (a *Article) Get() {
	a.Title("Article")
	opt := &mapi.ArticleListOption{
		Page: a.FormInt64("page", 1),
	}
	res := mapi.Call(mapi.Article.List, opt)
	if !res.Status {
		panic(res.Error)
	}
	a.Assign("Articles", res.Data["articles"].([]*model.Article))
	a.Assign("Pager", res.Data["pager"].(*utils.Pager))
	a.Render("article.tmpl")
}
Example #14
0
func (a *Article) Get() {
	param, err := mapi.Article.ParseRoute(blogParamRule, a.Param("*article"))
	if err != nil {
		a.RenderError(500, err)
		return
	}

	// get article
	res := mapi.Call(mapi.Article.Get, param)
	if res.Status {
		article := res.Data["article"].(*model.Article)
		if article.Status != model.ARTICLE_STATUS_PUBLISH {
			a.RenderError(404, nil)
			return
		}
		a.Assign("Article", article)
		a.Assign("Title", article.Title+" - "+a.GetGeneralByKey("title"))

		// get comments
		opt := &mapi.CommentListOption{
			ArticleId: article.Id,
			Page:      1,
			Size:      999,
			Status:    model.COMMENT_STATUS_APPROVED,
		}
		res = mapi.Call(mapi.Comment.List, opt)
		if res.Status {
			comments := res.Data["comments"].([]*model.Comment)
			a.Assign("Comments", comments)
		}
		prepareComment(article, a.ThemeRender)
		a.Assign("XsrfHtml", a.XsrfFormHtml())
		a.Render("article.tmpl")
		return
	}

	// not found
	a.RenderError(404, nil)
}
Example #15
0
// ajax callback
func (w *Write) Post() {
	if w.Form("type") == "page" {
		w.postPage()
		return
	}
	form := new(mapi.ArticleForm)
	if err := w.Bind(form); err != nil {
		w.ServeJson(mapi.Fail(err))
		return
	}
	form.AuthorId = w.AuthUser.Id

	res := mapi.Call(mapi.Article.Write, form)
	w.ServeJson(res)
}
Example #16
0
func (m *Media) Get() {
	opt := &mapi.MediaListOption{
		Page: m.FormInt64("page", 1),
		Size: 10,
	}
	res := mapi.Call(mapi.Media.List, opt)
	if !res.Status {
		m.RenderError(500, errors.New(res.Error))
		return
	}
	m.Assign("Media", res.Data["media"].([]*model.Media))
	m.Assign("Pager", res.Data["pager"].(*utils.Pager))
	m.Title("Media")
	m.Render("media.tmpl")
}
Example #17
0
func (s *Setting) postMedia() {
	form := new(mapi.SettingMediaForm)
	if err := s.Bind(form); err != nil {
		s.Assign("MediaError", err.Error())
		s.Get()
		return
	}
	res := mapi.Call(mapi.Setting.SaveMedia, form)
	if !res.Status {
		s.Assign("MediaError", res.Error)
		s.Get()
		return
	}
	s.Assign("MediaSuccess", true)
	s.Redirect("/admin/setting#setting-media")
}
Example #18
0
func (s *Setting) postGeneral() {
	form := new(mapi.SettingGeneralForm)
	if err := s.Bind(form); err != nil {
		s.Assign("Error", err.Error())
		s.Get()
		return
	}
	res := mapi.Call(mapi.Setting.SaveGeneral, form)
	if !res.Status {
		s.Assign("Error", res.Error)
		s.Get()
		return
	}
	s.Assign("Success", true)
	s.Get()
}
Example #19
0
func (p *Profile) Post() {
	form := new(mapi.UserProfileForm)
	if err := p.Bind(form); err != nil {
		p.Assign("Error", err.Error())
		p.Get()
		return
	}
	form.Id = p.AuthUser.Id
	res := mapi.Call(mapi.User.UpdateProfile, form)
	if !res.Status {
		p.Assign("Error", res.Error)
	} else {
		p.SetAuthUser(res.Data["user"].(*model.User))
		p.Assign("Success", true)
	}
	p.Get()
}
Example #20
0
func (md *MediaDelete) Get() {
	if !strings.Contains(md.Req().Referer(), "/admin/media") {
		md.RenderError(401, nil)
		return
	}
	id := md.FormInt64("id", 0)
	if id < 1 {
		md.Redirect("/admin/media")
		return
	}
	res := mapi.Call(mapi.Media.Remove, id)
	if !res.Status {
		md.RenderError(500, errors.New(res.Error))
		return
	}
	md.Redirect("/admin/media")
	return
}
Example #21
0
func (p *Page) Get() {
	param, err := mapi.Page.ParseRoute(pageParamRule, p.Param("*page"))
	if err != nil {
		p.RenderError(500, err)
		return
	}
	res := mapi.Call(mapi.Page.Get, param)
	if res.Status {
		page := res.Data["page"].(*model.Page)
		if page.Status != model.PAGE_STATUS_PUBLISH {
			p.RenderError(404, nil)
			return
		}
		p.Assign("Page", page)
		p.Title(page.Title + " - " + p.GetGeneralByKey("title"))
		p.Render("page.tmpl")
		return
	}
	p.RenderError(404, nil)
}
Example #22
0
func (p *Password) Post() {
	form := new(mapi.UserPasswordForm)
	if err := p.Bind(form); err != nil {
		p.Assign("PasswordError", err.Error())
		p.Title("Profile")
		p.Assign("User", p.AuthUser)
		p.Render("profile.tmpl")
		return
	}
	form.User = p.AuthUser

	res := mapi.Call(mapi.User.UpdatePassword, form)
	if !res.Status {
		p.Assign("PasswordError", res.Error)
	} else {
		p.Assign("PasswordSuccess", true)
	}
	p.Title("Profile")
	p.Assign("User", p.AuthUser)
	p.Render("profile.tmpl")
}
Example #23
0
func (idx *Index) Get() {
	page := idx.ParamInt64(":page", 1)
	if page > 1 {
		idx.Assign("Title", idx.GetGeneralByKey("title")+" - Page "+strconv.FormatInt(page, 10))
	}
	opt := &mapi.ArticleListOption{
		Page:   page,
		Size:   4,
		Status: model.ARTICLE_STATUS_PUBLISH,
	}
	res := mapi.Call(mapi.Article.List, opt)
	if !res.Status {
		idx.RenderError(500, errors.New(res.Error))
		return
	}
	pager := res.Data["pager"].(*utils.Pager)
	if pager.Current > pager.Pages {
		idx.RenderError(404, nil)
		return
	}
	idx.Assign("Articles", res.Data["articles"].([]*model.Article))
	idx.Assign("Pager", pager)
	idx.Render("index.tmpl")
}