TP-48508 | Adding helper to download files form a google drive directory (#305)

* TP-48508 | Adding helper to download files form a google drive directory
This commit is contained in:
Sriram Bhargav
2023-12-07 17:24:13 +05:30
committed by GitHub
parent a62ecbe0a5
commit 53da5c81b0
9 changed files with 315 additions and 38 deletions

View File

@@ -4,14 +4,16 @@ import (
"context"
"google.golang.org/api/drive/v3"
"houston/common/util"
"net/http"
"time"
)
type GoogleDriveActionsImpl struct {
type ActionsImpl struct {
filesService *drive.FilesService
}
func (driveActions *GoogleDriveActionsImpl) SearchInDrive(timeout time.Duration, query string) (*drive.FileList, error) {
func (driveActions *ActionsImpl) SearchInDrive(timeout time.Duration, query string) (*drive.FileList,
error) {
driveContext, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
fileList, err := driveActions.filesService.List().Q(query).Context(driveContext).Do()
@@ -21,7 +23,8 @@ func (driveActions *GoogleDriveActionsImpl) SearchInDrive(timeout time.Duration,
return fileList, nil
}
func (driveActions *GoogleDriveActionsImpl) CreateDirectory(timeout time.Duration, directoryName string) (*drive.File, error) {
func (driveActions *ActionsImpl) CreateDirectory(timeout time.Duration,
directoryName string) (*drive.File, error) {
driveContext, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
directory := &drive.File{MimeType: util.GoogleDriveFileMimeType, Name: directoryName}
@@ -32,7 +35,7 @@ func (driveActions *GoogleDriveActionsImpl) CreateDirectory(timeout time.Duratio
return file, nil
}
func (driveActions *GoogleDriveActionsImpl) DeleteFile(timeout time.Duration, fileId string) error {
func (driveActions *ActionsImpl) DeleteFile(timeout time.Duration, fileId string) error {
driveContext, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
err := driveActions.filesService.Delete(fileId).Context(driveContext).Do()
@@ -42,7 +45,8 @@ func (driveActions *GoogleDriveActionsImpl) DeleteFile(timeout time.Duration, fi
return nil
}
func (driveActions *GoogleDriveActionsImpl) CopyFile(timeout time.Duration, fileId string, directoryId string) (*drive.File, error) {
func (driveActions *ActionsImpl) CopyFile(timeout time.Duration, fileId string,
directoryId string) (*drive.File, error) {
driveContext, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
copyFile := &drive.File{Parents: []string{directoryId}}
@@ -52,3 +56,14 @@ func (driveActions *GoogleDriveActionsImpl) CopyFile(timeout time.Duration, file
}
return file, nil
}
func (driveActions *ActionsImpl) ExportFile(timeout time.Duration, fileId string,
mimeType string) (*http.Response, error) {
driveContext, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
download, err := driveActions.filesService.Export(fileId, mimeType).Context(driveContext).Download()
if err != nil {
return nil, err
}
return download, nil
}

View File

@@ -7,21 +7,23 @@ import (
"google.golang.org/api/option"
"houston/internal/clients"
"houston/logger"
"net/http"
"time"
)
type GoogleDriveActions interface {
type DriveActions interface {
SearchInDrive(timeout time.Duration, query string) (*drive.FileList, error)
CreateDirectory(timeout time.Duration, directoryName string) (*drive.File, error)
DeleteFile(timeout time.Duration, fileId string) error
CopyFile(timeout time.Duration, fileId string, directoryId string) (*drive.File, error)
ExportFile(timeout time.Duration, fileId string, mimeType string) (*http.Response, error)
}
func NewGoogleDriveActions() (*GoogleDriveActionsImpl, error) {
func NewGoogleDriveActions() (*ActionsImpl, error) {
driveService, err := drive.NewService(context.Background(), option.WithTokenSource(clients.GetGoogleTokenSource()))
if err != nil {
logger.Error("Unable to retrieve Drive client", zap.Error(err))
return nil, err
}
return &GoogleDriveActionsImpl{filesService: driveService.Files}, nil
return &ActionsImpl{filesService: driveService.Files}, nil
}