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
 9const Storage = "reposStorage"
10
11type Repo struct {
12	Dir string
13}
14
15func OpenRepo(directory string) Repo {
16	//if not exist create the repos directory
17	_ = os.Mkdir(Storage, os.ModePerm)
18
19	var repo Repo
20	repo.Dir = Storage + "/" + directory
21	//create the repo directory
22	_ = os.Mkdir(repo.Dir, os.ModePerm)
23	return repo
24}
25
26func (repo *Repo) StorePad(link string, directory string, title string, ipfsActive bool) (string, error) {
27	path, err := repo.GetPad(link, "md", directory, title)
28	if err != nil {
29		color.Red(err.Error())
30		return "", err
31	}
32
33	if !ipfsActive {
34		return "", nil
35	}
36	hash, err := IpfsAdd(path)
37	if err != nil {
38		color.Red(err.Error())
39		return hash, err
40	}
41
42	err = AddLineToFile(path, "IPFS hash of this document: "+hash)
43	if err != nil {
44		color.Red(err.Error())
45		return hash, err
46	}
47	// TODO
48	// err = repo.GitUpdate("update commit")
49	// if err != nil {
50	// 	color.Red(err.Error())
51	// 	return hash, err
52	// }
53
54	return hash, nil
55}