arnaucube
arnaucube/padArchiverPublic Self-hosted repository
padArchiver/padArchiver.go
padArchiver.goRaw
 1package padArchiver
 2
 3import (
 4	"os"
 5
 6	"github.com/fatih/color"
 7)
 8
 9//Storage is the directory where are stored the repos
10const Storage = "reposStorage"
11
12//Repo is the directory where is placed the repository of pads
13type Repo struct {
14	Dir string
15}
16
17//OpenRepo opens a repo from the directory
18func OpenRepo(directory string) Repo {
19	//if not exist create the repos directory
20	_ = os.Mkdir(Storage, os.ModePerm)
21
22	var repo Repo
23	repo.Dir = Storage + "/" + directory
24	//create the repo directory
25	_ = os.Mkdir(repo.Dir, os.ModePerm)
26	return repo
27}
28
29//StorePad gets a pad from the link, and stores it into local directory. Then also, adds the file to IPFS.
30func (repo *Repo) StorePad(link string, directory string, title string, ipfsActive bool) (string, error) {
31	path, err := repo.GetPad(link, "md", directory, title)
32	if err != nil {
33		color.Red(err.Error())
34		return "", err
35	}
36
37	if !ipfsActive {
38		return "", nil
39	}
40	hash, err := IpfsAdd(path)
41	if err != nil {
42		color.Red(err.Error())
43		return hash, err
44	}
45
46	err = AddLineToFile(path, "IPFS hash of this document: "+hash)
47	if err != nil {
48		color.Red(err.Error())
49		return hash, err
50	}
51	// TODO
52	// err = repo.GitUpdate("update commit")
53	// if err != nil {
54	// 	color.Red(err.Error())
55	// 	return hash, err
56	// }
57
58	return hash, nil
59}