diff options
author | KatolaZ <katolaz@freaknet.org> | 2017-07-03 13:08:46 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2017-07-03 13:08:46 +0100 |
commit | 8f5b41695d0f2b291b4ce3e26ee24c5f26e04cfb (patch) | |
tree | 4057cea5ae1e25727a8e8384e29920dfe559abcb /paste | |
parent | dece484ef755746d7a212694cffae65e2eaee6b8 (diff) |
moved paste operations into paste/paste.go. Using paste.Store()
Diffstat (limited to 'paste')
-rw-r--r-- | paste/paste.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/paste/paste.go b/paste/paste.go new file mode 100644 index 0000000..ce06f6a --- /dev/null +++ b/paste/paste.go @@ -0,0 +1,52 @@ +package paste + +import( + "crypto/sha256" + "fmt" + "log" + "os" + "io/ioutil" + "errors" +) + + + +func Store(title, date, content, dest_dir string) (string, error) { + + h := sha256.New() + + h.Write([]byte(title)) + h.Write([]byte(date)) + h.Write([]byte(content)) + + paste := fmt.Sprintf("# Title: %s\n# Date: %s\n%s", title, date, content) + + paste_hash := fmt.Sprintf("%x", h.Sum(nil)) + log.Printf(" `-- hash: %s\n", paste_hash) + paste_dir := dest_dir + "/" + + + // Now we save the file + for i := 0; i < len(paste_hash)-16; i++ { + paste_name := paste_hash[i:i+16] + if _, err := os.Stat(paste_dir + paste_name); os.IsNotExist(err) { + // The file does not exist, so we can create it + if err := ioutil.WriteFile(paste_dir + paste_name, []byte(paste), 0644); err == nil { + // and then we return the URL: + log.Printf(" `-- saving new paste to : %s", paste_dir + paste_name) + return paste_name, nil + } else { + log.Printf("Cannot create the paste: %s!\n", paste_dir + paste_name) + } + } + } + return "", errors.New("Cannot store the paste...Sorry!") +} + + +//func Retrieve(URI string) (title, date, content string) { + + + + +//} |