2022-11-23 02:22:39 +00:00
|
|
|
package download
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-11-23 02:46:29 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-11-23 02:22:39 +00:00
|
|
|
|
2022-11-23 02:48:33 +00:00
|
|
|
"github.com/tanaikech/go-getfilelist"
|
2022-11-23 02:22:39 +00:00
|
|
|
"google.golang.org/api/drive/v3"
|
|
|
|
"google.golang.org/api/option"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GoogleDrive struct {
|
2022-11-23 02:46:29 +00:00
|
|
|
apiKey string
|
|
|
|
svc *drive.Service
|
2022-11-23 02:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GoogleDriveFile struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2022-11-23 02:46:29 +00:00
|
|
|
var ggdrive GoogleDrive
|
|
|
|
|
2022-11-23 02:48:33 +00:00
|
|
|
func NewGoogleDrive(apiKey string) (*GoogleDrive, error) {
|
2022-11-23 02:46:29 +00:00
|
|
|
if apiKey == "" {
|
|
|
|
err := errors.New("missing google api key")
|
|
|
|
return nil, err
|
2022-11-23 02:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init google drive
|
2022-11-23 02:46:29 +00:00
|
|
|
svc, err := drive.NewService(context.Background(), option.WithAPIKey(apiKey))
|
2022-11-23 02:22:39 +00:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("unable to retrieve Drive: %v", err)
|
2022-11-23 02:46:29 +00:00
|
|
|
return nil, err
|
2022-11-23 02:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assign
|
2022-11-23 02:46:29 +00:00
|
|
|
ggdrive.apiKey = apiKey
|
|
|
|
ggdrive.svc = svc
|
2022-11-23 02:22:39 +00:00
|
|
|
|
2022-11-23 02:46:29 +00:00
|
|
|
return &ggdrive, nil
|
2022-11-23 02:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g GoogleDrive) GetFilesByFolderID(folderID string) ([]GoogleDriveFile, error) {
|
|
|
|
var (
|
|
|
|
result = make([]GoogleDriveFile, 0)
|
|
|
|
)
|
|
|
|
|
|
|
|
res, err := getfilelist.Folder(folderID).Fields("files(name,id)").Do(g.svc)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error when fetch folder %s: %s", folderID, err.Error())
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-11-23 02:46:29 +00:00
|
|
|
// if there is more than 1 subfolder
|
|
|
|
if len(res.FileList) != 1 {
|
|
|
|
err := fmt.Errorf("folder %s have more than 1 subfolder", folderID)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check list files
|
|
|
|
fol := res.FileList[0]
|
|
|
|
if len(fol.Files) == 0 {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect files
|
|
|
|
for _, f := range fol.Files {
|
|
|
|
result = append(result, GoogleDriveFile{
|
|
|
|
ID: f.Id,
|
|
|
|
Name: f.Name,
|
|
|
|
})
|
|
|
|
}
|
2022-11-23 02:22:39 +00:00
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2022-11-23 02:46:29 +00:00
|
|
|
|
|
|
|
func (g GoogleDrive) DownloadByFileID(id, name, location string) (err error) {
|
|
|
|
fmt.Printf("[google drive] start download file %s \n", name)
|
|
|
|
defer fmt.Printf("[google drive] done download file %s \n", name)
|
|
|
|
|
|
|
|
url := fmt.Sprintf("https://drive.google.com/uc?export=download&id=%s", id)
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(location)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// Get the data
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Check server response
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("bad status: %s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writer the body to file
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|