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