arnaucube
arnaucube/padArchiverPublic Self-hosted repository
padArchiver/etherpad.go
etherpad.goRaw
 1package padArchiver
 2
 3import (
 4	"errors"
 5	"fmt"
 6	"io/ioutil"
 7	"net/http"
 8	"os"
 9)
10
11//GetPad gets the pad from the link, and stores it into local directory
12func (repo *Repo) GetPad(link string, extension string, directory string, title string) (string, error) {
13	if extension != "md" && extension != "txt" && extension != "html" && extension != "pdf" && extension != "odt" {
14		return "", errors.New("No valid extension")
15	}
16	format := extension
17	if extension == "md" {
18		format = "markdown"
19		extension = "md"
20	}
21
22	//create the pads directory
23	_ = os.Mkdir(repo.Dir+"/"+directory, os.ModePerm)
24
25	completeLink := link + "/export/" + format
26
27	//get the content from the url
28	r, err := http.Get(completeLink)
29	if err != nil {
30		fmt.Println(err)
31		return "", err
32	}
33	defer r.Body.Close()
34	content, err := ioutil.ReadAll(r.Body)
35	if err != nil {
36		fmt.Printf("%s", err)
37		return "", err
38	}
39
40	//save the content into a file
41	err = ioutil.WriteFile(repo.Dir+"/"+directory+"/"+title+"."+extension, content, 0644)
42	if err != nil {
43		fmt.Println(err)
44		return "", err
45	}
46	return repo.Dir + "/" + directory + "/" + title + "." + extension, nil
47}