cloud.go 3.8 KB

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