2023-07-13 23:00:40 +00:00
|
|
|
|
package yadisk
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
|
|
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-09-15 13:53:55 +00:00
|
|
|
|
"io"
|
2022-07-28 15:00:43 +00:00
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
2022-09-15 13:53:55 +00:00
|
|
|
|
"os"
|
2022-10-11 10:07:29 +00:00
|
|
|
|
"strconv"
|
2022-09-15 13:53:55 +00:00
|
|
|
|
"strings"
|
2022-11-24 19:37:47 +00:00
|
|
|
|
"time"
|
2023-01-20 13:14:17 +00:00
|
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
2024-12-16 09:17:48 +00:00
|
|
|
|
"gitea.pena/PenaSide/docxTemplater/tools"
|
2022-07-28 15:00:43 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2023-07-13 23:00:40 +00:00
|
|
|
|
BaseURL = "https://cloud-api.yandex.net"
|
|
|
|
|
OauthURL = "https://oauth.yandex.ru"
|
|
|
|
|
V1DiskAPI = BaseURL + "/v1/disk"
|
|
|
|
|
DefaultFolder = "disk:/templategen"
|
|
|
|
|
DefaultTemplateFolder = DefaultFolder + "/templates"
|
|
|
|
|
DefaultSaveFolder = DefaultFolder + "/saved"
|
2023-08-17 08:12:22 +00:00
|
|
|
|
TokenTypeOAuth = "OAuth"
|
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
|
2023-07-13 23:00:40 +00:00
|
|
|
|
ServiceURL string
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +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,
|
2022-10-11 10:07:29 +00:00
|
|
|
|
Endpoint: oauth2.Endpoint{
|
2023-07-13 23:00:40 +00:00
|
|
|
|
AuthURL: OauthURL + "/authorize",
|
|
|
|
|
TokenURL: OauthURL + "/token",
|
2022-10-11 10:07:29 +00:00
|
|
|
|
},
|
2023-07-13 23:00:40 +00:00
|
|
|
|
RedirectURL: redirectURI,
|
2022-10-11 10:07:29 +00:00
|
|
|
|
Scopes: nil,
|
2022-09-15 13:53:55 +00:00
|
|
|
|
},
|
2023-07-13 23:00:40 +00:00
|
|
|
|
ServiceURL: serviceURL,
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 10:13:15 +00:00
|
|
|
|
func (ca *ClientApp) GenerateOAuthURL(amoID, penaID, redirectURL string) (string, error) {
|
2023-07-13 23:00:40 +00:00
|
|
|
|
state, err := tools.EncryptTokenAES(tools.StateToken{
|
2023-09-06 13:01:46 +00:00
|
|
|
|
AmoID: amoID,
|
2023-08-02 17:30:06 +00:00
|
|
|
|
PenaID: penaID,
|
2022-10-11 10:07:29 +00:00
|
|
|
|
Service: "yandex",
|
2023-07-13 23:00:40 +00:00
|
|
|
|
RedirectURL: redirectURL,
|
2022-10-11 10:07:29 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
return ca.Config.AuthCodeURL(
|
2022-10-11 10:07:29 +00:00
|
|
|
|
state,
|
2022-09-15 13:53:55 +00:00
|
|
|
|
oauth2.AccessTypeOffline,
|
|
|
|
|
oauth2.SetAuthURLParam("display", "popup"),
|
|
|
|
|
oauth2.SetAuthURLParam("force_confirm", "true"),
|
2022-10-11 10:07:29 +00:00
|
|
|
|
), nil
|
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
|
2023-08-17 08:12:22 +00:00
|
|
|
|
token.TokenType = TokenTypeOAuth
|
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
|
2023-08-17 08:12:22 +00:00
|
|
|
|
token.TokenType = TokenTypeOAuth
|
2022-09-15 13:53:55 +00:00
|
|
|
|
|
|
|
|
|
return token, nil
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
func (c *Client) GetDisk(ctx context.Context) (*Disk, error) {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, V1DiskAPI, http.NoBody)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 02:20:33 +00:00
|
|
|
|
defer resp.Body.Close()
|
2023-07-13 23:00:40 +00:00
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2022-09-15 13:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
func (c *Client) GetUser(ctx context.Context) (*User, error) {
|
|
|
|
|
disk, err := c.GetDisk(ctx)
|
2022-09-15 13:53:55 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &disk.User, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 13:14:17 +00:00
|
|
|
|
type DResp struct {
|
|
|
|
|
Href string `json:"href"`
|
|
|
|
|
Method string `json:"method"`
|
|
|
|
|
Templated bool `json:"templated"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) DownloadFile(path string) (*DResp, error) {
|
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequest(http.MethodGet, V1DiskAPI+"/resources/download?"+data.Encode(), http.NoBody)
|
2023-01-20 13:14:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := c.HTTPClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 19:32:50 +00:00
|
|
|
|
defer res.Body.Close()
|
2023-07-13 23:00:40 +00:00
|
|
|
|
|
2023-04-03 18:47:47 +00:00
|
|
|
|
if err := checkError(res); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 13:14:17 +00:00
|
|
|
|
var resp DResp
|
|
|
|
|
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
func (c *Client) GetResources(ctx context.Context, path string, limit, offset int) (*Resource, error) {
|
2022-10-11 10:07:29 +00:00
|
|
|
|
if path == "" {
|
|
|
|
|
path = "disk:/"
|
|
|
|
|
}
|
2022-07-28 15:00:43 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
2022-10-11 10:07:29 +00:00
|
|
|
|
|
|
|
|
|
if limit > 0 {
|
|
|
|
|
data.Set("limit", strconv.Itoa(limit))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if offset > 0 {
|
|
|
|
|
data.Set("offset", strconv.Itoa(offset))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.Encode()
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, V1DiskAPI+"/resources?"+data.Encode(), http.NoBody)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 02:20:33 +00:00
|
|
|
|
defer resp.Body.Close()
|
2023-07-13 23:00:40 +00:00
|
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
2022-07-28 15:00:43 +00:00
|
|
|
|
return nil, nil
|
2022-10-11 10:07:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
func (c *Client) DownloadResource(ctx context.Context, path, downloadPath string) error {
|
|
|
|
|
res, err := c.GetResources(ctx, path, 0, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-11-21 20:27:11 +00:00
|
|
|
|
|
|
|
|
|
if res == nil {
|
|
|
|
|
return errors.New("file not found")
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, res.File, http.NoBody)
|
2022-09-15 13:53:55 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-11-21 20:27:11 +00:00
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2022-09-15 13:53:55 +00:00
|
|
|
|
|
2022-11-21 20:27:11 +00:00
|
|
|
|
if checkError(resp) != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
// Create the file
|
|
|
|
|
out, err := os.Create(downloadPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-07-13 23:00:40 +00:00
|
|
|
|
defer out.Close() //nolint
|
2022-09-15 13:53:55 +00:00
|
|
|
|
|
|
|
|
|
// Write the body to file
|
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
func (c *Client) DownloadResourcesBytes(ctx context.Context, path string) ([]byte, error) {
|
|
|
|
|
res, err := c.GetResources(ctx, path, 0, 0)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
|
|
|
|
if res == nil {
|
|
|
|
|
return nil, errors.New("file not found")
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, res.File, http.NoBody)
|
|
|
|
|
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
if err = checkError(resp); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return io.ReadAll(resp.Body)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
// PutResources - создание папки.
|
2022-10-11 10:07:29 +00:00
|
|
|
|
func (c *Client) PutResources(path string) (*RespPutResources, error) {
|
2022-07-28 15:00:43 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequest(http.MethodPut, V1DiskAPI+"/resources?"+data.Encode(), http.NoBody)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
if err != nil {
|
2022-10-11 10:07:29 +00:00
|
|
|
|
return nil, err
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
|
if err != nil {
|
2022-10-11 10:07:29 +00:00
|
|
|
|
return nil, err
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 02:20:33 +00:00
|
|
|
|
defer resp.Body.Close()
|
2023-07-13 23:00:40 +00:00
|
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusConflict {
|
2022-10-11 10:07:29 +00:00
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
var r RespPutResources
|
2022-07-28 15:00:43 +00:00
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
return &r, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
func (c *Client) DeleteResources(ctx context.Context, path string) error {
|
2022-10-11 10:07:29 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, V1DiskAPI+"/resources?"+data.Encode(), http.NoBody)
|
2022-10-11 10:07:29 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 19:32:50 +00:00
|
|
|
|
defer resp.Body.Close()
|
2023-07-13 23:00:40 +00:00
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
err = checkError(resp)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
// UploadResourcesURL - Загрузить файл на Яндекс диск по URL. Нерекомендуемый.
|
|
|
|
|
func (c *Client) UploadResourcesURL(ctx context.Context, path, downloadPath string) (string, error) {
|
2022-09-15 13:53:55 +00:00
|
|
|
|
downloadPath = strings.TrimLeft(downloadPath, ".")
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
fmt.Println("dp:", c.App.ServiceURL+downloadPath)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
fmt.Println("path:", path)
|
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
data.Set("url", c.App.ServiceURL+downloadPath)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, V1DiskAPI+"/resources/upload?"+data.Encode(), http.NoBody)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
fmt.Println("req:", req.URL)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
if err != nil {
|
2022-11-24 19:37:47 +00:00
|
|
|
|
return "", err
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
|
if err != nil {
|
2022-11-24 19:37:47 +00:00
|
|
|
|
return "", err
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 19:32:50 +00:00
|
|
|
|
defer resp.Body.Close()
|
2023-07-13 23:00:40 +00:00
|
|
|
|
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if checkError(resp) != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
2022-11-24 19:37:47 +00:00
|
|
|
|
var r RespUploadResources
|
2022-07-28 15:00:43 +00:00
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
status, err := c.GetOperationsByURL(ctx, r.Href)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2022-11-21 20:27:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:37:47 +00:00
|
|
|
|
// Ожидаем пока загрузится файл на Я.Диск. Timeout: 5 sec; tick: 100 ms;
|
|
|
|
|
timer := time.NewTimer(5 * time.Second)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
|
defer ticker.Stop()
|
2022-11-24 19:37:47 +00:00
|
|
|
|
for status == "in-progress" {
|
|
|
|
|
select {
|
|
|
|
|
case <-timer.C:
|
|
|
|
|
status = "timeout"
|
2023-07-13 23:00:40 +00:00
|
|
|
|
case <-ticker.C:
|
|
|
|
|
status, err = c.GetOperationsByURL(ctx, r.Href)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
timer.Stop()
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
timer.Stop()
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if status != "success" {
|
2023-07-13 23:00:40 +00:00
|
|
|
|
return "", fmt.Errorf("bad upload status: %v", status)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
resource, err := c.GetResources(ctx, path, 0, 0)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
var exportURL string
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if resource != nil {
|
2023-07-13 23:00:40 +00:00
|
|
|
|
exportURL = resource.File
|
2022-11-24 19:37:47 +00:00
|
|
|
|
} else {
|
|
|
|
|
return "", errors.New("resource not found")
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
return exportURL, err
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
// UploadResources - Загрузить файл на Яндекс диск.
|
|
|
|
|
func (c *Client) UploadResources(ctx context.Context, path string, file io.Reader) (string, string, error) {
|
2022-11-24 19:37:47 +00:00
|
|
|
|
// Get url for request
|
2023-07-13 23:00:40 +00:00
|
|
|
|
target, err := c.getUploadResourcesURL(ctx, path)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if err != nil {
|
2022-12-19 12:01:38 +00:00
|
|
|
|
return "", "", err
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if target == nil {
|
2022-12-19 12:01:38 +00:00
|
|
|
|
return "", "", errors.New("got empty url for upload")
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, target.Method, target.Href, file)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2022-12-19 12:01:38 +00:00
|
|
|
|
return "", "", err
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 19:32:50 +00:00
|
|
|
|
defer resp.Body.Close()
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
2022-12-19 12:01:38 +00:00
|
|
|
|
return "", "", err
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get uploaded data
|
2023-07-13 23:00:40 +00:00
|
|
|
|
resource, err := c.GetResources(ctx, path, 0, 0)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2022-12-19 12:01:38 +00:00
|
|
|
|
return "", "", err
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
var exportURL string
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if resource != nil {
|
2023-07-13 23:00:40 +00:00
|
|
|
|
exportURL = resource.File
|
2022-11-24 19:37:47 +00:00
|
|
|
|
} else {
|
2022-12-19 12:01:38 +00:00
|
|
|
|
return "", "", errors.New("resource not found")
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
return resource.Path, exportURL, err
|
2022-11-24 19:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
// getUploadResourcesURL - получить ссылку для отправки файла по TLS.
|
|
|
|
|
func (c *Client) getUploadResourcesURL(ctx context.Context, path string) (*RespUploadResources, error) {
|
2022-11-24 19:37:47 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
data.Set("overwrite", "true")
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, V1DiskAPI+"/resources/upload?"+data.Encode(), http.NoBody)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 19:32:50 +00:00
|
|
|
|
defer resp.Body.Close()
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r := RespUploadResources{}
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
|
|
return &r, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
// GetOperations - возвращает статус асинхронной операции.
|
|
|
|
|
func (c *Client) GetOperations(ctx context.Context, operationID string) (string, error) {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, V1DiskAPI+"/operations/"+operationID, http.NoBody)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 19:32:50 +00:00
|
|
|
|
defer resp.Body.Close()
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r := Operation{}
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
|
|
return r.Status, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
// GetOperationsByURL - возвращает статус асинхронной операции.
|
|
|
|
|
func (c *Client) GetOperationsByURL(ctx context.Context, url string) (string, error) {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 19:32:50 +00:00
|
|
|
|
defer resp.Body.Close()
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r := Operation{}
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
|
|
return r.Status, err
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
2022-10-11 10:07:29 +00:00
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
// PublishResource - публикует ресурс.
|
|
|
|
|
func (c *Client) PublishResource(ctx context.Context, path string) error {
|
2023-01-14 12:27:53 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"/resources/publish?"+data.Encode(), http.NoBody)
|
2023-01-14 12:27:53 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2023-07-13 23:00:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 19:32:50 +00:00
|
|
|
|
defer resp.Body.Close()
|
2023-01-14 12:27:53 +00:00
|
|
|
|
|
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
// checkError - если статус не в диапазоне 200-х вернет расшифровку ошибки.
|
2022-10-11 10:07:29 +00:00
|
|
|
|
func checkError(resp *http.Response) error {
|
|
|
|
|
switch resp.StatusCode {
|
2023-07-13 23:00:40 +00:00
|
|
|
|
case http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNonAuthoritativeInfo,
|
|
|
|
|
http.StatusNoContent, http.StatusResetContent, http.StatusPartialContent,
|
|
|
|
|
http.StatusMultiStatus, http.StatusAlreadyReported, http.StatusIMUsed:
|
2022-10-11 10:07:29 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
r := Error{}
|
|
|
|
|
err := json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 23:00:40 +00:00
|
|
|
|
return fmt.Errorf("api/yaDisk (%v) err: %v | %v (%v)", resp.StatusCode, r.Message, r.Description, r.Error)
|
2022-10-11 10:07:29 +00:00
|
|
|
|
}
|