~erock/pico

pico/wish/pipe/pipe.go -rw-r--r-- 1.2 KiB
845894e7Eric Bower fix(feeds): wrong id for inline content section 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package pipe

import (
	"fmt"
	"io/fs"
	"strconv"
	"strings"
	"time"

	"github.com/charmbracelet/wish"
	"github.com/gliderlabs/ssh"
	"github.com/picosh/pico/wish/send/utils"
)

func Middleware(writeHandler utils.CopyFromClientHandler, ext string) wish.Middleware {
	return func(sshHandler ssh.Handler) ssh.Handler {
		return func(session ssh.Session) {
			_, _, activePty := session.Pty()
			if activePty {
				_ = session.Exit(0)
				_ = session.Close()
				return
			}

			name := strings.TrimSpace(strings.Join(session.Command(), " "))
			postTime := time.Now()

			if name == "" {
				name = fmt.Sprintf("%s%s", strconv.Itoa(int(postTime.UnixNano())), ext)
			}

			result, err := writeHandler.Write(session, &utils.FileEntry{
				Name:     name,
				Filepath: name,
				Mode:     fs.FileMode(0777),
				Size:     0,
				Mtime:    postTime.Unix(),
				Atime:    postTime.Unix(),
				Reader:   session,
			})
			if err != nil {
				utils.ErrorHandler(session, err)
				return
			}

			if result != "" {
				_, err = session.Write([]byte(fmt.Sprintf("%s\n", result)))
				if err != nil {
					utils.ErrorHandler(session, err)
				}
				return
			}

			sshHandler(session)
		}
	}
}