X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=cmd%2Fsgblog%2Fgemini.go;h=ccf719d94523025961c04748b705bd0d823e5b80;hb=b238d11be5852ecba1b72116545e7c8b2fed6133;hp=527874eae26488228777311f4369c0745da12d73;hpb=fb473fd627f8b146f56e69548998f870ce8f6976;p=sgblog.git diff --git a/cmd/sgblog/gemini.go b/cmd/sgblog/gemini.go index 527874e..ccf719d 100644 --- a/cmd/sgblog/gemini.go +++ b/cmd/sgblog/gemini.go @@ -26,7 +26,9 @@ import ( "log" "net/url" "os" + "path" "strconv" + "strings" "text/template" "github.com/go-git/go-git/v5" @@ -209,6 +211,24 @@ func serveGemini(cfgPath string) { if err != nil { log.Fatalln(err) } + } else if strings.HasPrefix(u.Path, "/img/") { + pth := strings.TrimPrefix(u.Path, "/img/") + if strings.Contains(pth, "..") { + log.Fatalln("unacceptable double dots") + } + typ := ImgTypes[path.Ext(pth)] + if typ == "" { + typ = "application/octet-stream" + } + fd, err := os.Open(path.Join(cfg.ImgPath, pth)) + if err != nil { + log.Fatalln(err) + } + bw := bufio.NewWriter(os.Stdout) + bw.Write([]byte("20 " + typ + "\r\n")) + io.Copy(bw, bufio.NewReader(fd)) + fd.Close() + bw.Flush() } else { makeGemErr(errors.New("unknown URL action")) }