From db0a1ee3f1599dd249af247641f0602bc9c05b0d Mon Sep 17 00:00:00 2001 From: namhq1989 Date: Wed, 23 Nov 2022 09:46:29 +0700 Subject: [PATCH] add google drive support --- by_url.go | 37 -------------------- google_drive.go | 93 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 75 insertions(+), 55 deletions(-) delete mode 100644 by_url.go diff --git a/by_url.go b/by_url.go deleted file mode 100644 index 9470c65..0000000 --- a/by_url.go +++ /dev/null @@ -1,37 +0,0 @@ -package download - -import ( - "fmt" - "io" - "net/http" - "os" -) - -func ByURL(url, location string) (err error) { - // 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 -} diff --git a/google_drive.go b/google_drive.go index bd6e677..5f597e0 100644 --- a/google_drive.go +++ b/google_drive.go @@ -4,8 +4,9 @@ import ( "context" "errors" "fmt" - - "github.com/kr/pretty" + "io" + "net/http" + "os" "github.com/tanaikech/go-getfilelist" @@ -15,9 +16,8 @@ import ( ) type GoogleDrive struct { - APIKey string - - svc *drive.Service + apiKey string + svc *drive.Service } type GoogleDriveFile struct { @@ -25,19 +25,21 @@ type GoogleDriveFile struct { Name string } -func (g GoogleDrive) Init() (err error) { - if g.APIKey == "" { - err = errors.New("missing google api key") +var ggdrive GoogleDrive + +func Init(apiKey string) (*GoogleDrive, error) { + if apiKey == "" { + err := errors.New("missing google api key") logger.Error("validation", logger.LogData{ Source: "download.google_drive.Init", Message: err.Error(), Data: nil, }) - return + return nil, err } // init google drive - svc, err := drive.NewService(context.Background(), option.WithAPIKey(g.APIKey)) + svc, err := drive.NewService(context.Background(), option.WithAPIKey(apiKey)) if err != nil { err = fmt.Errorf("unable to retrieve Drive: %v", err) logger.Error("init google drive service", logger.LogData{ @@ -45,13 +47,14 @@ func (g GoogleDrive) Init() (err error) { Message: err.Error(), Data: nil, }) - return + return nil, err } // assign - g.svc = svc + ggdrive.apiKey = apiKey + ggdrive.svc = svc - return + return &ggdrive, nil } func (g GoogleDrive) GetFilesByFolderID(folderID string) ([]GoogleDriveFile, error) { @@ -70,12 +73,66 @@ func (g GoogleDrive) GetFilesByFolderID(folderID string) ([]GoogleDriveFile, err return result, err } - // if there is no files - //if len(res.FileList) == 0 { - // return result, nil - //} + // if there is more than 1 subfolder + if len(res.FileList) != 1 { + err := fmt.Errorf("folder %s have more than 1 subfolder", folderID) + logger.Error("", logger.LogData{ + Source: "download.google_drive.GetFilesByFolderID", + Message: err.Error(), + Data: logger.Map{"folder": folderID}, + }) + return result, err + } - pretty.Println("res", res) + // 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, + }) + } + + //pretty.Println("res", res) return result, nil } + +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 +}