Files
houston-be/pkg/google/googleDrive/drive_actions.go
Sriram Bhargav 53da5c81b0 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
2023-12-07 17:24:13 +05:30

70 lines
2.1 KiB
Go

package googleDrive
import (
"context"
"google.golang.org/api/drive/v3"
"houston/common/util"
"net/http"
"time"
)
type ActionsImpl struct {
filesService *drive.FilesService
}
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()
if err != nil {
return nil, err
}
return fileList, nil
}
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}
file, err := driveActions.filesService.Create(directory).Context(driveContext).Fields("*").Do()
if err != nil {
return nil, err
}
return file, nil
}
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()
if err != nil {
return err
}
return nil
}
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}}
file, err := driveActions.filesService.Copy(fileId, copyFile).Fields("*").Context(driveContext).Do()
if err != nil {
return nil, err
}
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
}