]> Sergey Matveev's repositories - paster.git/blobdiff - main.go
Texinfo documentation, website and asciicasts
[paster.git] / main.go
diff --git a/main.go b/main.go
index 855fb14f37eae8b6a207e1f1fe6c3f77cd4b5139..89bcc6eabdb6010ae235c37b162fb06aae213f65 100644 (file)
--- a/main.go
+++ b/main.go
@@ -1,6 +1,6 @@
 /*
 paster -- paste service
-Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2021-2022 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -22,14 +22,23 @@ import (
        "bytes"
        "crypto/rand"
        "crypto/sha512"
+       _ "embed"
        "encoding/base32"
        "encoding/hex"
        "flag"
        "fmt"
+       "html/template"
        "io"
        "os"
        "strconv"
-       "time"
+)
+
+var (
+       //go:embed asciicast.tmpl
+       ASCIICastHTMLTmplRaw string
+       ASCIICastHTMLTmpl    = template.Must(template.New("asciicast").Parse(
+               ASCIICastHTMLTmplRaw,
+       ))
 )
 
 func fatal(s string) {
@@ -37,9 +46,33 @@ func fatal(s string) {
        os.Exit(1)
 }
 
+func asciicastHTML(playerPath, cast string) error {
+       var buf bytes.Buffer
+       err := ASCIICastHTMLTmpl.Execute(&buf, struct {
+               PlayerPath string
+               Cast       string
+       }{
+               PlayerPath: playerPath,
+               Cast:       cast,
+       })
+       if err != nil {
+               return err
+       }
+       fn := cast + ".html"
+       fd, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
+       if err != nil {
+               return err
+       }
+       if _, err = fd.Write(buf.Bytes()); err != nil {
+               os.Remove(fn)
+               return err
+       }
+       return fd.Close()
+}
+
 func main() {
-       maxSecs := flag.Uint("max-secs", 60, "Maximal time of aliveness (0=disable)")
        maxSize := flag.Uint64("max-size", 1<<20, "Maximal upload size")
+       asciicastPath := flag.String("asciicast-path", "", "Generate HTMLs for .cast asciicasts, specify \"asciinema-player-v2.6.1\"")
        flag.Usage = func() {
                fmt.Fprintf(os.Stderr, "Usage: paster [options] URL [...]\n")
                flag.PrintDefaults()
@@ -49,12 +82,7 @@ func main() {
                flag.Usage()
                os.Exit(1)
        }
-       if *maxSecs > 0 {
-               go func() {
-                       time.Sleep(time.Duration(*maxSecs) * time.Second)
-                       fatal("max aliveness time is reached")
-               }()
-       }
+       var fn string
        r := bufio.NewReader(os.Stdin)
        b, err := r.ReadByte()
        if err != nil {
@@ -114,7 +142,7 @@ AnotherKey:
        if _, err = io.ReadFull(rand.Reader, rnd); err != nil {
                fatal(err.Error())
        }
-       fn := "." + base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rnd) +
+       fn = "." + base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rnd) +
                ext
        fd, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
        if err != nil {
@@ -157,7 +185,15 @@ AnotherKey:
        for _, u := range flag.Args() {
                fmt.Println(u + fn[1:])
        }
-       fmt.Println(hex.EncodeToString(h.Sum(nil)[:512/2/8]))
+       fmt.Println("SHA512/2:", hex.EncodeToString(h.Sum(nil)[:512/2/8]))
+       if ext == ".cast" && *asciicastPath != "" {
+               if err = asciicastHTML(*asciicastPath, fn[1:]); err != nil {
+                       goto Failed
+               }
+               for _, u := range flag.Args() {
+                       fmt.Println(u + fn[1:] + ".html")
+               }
+       }
        fmt.Fprintf(
                os.Stderr,
                "[%s]:%s %s %d\n",