X-Git-Url: http://www.git.stargrave.org/?p=paster.git;a=blobdiff_plain;f=main.go;fp=main.go;h=89bcc6eabdb6010ae235c37b162fb06aae213f65;hp=139b5366d31f9caf6c75b001de1f671ab365877b;hb=0e006de337398d557b290d727f40e9303a5ababf;hpb=3e7bb594e7f3114c2373f197760cefd4b72010cb diff --git a/main.go b/main.go index 139b536..89bcc6e 100644 --- a/main.go +++ b/main.go @@ -22,22 +22,57 @@ import ( "bytes" "crypto/rand" "crypto/sha512" + _ "embed" "encoding/base32" "encoding/hex" "flag" "fmt" + "html/template" "io" "os" "strconv" ) +var ( + //go:embed asciicast.tmpl + ASCIICastHTMLTmplRaw string + ASCIICastHTMLTmpl = template.Must(template.New("asciicast").Parse( + ASCIICastHTMLTmplRaw, + )) +) + func fatal(s string) { fmt.Println(s) 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() { 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() @@ -150,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",