You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.1 KiB

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