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
11func (repo *Repo) GetPad(link string, extension string, directory string, title string) (string, error) {
12	if extension != "md" && extension != "txt" && extension != "html" && extension != "pdf" && extension != "odt" {
13		return "", errors.New("No valid extension")
14	}
15	format := extension
16	if extension == "md" {
17		format = "markdown"
18		extension = "md"
19	}
20
21	//create the pads directory
22	_ = os.Mkdir(repo.Dir+"/"+directory, os.ModePerm)
23
24	completeLink := link + "/export/" + format
25
26	//get the content from the url
27	r, err := http.Get(completeLink)
28	if err != nil {
29		fmt.Println(err)
30		return "", err
31	}
32	defer r.Body.Close()
33	content, err := ioutil.ReadAll(r.Body)
34	if err != nil {
35		fmt.Printf("%s", err)
36		return "", err
37	}
38
39	//save the content into a file
40	err = ioutil.WriteFile(repo.Dir+"/"+directory+"/"+title+"."+extension, content, 0644)
41	if err != nil {
42		fmt.Println(err)
43		return "", err
44	}
45	return repo.Dir + "/" + directory + "/" + title + "." + extension, nil
46}