padArchiver/ipfs.go
ipfs.goRaw
1package padArchiver
2
3import (
4 "fmt"
5 "os"
6
7 sh "github.com/ipfs/go-ipfs-api"
8)
9
10//IpfsStorage is the directory where are stored the pads that are getted from IPFS
11const IpfsStorage = "ipfsStorage"
12
13//GettedPads is the directory where are stored the pads that are getted from the links
14const GettedPads = "ipfsStorage/gettedPads"
15
16//IpfsAdd 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
17func IpfsAdd(path string) (string, error) {
18 //connect to ipfs shell
19 s := sh.NewShell("localhost:5001")
20 //save the file into IPFS
21 ipfsHash, err := s.AddDir(path)
22 if err != nil {
23 fmt.Println(err)
24 return "", err
25 }
26 return ipfsHash, nil
27}
28
29//IpfsGet gets the content from IPFS for a given hash, and saves it into a file
30func IpfsGet(hash string, filename string) error { //create the pads directory
31 //create the pads directory
32 _ = os.Mkdir(IpfsStorage, os.ModePerm)
33 _ = os.Mkdir(GettedPads, os.ModePerm)
34
35 //connect to ipfs shell
36 s := sh.NewShell("localhost:5001")
37 err := s.Get(hash, GettedPads+"/"+filename)
38 if err != nil {
39 fmt.Println(err)
40 return err
41 }
42 return nil
43}