2022-07-28 15:00:43 +00:00
|
|
|
package YaDisk
|
|
|
|
|
|
|
|
import (
|
2022-09-15 13:53:55 +00:00
|
|
|
"context"
|
2022-07-28 15:00:43 +00:00
|
|
|
"encoding/json"
|
2022-09-15 13:53:55 +00:00
|
|
|
"errors"
|
2022-07-28 15:00:43 +00:00
|
|
|
"fmt"
|
2022-08-10 13:53:34 +00:00
|
|
|
"golang.org/x/oauth2"
|
2022-09-15 13:53:55 +00:00
|
|
|
"golang.org/x/oauth2/yandex"
|
|
|
|
"io"
|
2022-07-28 15:00:43 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-09-15 13:53:55 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2022-07-28 15:00:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BASE_URL = "https://cloud-api.yandex.net"
|
2022-09-15 13:53:55 +00:00
|
|
|
OAUTH_URL = "https://oauth.yandex.ru"
|
2022-07-28 15:00:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2022-08-10 13:53:34 +00:00
|
|
|
App *ClientApp
|
|
|
|
HTTPClient *http.Client
|
|
|
|
Token *oauth2.Token
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ClientApp struct {
|
2022-09-15 13:53:55 +00:00
|
|
|
Config *oauth2.Config
|
|
|
|
ServiceUrl string
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
func NewClientApp(clientID, clientSecret, redirectUri, serviceUrl string) *ClientApp {
|
2022-07-28 15:00:43 +00:00
|
|
|
return &ClientApp{
|
2022-09-15 13:53:55 +00:00
|
|
|
Config: &oauth2.Config{
|
|
|
|
ClientID: clientID,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
Endpoint: yandex.Endpoint,
|
|
|
|
RedirectURL: redirectUri,
|
|
|
|
Scopes: nil,
|
|
|
|
},
|
|
|
|
ServiceUrl: serviceUrl,
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ca *ClientApp) GenerateOAuthUrl() string {
|
2022-09-15 13:53:55 +00:00
|
|
|
return ca.Config.AuthCodeURL(
|
|
|
|
"state-token",
|
|
|
|
oauth2.AccessTypeOffline,
|
|
|
|
oauth2.SetAuthURLParam("display", "popup"),
|
|
|
|
oauth2.SetAuthURLParam("force_confirm", "true"),
|
|
|
|
)
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
func (ca *ClientApp) NewClient(ctx context.Context, token *oauth2.Token, code string) (*Client, error) {
|
|
|
|
var err error
|
|
|
|
if code != "" {
|
|
|
|
token, err = ca.Config.Exchange(ctx, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Этот костыль нужен, т.к. Яндекс принимает токены только типа OAuth, хоть и отправляет типа bearer
|
|
|
|
token.TokenType = "OAuth"
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
return &Client{
|
2022-08-10 13:53:34 +00:00
|
|
|
App: ca,
|
2022-09-15 13:53:55 +00:00
|
|
|
HTTPClient: ca.Config.Client(ctx, token),
|
2022-08-10 13:53:34 +00:00
|
|
|
Token: token,
|
2022-09-15 13:53:55 +00:00
|
|
|
}, err
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
func (ca *ClientApp) RefreshToken(ctx context.Context, oldToken *oauth2.Token) (*oauth2.Token, error) {
|
|
|
|
token, err := ca.Config.TokenSource(ctx, oldToken).Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Этот костыль нужен, т.к. Яндекс принимает токены только типа OAuth, хоть и отправляет типа bearer
|
|
|
|
token.TokenType = "OAuth"
|
|
|
|
|
|
|
|
return token, nil
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetDisk() (*Disk, error) {
|
|
|
|
req, err := http.NewRequest("GET", BASE_URL+"/v1/disk", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-15 13:53:55 +00:00
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2022-09-15 13:53:55 +00:00
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
r := Error{}
|
|
|
|
json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
return nil, errors.New(fmt.Sprintf("api/yaDisk err: %v (%v)", r.Description, r.Error))
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
r := Disk{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
func (c *Client) GetUser() (*User, error) {
|
|
|
|
disk, err := c.GetDisk()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &disk.User, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
func (c *Client) GetResources(path string) (*Resource, error) {
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("path", path)
|
|
|
|
req, err := http.NewRequest("GET", BASE_URL+"/v1/disk/resources?path="+path, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == 404 {
|
2022-09-15 13:53:55 +00:00
|
|
|
fmt.Println("api/yaDisk: 404")
|
2022-07-28 15:00:43 +00:00
|
|
|
return nil, nil
|
2022-09-15 13:53:55 +00:00
|
|
|
} else if resp.StatusCode != 200 {
|
|
|
|
r := Error{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, errors.New(fmt.Sprintf("api/yaDisk err: %v (%v)", r.Description, r.Error))
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
r := Resource{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
return &r, err
|
|
|
|
}
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
func (c *Client) DownloadResource(path, downloadPath string) error {
|
|
|
|
res, err := c.GetResources(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(res.File)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(downloadPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// Write the body to file
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
func (c *Client) PutResources(path string) error {
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("path", path)
|
|
|
|
req, err := http.NewRequest("PUT", BASE_URL+"/v1/disk/resources?"+data.Encode(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := map[string]interface{}{}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
fmt.Printf("%+v", r)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
func (c *Client) ResourcesUpload(path, downloadPath string) error {
|
|
|
|
downloadPath = strings.TrimLeft(downloadPath, "..")
|
|
|
|
downloadPath = strings.TrimLeft(downloadPath, ".")
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
data := url.Values{}
|
|
|
|
data.Set("path", path)
|
2022-09-15 13:53:55 +00:00
|
|
|
data.Set("url", c.App.ServiceUrl+downloadPath)
|
2022-07-28 15:00:43 +00:00
|
|
|
req, err := http.NewRequest("POST", BASE_URL+"/v1/disk/resources/upload?"+data.Encode(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := map[string]interface{}{}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
fmt.Printf("%+v", r)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|