~kota/pcf

ref: v1.0.1 pcf/main.go -rw-r--r-- 2.2 KiB
cc9e504bDakota Walsh Change placeholder manpage into functional manpage 2 years 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// pcf - A powerful paste.cf command line client
// Copyright (C) 2019 Dakota Walsh
// GPL3+ See LICENSE in this repo for details.
package main

import (
	"crypto/sha1"
	"fmt"
	"github.com/jlaffaye/ftp"
	"io"
	"net/url"
	"os"
	"path"
	"path/filepath"
	"time"
)

var addr = "https://paste.cf"
var port = "21"
var max = 10 * 1024 * 1024
var pub = "incoming"
var stdin_name = "file"

// create the ftp connection
func login(u *url.URL) *ftp.ServerConn {
	c, err := ftp.Dial(u.Host+":"+port, ftp.DialWithTimeout(10*time.Second))
	if err != nil {
		fmt.Fprintf(os.Stderr, "pcf: dial: %v\n", err)
	}
	err = c.Login("anonymous", "anonymous")
	if err != nil {
		fmt.Fprintf(os.Stderr, "pcf: login: %v\n", err)
	}
	return c
}

// store the passed file in the passed connection
func store(f *os.File, c *ftp.ServerConn, n string) {
	err := c.Stor(path.Join(pub, n), f)
	if err != nil {
		fmt.Fprintf(os.Stderr, "pcf: put: %v\n", err)
	}
}

// close the ftp connection
func exit(c *ftp.ServerConn) {
	err := c.Quit()
	if err != nil {
		fmt.Fprintf(os.Stderr, "pcf: quit: %v\n", err)
	}
}

// upload the file to the ftp server
func put(f *os.File, n string, u *url.URL) {
	if _, err := f.Seek(0, 0); err != nil {
		fmt.Fprintf(os.Stderr, "pcf: failed to read: %v\n", err)
	}
	c := login(u)
	store(f, c, n)
	exit(c)
}

// calculate and print the hash
func hash(f *os.File) string {
	if _, err := f.Seek(0, 0); err != nil {
		fmt.Fprintf(os.Stderr, "pcf: failed to read: %v\n", err)
	}
	h := sha1.New()
	if _, err := io.Copy(h, f); err != nil {
		fmt.Fprintf(os.Stderr, "pcf: failed to hash: %v\n", err)
	}
	return fmt.Sprintf("%x", h.Sum(nil))
}

func main() {
	// parse the url
	u, err := url.Parse(addr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "pcf: url configuration wrong: %v\n", err)
	}
	files := os.Args[1:]
	if len(files) == 0 {
		// use stdin data
		put(os.Stdin, stdin_name, u)
		hash(os.Stdin)
	} else {
		// loop through and use all arguments
		for _, arg := range files {
			f, err := os.Open(arg)
			if err != nil {
				fmt.Fprintf(os.Stderr, "pcf: open: %v\n", err)
				continue
			}
			defer f.Close()

			// upload the file
			put(f, arg, u)
			// calculate the hash
			h := hash(f)
			// print the url
			u.Path = h + filepath.Ext(arg)
			fmt.Println(u)
		}
	}
}