func loadPost(c *fs.Context, name string, req *http.Request) (meta *PostData, article string, err error) { meta = &PostData{ Name: name, Title: "TITLE HERE", PlusAuthor: plusRsc, PlusAPIKey: plusKey, HostURL: hostURL(req), } art, fi, err := c.Read("blog/post/" + 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 }
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 }
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") }
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 }
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()) }
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) }