219 lines
4.5 KiB
Go
219 lines
4.5 KiB
Go
package GDisk
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"golang.org/x/oauth2"
|
||
"golang.org/x/oauth2/google"
|
||
"google.golang.org/api/drive/v3"
|
||
"google.golang.org/api/option"
|
||
"io"
|
||
"io/ioutil"
|
||
"net/http"
|
||
"os"
|
||
"strings"
|
||
)
|
||
|
||
const (
|
||
CredentialsFile = "gdisk/client_secret_150245192265-q14giqad5mcvpi3o9gm3cicqc8dr48bl.apps.googleusercontent.com.json"
|
||
)
|
||
|
||
type Client struct {
|
||
App *ClientApp
|
||
HTTPClient *http.Client
|
||
Service *drive.Service
|
||
Token *oauth2.Token
|
||
}
|
||
|
||
type ClientApp struct {
|
||
Config *oauth2.Config
|
||
}
|
||
|
||
func NewClientApp() (*ClientApp, error) {
|
||
b, err := ioutil.ReadFile(CredentialsFile)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
config, err := google.ConfigFromJSON(b,
|
||
drive.DriveScope,
|
||
drive.DriveMetadataScope,
|
||
drive.DriveFileScope,
|
||
drive.DriveAppdataScope,
|
||
)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &ClientApp{Config: config}, nil
|
||
}
|
||
|
||
func (ca *ClientApp) GenerateOAuthUrl() string {
|
||
return ca.Config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
|
||
}
|
||
|
||
func (ca *ClientApp) NewClient(ctx context.Context, token *oauth2.Token) (*Client, error) {
|
||
client := ca.Config.Client(ctx, token)
|
||
srv, err := drive.NewService(context.Background(), option.WithHTTPClient(client))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &Client{
|
||
App: ca,
|
||
HTTPClient: client,
|
||
Service: srv,
|
||
Token: token,
|
||
}, nil
|
||
}
|
||
|
||
func (ca *ClientApp) GetToken(ctx context.Context, authCode string) (*oauth2.Token, error) {
|
||
return ca.Config.Exchange(ctx, authCode)
|
||
}
|
||
|
||
func (c *Client) SetToken(token oauth2.Token) {
|
||
c.Token = &token
|
||
}
|
||
|
||
func (c *Client) GetDisk() {
|
||
//srv, err := drive.NewService(context.Background(), option.WithHTTPClient(c.HTTPClient))
|
||
//
|
||
//if err != nil {
|
||
// fmt.Println("err123", err)
|
||
// return
|
||
//}
|
||
//
|
||
//about, err := srv.About.Get().Fields("storageQuota", "user", "exportFormats", "maxImportSizes", "maxUploadSize").Do()
|
||
//if err != nil {
|
||
// fmt.Println("err1234", err)
|
||
// return
|
||
//}
|
||
//fmt.Printf("srv.About %+v \r\n", about.User)
|
||
//
|
||
//dl, err := srv.Drives.List().Do()
|
||
//if err != nil {
|
||
// fmt.Println("err12345", err)
|
||
// return
|
||
//}
|
||
//
|
||
//fmt.Println("DRIVES ________________________")
|
||
//for _, drive := range dl.Drives {
|
||
// fmt.Println(drive)
|
||
//}
|
||
}
|
||
|
||
// GetResourceByName - получить информацию о ресурсе
|
||
func (c *Client) GetResourceByName(name, parentID string) ([]*drive.File, error) {
|
||
query := "name = '" + name + "' and trashed = false"
|
||
|
||
if parentID != "" {
|
||
query += "and '" + parentID + "' in parents"
|
||
}
|
||
|
||
fl, err := c.Service.Files.List().
|
||
Q(query).
|
||
Do()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if len(fl.Files) == 0 {
|
||
return nil, nil
|
||
}
|
||
|
||
return fl.Files, nil
|
||
}
|
||
|
||
// GetResources - получить список файлов и папок ресурса по его id
|
||
func (c *Client) GetResources(id string) ([]*drive.File, error) {
|
||
query := fmt.Sprintf("trashed = false and '%v' in parents", id)
|
||
|
||
fl, err := c.Service.Files.List().
|
||
Q(query).
|
||
Do()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if len(fl.Files) == 0 {
|
||
return nil, nil
|
||
}
|
||
|
||
return fl.Files, nil
|
||
}
|
||
|
||
// PutResources - создание папки
|
||
func (c *Client) PutResources(name, parentID string) (*drive.File, error) {
|
||
f, err := c.Service.Files.Create(&drive.File{Name: name, MimeType: "application/vnd.google-apps.folder",
|
||
Parents: []string{parentID}}).Do()
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return f, nil
|
||
}
|
||
|
||
// UploadFile - отправить файл в диск
|
||
func (c *Client) UploadFile(filepath, mimetype, parentID string) error {
|
||
file, err := os.Open(filepath)
|
||
defer file.Close()
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
filename := strings.Split(file.Name(), "/")
|
||
|
||
metaFile := drive.File{
|
||
Name: filename[len(filename)-1],
|
||
MimeType: mimetype,
|
||
Parents: []string{parentID},
|
||
}
|
||
|
||
_, err = c.Service.Files.Create(&metaFile).Media(file).Do()
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func (c *Client) DownloadFile(filepath, fileID string) error {
|
||
file, err := c.Service.Files.Get(fileID).Do()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
var resp *http.Response
|
||
fmt.Println("mimeType", file.MimeType)
|
||
if file.MimeType == "application/vnd.google-apps.document" {
|
||
resp, err = c.Service.Files.Export(fileID, "application/vnd.openxmlformats-officedocument.wordprocessingml.document").
|
||
Fields("exportFormat", "docx").
|
||
Download()
|
||
} else {
|
||
resp, err = c.Service.Files.Get(fileID).Download()
|
||
}
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
defer resp.Body.Close()
|
||
|
||
out, err := os.Create(filepath)
|
||
defer out.Close()
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
_, err = io.Copy(out, resp.Body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|