2015-12-07 05:01:03 +01:00
|
|
|
package src
|
|
|
|
|
|
|
|
//from yadisk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-06-12 16:06:02 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-12-07 05:01:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// PerformUpload does the actual upload via unscoped PUT request.
|
2016-09-21 23:13:24 +02:00
|
|
|
func (c *Client) PerformUpload(url string, data io.Reader, contentType string) (err error) {
|
2015-12-07 05:01:03 +01:00
|
|
|
req, err := http.NewRequest("PUT", url, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-21 23:13:24 +02:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2015-12-07 05:01:03 +01:00
|
|
|
|
|
|
|
//c.setRequestScope(req)
|
|
|
|
|
2016-02-15 17:43:18 +01:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2015-12-07 05:01:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer CheckClose(resp.Body, &err)
|
|
|
|
|
|
|
|
if resp.StatusCode != 201 {
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:06:02 +02:00
|
|
|
return errors.Errorf("upload error [%d]: %s", resp.StatusCode, string(body[:]))
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|