From d9c4c90936efe0dbcfefba810093e2ff66a56f28 Mon Sep 17 00:00:00 2001 From: Amrit Panesar Date: Thu, 28 Dec 2017 19:46:22 -0800 Subject: [PATCH] use a standard Base62 library for slug generation, attempt to create output directory if it doesn't exist --- fiche.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ init.go | 13 +++++++++++++ main.go | 46 -------------------------------------------- slug.go | 23 ---------------------- 4 files changed, 71 insertions(+), 69 deletions(-) create mode 100644 fiche.go delete mode 100644 slug.go diff --git a/fiche.go b/fiche.go new file mode 100644 index 0000000..da69317 --- /dev/null +++ b/fiche.go @@ -0,0 +1,58 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "net" + "os" + "time" + + "github.com/pilu/go-base62" + "github.com/spf13/viper" +) + +func handleConnection(conn net.Conn) { + remoteHost, err := net.LookupCNAME(conn.RemoteAddr().String()) + if err != nil { + remoteHost = conn.RemoteAddr().String() + } + defer func() { + log.Printf("Closing connection to: %s (%s).\n", remoteHost, conn.RemoteAddr().String()) + conn.Close() + }() + log.Printf("Incoming connection from: %s (%s).\n", remoteHost, conn.RemoteAddr().String()) + conn.SetReadDeadline(time.Now().Add(time.Second * 5)) + + slug := base62.Encode(int(time.Now().UnixNano())) + slugFullpath := fmt.Sprintf("%s/%s", viper.GetString("output"), slug) + log.Printf("Writing file to %s", slugFullpath) + + file, err := os.Create(slugFullpath) + if err != nil { + conn.Write([]byte(fmt.Sprintf("%s", "Internal error encountered - Please try again later...\n\n"))) + log.Fatalf("Unable to create slug file: %s", slugFullpath) + return + } + + scanner := bufio.NewScanner(conn) + for scanner.Scan() { + file.WriteString(scanner.Text()) + file.Write([]byte("\n")) + conn.SetReadDeadline(time.Now().Add(time.Second * 5)) + } + + stat, _ := file.Stat() + if stat.Size() <= 0 { + file.Close() + log.Printf("0 bytes received, aborting...") + os.Remove(slugFullpath) + return + } + + file.Close() + + log.Printf("Wrote %d bytes to %s", stat.Size(), slugFullpath) + + conn.Write([]byte(fmt.Sprintf("%s://%s/%s\r\n", viper.GetString("uriprefix"), viper.GetString("domain"), slug))) +} diff --git a/init.go b/init.go index 21506e7..7d227ae 100644 --- a/init.go +++ b/init.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "os" "github.com/spf13/pflag" @@ -46,4 +47,16 @@ func init() { } else { viper.Set("uriprefix", "http") } + + _, err := os.Stat(viper.GetString("output")) + if err != nil { + log.Printf("unable to stat() output directory: %s\n", viper.GetString("output")) + log.Printf("attempting to create %s\n", viper.GetString("output")) + err = os.Mkdir(viper.GetString("output"), os.ModeDir) + if err != nil { + log.Printf("Unable to create directory %s\n", viper.GetString("output")) + log.Fatalf("System error: %s\n", err) + os.Exit(1) + } + } } diff --git a/main.go b/main.go index 5403fd9..09709fa 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "bufio" "fmt" "log" "net" @@ -32,48 +31,3 @@ func main() { go handleConnection(conn) } } - -func handleConnection(conn net.Conn) { - remoteHost, err := net.LookupCNAME(conn.RemoteAddr().String()) - if err != nil { - remoteHost = conn.RemoteAddr().String() - } - defer func() { - log.Printf("Closing connection to: %s (%s).\n", remoteHost, conn.RemoteAddr().String()) - conn.Close() - }() - log.Printf("Incoming connection from: %s (%s).\n", remoteHost, conn.RemoteAddr().String()) - conn.SetReadDeadline(time.Now().Add(time.Second * 5)) - - slug := generateSlug(time.Now().UnixNano()) - slugFullpath := fmt.Sprintf("%s/%s", viper.GetString("output"), slug) - log.Printf("Writing file to %s", slugFullpath) - - file, err := os.Create(slugFullpath) - if err != nil { - log.Fatalf("Unable to create slug file: %s", slugFullpath) - conn.Write([]byte(fmt.Sprintf("%s", "Internal error encountered - Please try again later..."))) - return - } - - scanner := bufio.NewScanner(conn) - for scanner.Scan() { - file.WriteString(scanner.Text()) - file.Write([]byte("\n")) - conn.SetReadDeadline(time.Now().Add(time.Second * 5)) - } - - stat, _ := file.Stat() - if stat.Size() <= 0 { - file.Close() - log.Printf("0 bytes received, aborting...") - os.Remove(slugFullpath) - return - } - - file.Close() - - log.Printf("Wrote %d bytes to %s", stat.Size(), slugFullpath) - - conn.Write([]byte(fmt.Sprintf("%s://%s/%s\r\n", viper.GetString("uriprefix"), viper.GetString("domain"), slug))) -} diff --git a/slug.go b/slug.go deleted file mode 100644 index c295a68..0000000 --- a/slug.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "fmt" - "strconv" -) - -// slugMap contains a sequence of valid characters for slug generation -const slugMap = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789" - -// generateSlug takes an integer and generates a string based on slugMap -func generateSlug(seed int64) string { - stringSeed := fmt.Sprintf("%d", seed) - evenLength := (len(stringSeed) / 2) * 2 - digitHold := int64(0) - out := "" - for i := 0; i < evenLength; i += 2 { - digitHold, _ = strconv.ParseInt(stringSeed[i:i+2], 10, 8) - digitHold = digitHold % int64(len(slugMap)) - out += slugMap[digitHold : digitHold+1] - } - return out -}