From dfbae1fd70a17f344f1828b8b0b2726c5cde1b21 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Mon, 2 Apr 2018 13:57:21 +0200 Subject: [PATCH] Add from pad link to IPFS works. Get from IPFS to file works. --- .gitignore | 2 ++ LICENSE | 21 +++++++++++++ README.md | 51 ++++++++++++++++++++++++++++++ pad2ipfs.go | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ pad2ipfs_test.go | 56 +++++++++++++++++++++++++++++++++ 5 files changed, 210 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 pad2ipfs.go create mode 100644 pad2ipfs_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0863224 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +addedPads +gettedPads diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0eae6e7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 arnau + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d0bd8ed --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +# pad2ipfs [![Go Report Card](https://goreportcard.com/badge/github.com/arnaucode/pad2ipfs)](https://goreportcard.com/report/github.com/arnaucode/pad2ipfs) + +Simply Go lang library to get the content from a pad (etherpad) and put into IPFS. + + +Needs to have installed IPFS (https://ipfs.io), and the daemon running ('> ipfs daemon'). + +### Install +``` +go get github.com/arnaucode/pad2ipfs +``` + +### Usage + +The added pads are stored in 'addedPads' directory. +The getted pads are stored in 'gettedPads' directory. + + +##### - Add +Adds the content from a pad to IPFS + +```go +hash, err := pad2ipfs.Add(link, format) +``` +```go +hash, err := pad2ipfs.Add("https://board.net/p/selectedpad", "md") +if err!=nil{ + fmt.Println(err) +} +``` +Supported formats: + - md + - txt + - html + - pdf + - odt + + + +##### - Get +Gets the content from IPFS and stores it into a file + +```go +err := pad2ipfs.Get(hash, filename) +``` +```go +err := pad2ipfs.Get("QmVyp4JSREK5syLmNRCafkZkhzC7CfvS9qYWKfvfffqK2B", "selectedpad.md") +if err!=nil { + fmt.Println(err) +} +``` diff --git a/pad2ipfs.go b/pad2ipfs.go new file mode 100644 index 0000000..9a660d3 --- /dev/null +++ b/pad2ipfs.go @@ -0,0 +1,80 @@ +package pad2ipfs + +import ( + "errors" + "fmt" + "io/ioutil" + "net/http" + "os" + "strings" + + sh "github.com/ipfs/go-ipfs-api" +) + +const addedPads = "addedPads" +const gettedPads = "gettedPads" + +//Add gets the content from the etherpad specified in the link, and downloads it in the format of the specified extension, and then, puts it into IPFS +func Add(link string, extension string) (string, error) { + if extension != "md" && extension != "txt" && extension != "html" && extension != "pdf" && extension != "odt" { + return "", errors.New("No valid extension") + } + format := extension + if extension == "md" { + format = "markdown" + extension = "md" + } + + //create the pads directory + _ = os.Mkdir(addedPads, os.ModePerm) + + //get the pad name + linkSplitted := strings.Split(link, "/") + padName := linkSplitted[len(linkSplitted)-1] + completeLink := link + "/export/" + format + + //get the content from the url + r, err := http.Get(completeLink) + if err != nil { + fmt.Println(err) + return "", err + } + defer r.Body.Close() + content, err := ioutil.ReadAll(r.Body) + if err != nil { + fmt.Printf("%s", err) + return "", err + } + + //save the content into a file + err = ioutil.WriteFile(addedPads+"/"+padName+"."+extension, content, 0644) + if err != nil { + fmt.Println(err) + return "", err + } + + //connect to ipfs shell + s := sh.NewShell("localhost:5001") + //save the file into IPFS + ipfsHash, err := s.AddDir(addedPads + "/" + padName + "." + extension) + if err != nil { + fmt.Println(err) + return "", err + } + return ipfsHash, nil +} + +//Get gets the content from IPFS for a given hash, and saves it into a file +func Get(hash string, filename string) error { + //create the pads directory + _ = os.Mkdir(gettedPads, os.ModePerm) + + //connect to ipfs shell + s := sh.NewShell("localhost:5001") + err := s.Get(hash, gettedPads+"/"+filename) + if err != nil { + fmt.Println(err) + return err + } + return nil +} diff --git a/pad2ipfs_test.go b/pad2ipfs_test.go new file mode 100644 index 0000000..068a915 --- /dev/null +++ b/pad2ipfs_test.go @@ -0,0 +1,56 @@ +package pad2ipfs + +import ( + "fmt" + "testing" + + "github.com/fatih/color" +) + +const checkIcon = "\xE2\x9C\x94 " + +func TestGenerateEmptyMT(t *testing.T) { + hash1, err := Add("http://board.net/p/pad1", "md") + if err != nil { + t.Errorf(err.Error()) + } else { + color.Green(checkIcon + "added file1") + fmt.Print(" IPFS hash:") + color.Blue(hash1) + } + hash2, err := Add("http://board.net/p/pad2", "txt") + if err != nil { + t.Errorf(err.Error()) + } else { + color.Green(checkIcon + "added file2") + fmt.Print(" IPFS hash:") + color.Blue(hash2) + } + hash3, err := Add("http://board.net/p/pad3", "html") + if err != nil { + t.Errorf(err.Error()) + } else { + color.Green(checkIcon + "added file3") + fmt.Print(" IPFS hash:") + color.Blue(hash3) + } + + err = Get(hash1, "pad1.md") + if err != nil { + t.Errorf(err.Error()) + } else { + color.Green(checkIcon + "getted file1") + } + err = Get(hash2, "pad2.txt") + if err != nil { + t.Errorf(err.Error()) + } else { + color.Green(checkIcon + "getted file2") + } + err = Get(hash3, "pad3.html") + if err != nil { + t.Errorf(err.Error()) + } else { + color.Green(checkIcon + "getted file3") + } +}