2 paster -- paste service
3 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 "go.cypherpunks.ru/netstring/v2"
34 "lukechampine.com/blake3"
40 //go:embed asciicast.tmpl
41 ASCIICastHTMLTmplRaw string
42 ASCIICastHTMLTmpl = template.Must(template.New("asciicast").Parse(
47 func fatal(s string) {
52 func asciicastHTML(playerPath, cast string) error {
54 err := ASCIICastHTMLTmpl.Execute(&buf, struct {
58 PlayerPath: playerPath,
65 fd, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
69 if _, err = fd.Write(buf.Bytes()); err != nil {
77 maxSize := flag.Uint64("max-size", 1<<20, "Maximal upload size")
78 asciicastPath := flag.String("asciicast-path", "", "Generate HTMLs for .cast asciicasts, specify \"asciinema-player-v2.6.1\"")
80 fmt.Fprintf(os.Stderr, "Usage: paster [options] URL [URL ...]\n")
84 if len(flag.Args()) == 0 {
88 r := netstring.NewReader(os.Stdin)
94 fatal("too long extension length")
96 data, err := io.ReadAll(r)
104 ext = "." + string(data)
116 rnd := make([]byte, 12)
117 if _, err = io.ReadFull(rand.Reader, rnd); err != nil {
120 fn := "." + base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rnd) + ext
121 fd, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
125 h := blake3.New(32, nil)
126 bfd := bufio.NewWriter(fd)
127 mw := io.MultiWriter(bfd, h)
128 buf := make([]byte, 1)
129 _, err = io.CopyN(mw, r, int64(size-1))
137 _, err = mw.Write(buf)
141 if (ext == ".txt" || ext == ".url") && buf[0] != '\n' {
142 err = bfd.WriteByte('\n')
155 err = os.Rename(fn, fn[1:])
159 for _, u := range flag.Args() {
160 fmt.Println(u + fn[1:])
162 fmt.Println("BLAKE3-256:", hex.EncodeToString(h.Sum(nil)))
163 if ext == ".cast" && *asciicastPath != "" {
164 if err = asciicastHTML(*asciicastPath, fn[1:]); err != nil {
167 for _, u := range flag.Args() {
168 fmt.Println(u + fn[1:] + ".html")
174 os.Getenv("TCPREMOTEIP"),
175 os.Getenv("TCPREMOTEPORT"),