cloud.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package cloud
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "path/filepath"
  10. )
  11. // A client represents a client connection to a {own|next}cloud
  12. type Client struct {
  13. Url *url.URL
  14. Username string
  15. Password string
  16. }
  17. // Error type encapsulates the returned error messages from the
  18. // server.
  19. type Error struct {
  20. // Exception contains the type of the exception returned by
  21. // the server.
  22. Exception string `xml:"exception"`
  23. // Message contains the error message string from the server.
  24. Message string `xml:"message"`
  25. }
  26. func (e *Error) Error() string {
  27. return fmt.Sprintf("Exception: %s, Message: %s", e.Exception, e.Message)
  28. }
  29. // Dial connects to an {own|next}Cloud instance at the specified
  30. // address using the given credentials.
  31. func Dial(host, username, password string) (*Client, error) {
  32. url, err := url.Parse(host)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &Client{
  37. Url: url,
  38. Username: username,
  39. Password: password,
  40. }, nil
  41. }
  42. // Mkdir creates a new directory on the cloud with the specified name.
  43. func (c *Client) Mkdir(path string) error {
  44. _, err := c.sendRequest("MKCOL", path, nil)
  45. return err
  46. }
  47. // Delete removes the specified folder from the cloud.
  48. func (c *Client) Delete(path string) error {
  49. _, err := c.sendRequest("DELETE", path, nil)
  50. return err
  51. }
  52. // Upload uploads the specified source to the specified destination
  53. // path on the cloud.
  54. func (c *Client) Upload(src []byte, dest string) error {
  55. _, err := c.sendRequest("PUT", dest, src)
  56. return err
  57. }
  58. // UploadDir uploads an entire directory on the cloud. It returns the
  59. // path of uploaded files or error. It uses glob pattern in src.
  60. func (c *Client) UploadDir(src string, dest string) ([]string, error) {
  61. files, err := filepath.Glob(src)
  62. if err != nil {
  63. return nil, err
  64. }
  65. for _, file := range files {
  66. data, err := ioutil.ReadFile(file)
  67. if err != nil {
  68. return nil, err
  69. }
  70. err = c.Upload(data, filepath.Join(dest, filepath.Base(file)))
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. return files, nil
  76. }
  77. // Download downloads a file from the specified path.
  78. func (c *Client) Download(path string) ([]byte, error) {
  79. pathUrl, err := url.Parse(path)
  80. if err != nil {
  81. return nil, err
  82. }
  83. // Create the https request
  84. client := &http.Client{}
  85. req, err := http.NewRequest("GET", c.Url.ResolveReference(pathUrl).String(), nil)
  86. if err != nil {
  87. return nil, err
  88. }
  89. req.SetBasicAuth(c.Username, c.Password)
  90. resp, err := client.Do(req)
  91. if err != nil {
  92. return nil, err
  93. }
  94. body, err := ioutil.ReadAll(resp.Body)
  95. if err != nil {
  96. return nil, err
  97. }
  98. error := Error{}
  99. err = xml.Unmarshal(body, &error)
  100. if err == nil {
  101. if error.Exception != "" {
  102. return nil, err
  103. }
  104. }
  105. return body, nil
  106. }
  107. func (c *Client) Exists(path string) bool {
  108. _, err := c.sendRequest("PROPFIND", path, nil)
  109. return err == nil
  110. }
  111. func (c *Client) sendRequest(request string, path string, data []byte) ([]byte, error) {
  112. // Create the https request
  113. folderUrl, err := url.Parse(path)
  114. if err != nil {
  115. return nil, err
  116. }
  117. client := &http.Client{}
  118. req, err := http.NewRequest(request, c.Url.ResolveReference(folderUrl).String(), bytes.NewReader(data))
  119. if err != nil {
  120. return nil, err
  121. }
  122. req.SetBasicAuth(c.Username, c.Password)
  123. resp, err := client.Do(req)
  124. if err != nil {
  125. return nil, err
  126. }
  127. body, err := ioutil.ReadAll(resp.Body)
  128. if err != nil {
  129. return nil, err
  130. }
  131. if len(body) > 0 {
  132. error := Error{}
  133. err = xml.Unmarshal(body, &error)
  134. if err != nil {
  135. return body, err
  136. }
  137. if error.Exception != "" {
  138. return nil, err
  139. }
  140. }
  141. return body, nil
  142. }