@ -0,0 +1,2 @@ |
|||
addedPads |
|||
gettedPads |
@ -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. |
@ -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) |
|||
} |
|||
``` |
@ -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 |
|||
} |
@ -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") |
|||
} |
|||
} |