示例#1
0
文件: post.go 项目: petar/blog
// ☻ Parse a post file
func loadPost(c *fs.Context, name string, req *http.Request) (meta *PostData, article string, err error) {
	meta = &PostData{
		Name:       name,
		Title:      "¿Title?",
		PlusAuthor: config.PlusID,
		PlusAPIKey: config.PlusKey,
		HostURL:    hostURL(req),
	}

	art, fi, err := c.Read(name)
	if err != nil {
		return nil, "", err
	}
	if bytes.HasPrefix(art, []byte("{\n")) {
		i := bytes.Index(art, []byte("\n}\n"))
		if i < 0 {
			panic("cannot find end of json metadata")
		}
		hdr, rest := art[:i+3], art[i+3:]
		if err := json.Unmarshal(hdr, meta); err != nil {
			panic(fmt.Sprintf("loading %s: %s", name, err))
		}
		art = rest
	}
	meta.FileModTime = fi.ModTime
	meta.FileSize = fi.Size

	return meta, replacer.Replace(string(art)), nil
}
示例#2
0
文件: play.go 项目: 0x7cc/rsc
func loadSize(ctxt *fs.Context, name string, max int) *image.RGBA {
	data, _, err := ctxt.Read("qr/upload/" + name + ".png")
	if err != nil {
		panic(err)
	}
	i, _, err := image.Decode(bytes.NewBuffer(data))
	if err != nil {
		panic(err)
	}
	b := i.Bounds()
	dx, dy := max, max
	if b.Dx() > b.Dy() {
		dy = b.Dy() * dx / b.Dx()
	} else {
		dx = b.Dx() * dy / b.Dy()
	}
	var irgba *image.RGBA
	switch i := i.(type) {
	case *image.RGBA:
		irgba = resize.ResizeRGBA(i, i.Bounds(), dx, dy)
	case *image.NRGBA:
		irgba = resize.ResizeNRGBA(i, i.Bounds(), dx, dy)
	}
	return irgba
}
示例#3
0
文件: play.go 项目: 0x7cc/rsc
func flag(w http.ResponseWriter, req *http.Request, img string, ctxt *fs.Context) {
	if !isImgName(img) && !isTagName(img) {
		fmt.Fprintf(w, "Invalid image.\n")
		return
	}
	data, _, _ := ctxt.Read("qr/flag/" + img)
	data = append(data, '!')
	ctxt.Write("qr/flag/"+img, data)

	fmt.Fprintf(w, "Thank you.  The image has been reported.\n")
}
示例#4
0
文件: post.go 项目: petar/blog
func mainTemplate(c *fs.Context) *template.Template {
	t := template.New("main")
	t.Funcs(funcMap)

	main, _, err := c.Read("blog/main.html")
	if err != nil {
		panic(err)
	}
	style, _, _ := c.Read("blog/style.html")
	main = append(main, style...)
	_, err = t.Parse(string(main))
	if err != nil {
		panic(err)
	}
	return t
}
示例#5
0
文件: play.go 项目: 0x7cc/rsc
func runTemplate(c *fs.Context, w http.ResponseWriter, name string, data interface{}) {
	t := template.New("main")

	main, _, err := c.Read(name)
	if err != nil {
		panic(err)
	}
	style, _, _ := c.Read("style.html")
	main = append(main, style...)
	_, err = t.Parse(string(main))
	if err != nil {
		panic(err)
	}

	var buf bytes.Buffer
	if err := t.Execute(&buf, &data); err != nil {
		panic(err)
	}
	w.Write(buf.Bytes())
}
示例#6
0
文件: post.go 项目: riquito/rsc
func oldRedirect(ctxt *fs.Context, w http.ResponseWriter, req *http.Request, p string) {
	m := map[string]string{}
	if key, ok := ctxt.CacheLoad("blog:oldRedirectMap", "blog/post", &m); !ok {
		dir, err := ctxt.ReadDir("blog/post")
		if err != nil {
			panic(err)
		}

		for _, d := range dir {
			meta, _, err := loadPost(ctxt, d.Name, req)
			if err != nil {
				// Should not happen: we just listed the directory.
				panic(err)
			}
			m[meta.OldURL] = "/" + d.Name
		}

		ctxt.CacheStore(key, m)
	}

	if url, ok := m[p]; ok {
		http.Redirect(w, req, url, http.StatusFound)
		return
	}

	notfound(ctxt, w, req)
}
示例#7
0
文件: post.go 项目: petar/blog
func readDir(c *fs.Context, root string) ([]proto.FileInfo, error) {
	return c.ReadDir(root)
}
示例#8
0
文件: dash.go 项目: 0x7cc/rsc
func Update(ctxt *fs.Context, client *http.Client, version string) error {
	prefix := strings.Map(func(r rune) rune {
		if 'A' <= r && r <= 'Z' {
			return r - 'A' + 'a'
		}
		if 'a' <= r && r <= 'z' || '0' <= r && r <= '9' {
			return r
		}
		return -1
	}, version)

	label := strings.Map(func(r rune) rune {
		if r == ' ' {
			return -1
		}
		return r
	}, version)

	graphFile := "/issue-dashboard/" + prefix + ".graph"
	htmlFile := "/issue-dashboard/" + label

	var graph []Point
	data, _, err := ctxt.Read(graphFile)
	if err == nil {
		if err := json.Unmarshal(data, &graph); err != nil {
			return fmt.Errorf("unmarshal dashboard graph: %v", err)
		}
	}

	yes, err := issue.Search("go", "open", "label:"+label, false, client)
	if err != nil {
		return fmt.Errorf("searching for %s issues: %v", version, err)
	}
	maybe, err := issue.Search("go", "open", "label:"+label+"Maybe", false, client)
	if err != nil {
		return fmt.Errorf("searching for %sMaybe issues: %v", label, err)
	}

	graph = append(graph, Point{time.Now(), len(yes), len(maybe)})
	data, err = json.Marshal(graph)
	if err != nil {
		return fmt.Errorf("marshal dashboard graph: %v", err)
	}
	if err := ctxt.Write(graphFile, data); err != nil {
		return fmt.Errorf("writing dashboard graph: %v", err)
	}

	byDir := map[string][]*issue.Issue{}
	for _, p := range append(yes, maybe...) {
		dir := p.Summary
		if i := strings.Index(dir, ":"); i >= 0 {
			dir = dir[:i]
		}
		if i := strings.Index(dir, ","); i >= 0 {
			dir = dir[:i]
		}
		byDir[dir] = append(byDir[dir], p)
	}

	var small []Point
	now := time.Now()
	day := -1
	for _, p := range graph {
		if p.Maybe == 0 {
			continue
		}
		d := p.Time.Day()
		if d != day || now.Sub(p.Time) < 3*24*time.Hour {
			day = d
			small = append(small, p)
		}
	}

	tmplData := struct {
		Version string
		Label   string
		Graph   []Point
		Issues  map[string][]*issue.Issue
	}{
		Version: version,
		Label:   label,
		Graph:   small,
		Issues:  byDir,
	}

	var buf bytes.Buffer
	tmpl, err := template.New("main").
		Funcs(template.FuncMap{
			"hasLabel":  hasLabel,
			"hasStatus": hasStatus,
		}).
		Parse(dashTemplate)
	if err != nil {
		return fmt.Errorf("parsing template: %v", err)
	}
	if err := tmpl.Execute(&buf, &tmplData); err != nil {
		return fmt.Errorf("executing template: %v", err)
	}
	if err := ctxt.Write(htmlFile, buf.Bytes()); err != nil {
		return fmt.Errorf("writing html: %v", err)
	}
	return nil
}