~kota/pcf

2ebd395243af4fb525219ae2a4a622fcd5a6ae84 — Dakota Walsh 2 years ago 97311c4
implement hashing and printing the correct url
3 files changed, 62 insertions(+), 11 deletions(-)

M go.mod
M go.sum
M main.go
M go.mod => go.mod +4 -1
@@ 2,4 2,7 @@ module git.sr.ht/~kota/pcf

go 1.12

require github.com/jlaffaye/ftp v0.0.0-20190721194432-7cd8b0bcf3fc
require (
	github.com/jlaffaye/ftp v0.0.0-20190721194432-7cd8b0bcf3fc
	github.com/stretchr/testify v1.4.0 // indirect
)

M go.sum => go.sum +10 -0
@@ 1,2 1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jlaffaye/ftp v0.0.0-20190721194432-7cd8b0bcf3fc h1:Mc2Gk3kF0Uqx+cI97pN0gbgZb0DVW2L+htrZSKkOmtE=
github.com/jlaffaye/ftp v0.0.0-20190721194432-7cd8b0bcf3fc/go.mod h1:lli8NYPQOFy3O++YmYbqVgOcQ1JPCwdOy+5zSjKJ9qY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

M main.go => main.go +48 -10
@@ 4,20 4,26 @@
package main

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

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

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


@@ 28,6 34,7 @@ func login() *ftp.ServerConn {
	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 {


@@ 35,6 42,7 @@ func store(f *os.File, c *ftp.ServerConn, n string) {
	}
}

// close the ftp connection
func exit(c *ftp.ServerConn) {
	err := c.Quit()
	if err != nil {


@@ 42,27 50,57 @@ func exit(c *ftp.ServerConn) {
	}
}

func put(f *os.File, n string) {
	c := login()
// 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)
		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: %v\n", err)
				fmt.Fprintf(os.Stderr, "pcf: open: %v\n", err)
				continue
			}
			put(f, arg)
			f.Close()
			defer f.Close()

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