]> Sergey Matveev's repositories - sgblog.git/commitdiff
Serve /img/ on gemini
authorSergey Matveev <stargrave@stargrave.org>
Sun, 30 Jul 2023 11:44:09 +0000 (14:44 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 30 Jul 2023 12:00:32 +0000 (15:00 +0300)
cmd/sgblog/gemini-entry.tmpl
cmd/sgblog/gemini.go
common.go

index 67d6fc41902a8764e7504408c6e6eafcef57cb1e..abbca9e7d2f57c4e98c3049776adab940005bace 100644 (file)
@@ -1,4 +1,4 @@
-{{$Cfg := .Cfg -}}{{$CR := printf "\r"}}20 text/gemini{{$CR}}
+{{$CR := printf "\r"}}20 text/gemini{{$CR}}
 # {{.Title}}
 {{$.T.Get "What"}}: {{.Commit.Hash.String}}
 {{$.T.Get "When"}}: {{.When}}
@@ -12,7 +12,7 @@
 ```
 {{end -}}
 {{- if .Images}}
-{{range $idx, $img := .Images}}=> http://{{$Cfg.ImgDomain}}/{{$img.Path}} {{$img.Alt}}
+{{range $idx, $img := .Images}}=> /img/{{$img.Path}} {{$img.Alt}}
 {{end}}
 {{end -}}
 {{- if .Cfg.CommentsEmail}}
index 527874eae26488228777311f4369c0745da12d73..ccf719d94523025961c04748b705bd0d823e5b80 100644 (file)
@@ -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"))
        }
index 9a33a4a64dd025d46eef30c07c5384b214f82e06..1f5984a31d3a8a1907d1334bd6276c89a52cf713 100644 (file)
--- a/common.go
+++ b/common.go
@@ -15,7 +15,7 @@ import (
 )
 
 const (
-       Version = "0.29.0"
+       Version = "0.30.0"
        WhenFmt = "2006-01-02 15:04:05Z07:00"
 )