]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/gemini.go
Serve /img/ on gemini
[sgblog.git] / cmd / sgblog / gemini.go
index 5a2f9a349d599acb78b2f6f9ca7dfbeac1f4f6df..ccf719d94523025961c04748b705bd0d823e5b80 100644 (file)
@@ -1,6 +1,6 @@
 /*
 SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
@@ -26,12 +26,15 @@ import (
        "log"
        "net/url"
        "os"
+       "path"
        "strconv"
+       "strings"
        "text/template"
 
        "github.com/go-git/go-git/v5"
        "github.com/go-git/go-git/v5/plumbing"
        "github.com/go-git/go-git/v5/plumbing/object"
+       "github.com/vorlif/spreak"
        "go.stargrave.org/sgblog"
 )
 
@@ -55,6 +58,7 @@ func serveGemini(cfgPath string) {
        if err != nil {
                log.Fatalln(err)
        }
+       initLocalizer(cfg.Lang)
 
        headHash, err := initRepo(cfg)
        if err != nil {
@@ -129,9 +133,10 @@ func serveGemini(cfgPath string) {
                        }
                        lines := msgSplit(commit.Message)
                        entries = append(entries, TableMenuEntry{
-                               Commit:   commit,
-                               Title:    lines[0],
-                               LinesNum: len(lines) - 2,
+                               Commit:    commit,
+                               Title:     lines[0],
+                               LinesNum:  len(lines) - 2,
+                               ImagesNum: len(listImgs(cfg, commit.Hash)),
                                CommentsNum: len(sgblog.ParseComments(sgblog.GetNote(
                                        repo, commentsTree, commit.Hash,
                                ))),
@@ -145,6 +150,7 @@ func serveGemini(cfgPath string) {
                        offsetPrev = 0
                }
                err = TmplGemMenu.Execute(os.Stdout, struct {
+                       T          *spreak.Localizer
                        Cfg        *Cfg
                        Topic      string
                        Offset     int
@@ -155,6 +161,7 @@ func serveGemini(cfgPath string) {
                        Topics     []string
                        Version    string
                }{
+                       T:          localizer,
                        Cfg:        cfg,
                        Topic:      topic,
                        Offset:     offset,
@@ -175,21 +182,25 @@ func serveGemini(cfgPath string) {
                }
                title := msgSplit(commit.Message)[0]
                err = TmplGemEntry.Execute(os.Stdout, struct {
+                       T            *spreak.Localizer
                        Title        string
                        Commit       *object.Commit
                        When         string
                        Cfg          *Cfg
                        Note         string
+                       Images       []Img
                        Comments     []string
                        Topics       []string
                        Version      string
                        TitleEscaped string
                }{
+                       T:        localizer,
                        Title:    title,
                        Commit:   commit,
                        When:     commit.Author.When.Format(sgblog.WhenFmt),
                        Cfg:      cfg,
                        Note:     string(sgblog.GetNote(repo, notesTree, commit.Hash)),
+                       Images:   listImgs(cfg, commit.Hash),
                        Comments: sgblog.ParseComments(sgblog.GetNote(repo, commentsTree, commit.Hash)),
                        Topics:   sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)),
                        Version:  sgblog.Version,
@@ -200,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"))
        }