Esempio n. 1
0
func Rss(ctx *GoInk.Context) {
	baseUrl := model.GetSetting("site_url")
	article, _ := model.GetPublishArticleList(1, 20)
	author := model.GetUsersByRole("ADMIN")[0]

	articleMap := make([]map[string]string, len(article))
	for i, a := range article {
		m := make(map[string]string)
		m["Title"] = a.Title
		m["Link"] = strings.Replace(baseUrl+a.Link(), baseUrl+"/", baseUrl, -1)
		m["Author"] = author.Nick
		str := utils.Markdown2Html(a.Content())
		str = strings.Replace(str, `src="/`, `src="`+strings.TrimSuffix(baseUrl, "/")+"/", -1)
		str = strings.Replace(str, `href="/`, `href="`+strings.TrimSuffix(baseUrl, "/")+"/", -1)
		m["Desc"] = str
		m["Created"] = time.Unix(a.CreateTime, 0).Format(time.RFC822)
		articleMap[i] = m
	}

	ctx.ContentType("application/rss+xml;charset=UTF-8")

	bytes, e := ctx.App().View().Render("rss.xml", map[string]interface{}{
		"Title":    model.GetSetting("site_title"),
		"Link":     baseUrl,
		"Desc":     model.GetSetting("site_description"),
		"Created":  time.Unix(utils.Now(), 0).Format(time.RFC822),
		"Articles": articleMap,
	})
	if e != nil {
		panic(e)
	}
	ctx.Body = bytes
}
Esempio n. 2
0
// CreateComment creates a comment and links it to the cid content.
func CreateComment(cid int, c *Comment) {
	commentMaxId += Storage.TimeInc(4)
	c.Id = commentMaxId
	c.CreateTime = utils.Now()
	c.Status = "check"
	c.Cid = cid
	// escape content
	c.Content = strings.Replace(utils.Html2str(template.HTMLEscapeString(c.Content)), "\n", "<br/>", -1)
	// if empty url, use # instead.
	if c.Url == "" {
		c.Url = "#"
	}
	// if admin comment, must be approved.
	if c.IsAdmin {
		c.Status = "approved"
	} else {
		// if common comment, get reader status for checking status.
		r := c.GetReader()
		if r != nil {
			if r.Active {
				c.Status = "approved"
			}
		} else {
			CreateReader(c)
		}
	}
	// update comment memory data
	comments[c.Id] = c
	commentsIndex = append([]int{c.Id}, commentsIndex...)
	// append to content
	content := GetContentById(cid)
	content.Comments = append(content.Comments, c)
	go SyncContent(content)
}
Esempio n. 3
0
func Feed(context *GoInk.Context) {
	baseUrl := model.GetSetting("site_url")
	article, _ := model.GetArticleList(1, 20)
	feed := new(feeds.Feed)
	feed.Title = model.GetSetting("site_title")
	feed.Link = &feeds.Link{Href: baseUrl}
	feed.Description = model.GetSetting("site_description")
	author := model.GetUsersByRole("ADMIN")[0]
	feed.Author = &feeds.Author{author.Nick, author.Email}
	feed.Items = make([]*feeds.Item, 0)
	var create int64
	if len(article) > 0 {
		create = article[0].EditTime
	} else {
		create = utils.Now()
	}
	feed.Created = time.Unix(create, 0)
	for _, a := range article {
		item := new(feeds.Item)
		item.Title = a.Title
		item.Link = &feeds.Link{Href: path.Join(baseUrl, a.Link())}
		item.Author = feed.Author
		item.Created = time.Unix(a.CreateTime, 0)
		item.Description = utils.Html2str(a.Summary())
		feed.Items = append(feed.Items, item)
	}
	str, e := feed.ToRss()
	if e != nil {
		panic(e)
	}
	context.ContentType("application/rss+xml;charset=UTF-8")
	context.Body = []byte(str)
}
Esempio n. 4
0
// SaveContent saves changed content.
// It will re-generate related indexes.
func SaveContent(c *Content) {
	c.EditTime = utils.Now()
	// clean rendered cache text
	c.textRendered = ""
	generatePublishArticleIndex()
	go SyncContent(c)
}
Esempio n. 5
0
func CreateComment(cid int, c *Comment) {
	commentMaxId += Storage.TimeInc(4)
	c.Id = commentMaxId
	c.CreateTime = utils.Now()
	c.Status = "check"
	c.Cid = cid
	if c.Url == "" {
		c.Url = "#"
	}
	if c.IsAdmin {
		c.Status = "approved"
	} else {
		r := c.GetReader()
		if r != nil {
			if r.Active {
				c.Status = "approved"
			}
		} else {
			CreateReader(c)
		}
	}
	// update comment memory data
	comments[c.Id] = c
	commentsIndex = append([]int{c.Id}, commentsIndex...)
	// append to content
	content := GetContentById(cid)
	content.Comments = append(content.Comments, c)
	go SyncContent(content)
}
Esempio n. 6
0
func RecycleMessages() {
	for i, m := range messages {
		if m.CreateTime+3600*24*3 < utils.Now() {
			messages = messages[:i]
			return
		}
	}
}
Esempio n. 7
0
func CreateFile(f *File) *File {
	fileMaxId += Storage.TimeInc(3)
	f.Id = fileMaxId
	f.UploadTime = utils.Now()
	f.IsUsed = true
	f.Hits = 0
	files = append([]*File{f}, files...)
	go SyncFiles()
	return f
}
Esempio n. 8
0
// create new token from user and context.
func CreateToken(u *User, context *GoInk.Context, expire int64) *Token {
	t := new(Token)
	t.UserId = u.Id
	t.CreateTime = utils.Now()
	t.ExpireTime = t.CreateTime + expire
	t.Value = utils.Sha1(fmt.Sprintf("%s-%s-%d-%d", context.Ip, context.UserAgent, t.CreateTime, t.UserId))
	tokens[t.Value] = t
	go SyncTokens()
	return t
}
Esempio n. 9
0
// create new user.
func CreateUser(u *User) error {
	if GetUserByName(u.Email) != nil {
		return errors.New("email-repeat")
	}
	userMaxId += Storage.TimeInc(5)
	u.Id = userMaxId
	u.CreateTime = utils.Now()
	u.LastLoginTime = u.CreateTime
	users = append(users, u)
	go SyncUsers()
	return nil
}
Esempio n. 10
0
func CreateMessage(tp string, data interface{}) *Message {
	m := new(Message)
	m.Type = tp
	m.Data = messageGenerator[tp](data)
	if m.Data == "" {
		println("message generator returns empty")
		return nil
	}
	m.CreateTime = utils.Now()
	m.IsRead = false
	messageMaxId += Storage.TimeInc(3)
	m.Id = messageMaxId
	messages = append([]*Message{m}, messages...)
	SyncMessages()
	return m
}
Esempio n. 11
0
// create new content.
func CreateContent(c *Content, t string) (*Content, error) {
	c2 := GetContentBySlug(c.Slug)
	if c2 != nil {
		return nil, errors.New("slug-repeat")
	}
	contentMaxId += Storage.TimeInc(3)
	c.Id = contentMaxId
	c.CreateTime = utils.Now()
	c.EditTime = c.CreateTime
	c.UpdateTime = c.CreateTime
	c.Comments = make([]*Comment, 0)
	c.Type = t
	c.Hits = 1
	contents[c.Id] = c
	contentsIndex[c.Type] = append([]int{c.Id}, contentsIndex[c.Type]...)
	go SyncContent(c)
	return c, nil
}
Esempio n. 12
0
func SiteMap(ctx *GoInk.Context) {
	baseUrl := model.GetSetting("site_url")
	println(baseUrl)
	article, _ := model.GetPublishArticleList(1, 50)
	navigators := model.GetNavigators()
	now := time.Unix(utils.Now(), 0).Format(time.RFC3339)

	articleMap := make([]map[string]string, len(article))
	for i, a := range article {
		m := make(map[string]string)
		m["Link"] = strings.Replace(baseUrl+a.Link(), baseUrl+"/", baseUrl, -1)
		m["Created"] = time.Unix(a.CreateTime, 0).Format(time.RFC3339)
		articleMap[i] = m
	}

	navMap := make([]map[string]string, 0)
	for _, n := range navigators {
		m := make(map[string]string)
		if n.Link == "/" {
			continue
		}
		if strings.HasPrefix(n.Link, "/") {
			m["Link"] = strings.Replace(baseUrl+n.Link, baseUrl+"/", baseUrl, -1)
		} else {
			m["Link"] = n.Link
		}
		m["Created"] = now
		navMap = append(navMap, m)
	}

	ctx.ContentType("text/xml")
	bytes, e := ctx.App().View().Render("sitemap.xml", map[string]interface{}{
		"Title":      model.GetSetting("site_title"),
		"Link":       baseUrl,
		"Created":    now,
		"Articles":   articleMap,
		"Navigators": navMap,
	})
	if e != nil {
		panic(e)
	}
	ctx.Body = bytes

}
Esempio n. 13
0
func DoInstall() {
	// origin from https://github.com/wendal/gor/blob/master/gor/gor.go
	decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(zipBytes))
	b, _ := ioutil.ReadAll(decoder)
	ioutil.WriteFile(tmpZipFile, b, os.ModePerm)
	z, e := zip.Open(tmpZipFile)
	if e != nil {
		panic(e)
		os.Exit(1)
	}
	z.ExtractTo("")
	defer func() {
		z.Close()
		decoder = nil
		os.Remove(tmpZipFile)
	}()
	ioutil.WriteFile(installLockFile, []byte(fmt.Sprint(utils.Now())), os.ModePerm)
	println("install success")
}
Esempio n. 14
0
// check token is valid or expired.
func (t *Token) IsValid() bool {
	if GetUserById(t.UserId) == nil {
		return false
	}
	return t.ExpireTime > utils.Now()
}
Esempio n. 15
0
func CreateFilePath(dir string, f *File) string {
	os.MkdirAll(dir, os.ModePerm)
	name := utils.DateInt64(utils.Now(), "YYYYMMDDHHmmss")
	name += strconv.Itoa(Storage.TimeInc(10)) + path.Ext(f.Name)
	return path.Join(dir, name)
}
Esempio n. 16
0
// LogErrors logs error bytes to tmp/log directory.
func LogError(bytes []byte) {
	dir := App.Config().String("app.log_dir")
	file := path.Join(dir, utils.DateInt64(utils.Now(), "MMDDHHmmss.log"))
	ioutil.WriteFile(file, bytes, os.ModePerm)
}
Esempio n. 17
0
func writeDefaultData() {
	// write user
	u := new(User)
	u.Id = Storage.TimeInc(10)
	u.Name = "admin"
	u.Password = utils.Sha1("adminxxxxx")
	u.Nick = "管理员"
	u.Email = "*****@*****.**"
	u.Url = "http://example.com/"
	u.CreateTime = utils.Now()
	u.Bio = "这是站点的管理员,你可以添加一些个人介绍,支持换行不支持markdown"
	u.LastLoginTime = u.CreateTime
	u.Role = "ADMIN"
	Storage.Set("users", []*User{u})

	// write token
	Storage.Set("tokens", map[string]*Token{})

	// write contents
	a := new(Content)
	a.Id = Storage.TimeInc(9)
	a.Title = "欢迎使用 Fxh.Go"
	a.Slug = "welcome-fxh-go"
	a.Text = "如果您看到这篇文章,表示您的 blog 已经安装成功."
	a.Tags = []string{"Fxh.Go"}
	a.CreateTime = utils.Now()
	a.EditTime = a.CreateTime
	a.UpdateTime = a.CreateTime
	a.IsComment = true
	a.IsLinked = false
	a.AuthorId = u.Id
	a.Type = "article"
	a.Status = "publish"
	a.Format = "markdown"
	a.Template = "blog.html"
	a.Hits = 1
	// write comments
	co := new(Comment)
	co.Author = u.Nick
	co.Email = u.Email
	co.Url = u.Url
	co.Content = "欢迎加入使用 Fxh.Go"
	co.Avatar = utils.Gravatar(co.Email, "50")
	co.Pid = 0
	co.Ip = "127.0.0.1"
	co.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17"
	co.IsAdmin = true
	co.Id = Storage.TimeInc(7)
	co.CreateTime = utils.Now()
	co.Status = "approved"
	co.Cid = a.Id
	a.Comments = []*Comment{co}
	Storage.Set("content/article-"+strconv.Itoa(a.Id), a)

	// write pages
	p := new(Content)
	p.Id = a.Id + Storage.TimeInc(6)
	p.Title = "关于"
	p.Slug = "about-me"
	p.Text = "本页面由 Fxh.Go 创建, 这只是个测试页面."
	p.Tags = []string{}
	p.CreateTime = utils.Now()
	p.EditTime = p.CreateTime
	p.UpdateTime = p.UpdateTime
	p.IsComment = true
	p.IsLinked = true
	p.AuthorId = u.Id
	p.Type = "page"
	p.Status = "publish"
	p.Format = "markdown"
	p.Comments = make([]*Comment, 0)
	p.Template = "page.html"
	p.Hits = 1
	Storage.Set("content/page-"+strconv.Itoa(p.Id), p)
	p2 := new(Content)
	p2.Id = p.Id + Storage.TimeInc(6)
	p2.Title = "好友"
	p2.Slug = "friends"
	p2.Text = "本页面由 Fxh.Go 创建, 这只是个测试页面."
	p2.Tags = []string{}
	p2.CreateTime = utils.Now()
	p2.EditTime = p2.CreateTime
	p2.UpdateTime = p2.UpdateTime
	p2.IsComment = true
	p2.IsLinked = true
	p2.AuthorId = u.Id
	p2.Type = "page"
	p2.Status = "publish"
	p2.Format = "markdown"
	p2.Comments = make([]*Comment, 0)
	p2.Template = "page.html"
	p2.Hits = 1
	Storage.Set("content/page-"+strconv.Itoa(p2.Id), p2)

	// write new reader
	Storage.Set("readers", map[string]*Reader{})

	// write version
	v := new(version)
	v.Name = "Fxh.Go"
	v.BuildTime = utils.Now()
	v.Version = 20140116
	Storage.Set("version", v)

	// write settings
	s := map[string]string{
		"site_title":       "Fxh.Go",
		"site_sub_title":   "Go开发的简单博客",
		"site_keywords":    "Fxh.Go,Golang,Blog",
		"site_description": "Go语言开发的简单博客程序",
		"site_url":         "http://localhost/",
		"article_size":     "4",
		"c_footer_weibo":   "#",
		"c_footer_github":  "#",
		"c_footer_email":   "#",
		"c_home_avatar":    "/static/img/site.png",
	}
	Storage.Set("settings", s)

	// write files
	Storage.Set("files", []*File{})
}
Esempio n. 18
0
func (jss *jsonStorage) TimeInc(d int) int {
	return int(utils.Now())%d + 1
}
Esempio n. 19
0
func DoInstall() {
	ExtractBundleBytes()
	ioutil.WriteFile(installLockFile, []byte(fmt.Sprint(utils.Now())), os.ModePerm)
	println("install success")
}
Esempio n. 20
0
// save changed content.
func SaveContent(c *Content) {
	c.EditTime = utils.Now()
	go SyncContent(c)
}