diff --git a/cmd/files.com-test/main.go b/cmd/files.com-test/main.go index 23fabc4..36a3ccd 100644 --- a/cmd/files.com-test/main.go +++ b/cmd/files.com-test/main.go @@ -1,10 +1,13 @@ package main import ( + "context" "fmt" "os" + "path/filepath" files_sdk "github.com/Files-com/files-sdk-go" + "github.com/Files-com/files-sdk-go/file" "github.com/Files-com/files-sdk-go/folder" "github.com/canonical/athena-core/pkg/common" "github.com/canonical/athena-core/pkg/config" @@ -16,6 +19,7 @@ var configs = common.StringList( ) var path = kingpin.Flag("path", "Path to check").Default("/").String() +var uploadFile = kingpin.Flag("upload", "File to upload to path").String() func main() { kingpin.HelpFlag.Short('h') @@ -31,8 +35,18 @@ func main() { Endpoint: cfg.FilesCom.Endpoint, } + if *uploadFile != "" { + filename := filepath.Base(*uploadFile) + fileClient := file.Client{Config: filesConfig} + status, err := fileClient.UploadFile(context.Background(), &file.UploadParams{Source: *uploadFile, Destination: filepath.Join(*path, filename)}) + if err != nil { + fmt.Printf("Error uploading file %s: %s", *uploadFile, err) + } + fmt.Println(status.Files()) + } + folderClient := folder.Client{Config: filesConfig} - folderIterator, err := folderClient.ListFor(files_sdk.FolderListForParams{Path: *path}) + folderIterator, err := folderClient.ListFor(context.Background(), files_sdk.FolderListForParams{Path: *path}) if err != nil { fmt.Printf("Error reading folder: %s", err) os.Exit(1) diff --git a/go.mod b/go.mod index 38cea74..8de23d1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/canonical/athena-core go 1.22 require ( - github.com/Files-com/files-sdk-go v1.0.1139 + github.com/Files-com/files-sdk-go v1.1.1145 github.com/flosch/pongo2/v4 v4.0.2 github.com/lileio/pubsub/v2 v2.6.1 github.com/makyo/snuffler v0.0.0-20190210075944-33446730a4fe @@ -25,6 +25,7 @@ require ( github.com/appscode/go-querystring v0.0.0-20170504095604-0126cfb3f1dc // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/chilts/sid v0.0.0-20190607042430-660e94789ec9 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dropbox/godropbox v0.0.0-20200228041828-52ad444d3502 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect diff --git a/go.sum b/go.sum index b5c9848..a3b4918 100644 --- a/go.sum +++ b/go.sum @@ -50,8 +50,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DataDog/datadog-go v2.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= -github.com/Files-com/files-sdk-go v1.0.1139 h1:nVgljKqenvrxPDfAfSRrYjPY9mZOK/xHwzIzj3YDHEM= -github.com/Files-com/files-sdk-go v1.0.1139/go.mod h1:u1HNt8r69feG2tndNA/HZGfkpdkXyrhR6rWJaQ2dVLM= +github.com/Files-com/files-sdk-go v1.1.1145 h1:XmrD0805kGbLIQ0pKpSOZIw7B2a3GV4XHkgDR+HBAIE= +github.com/Files-com/files-sdk-go v1.1.1145/go.mod h1:/qeqiWrGdmRwnHi3ySD9sQ9dgaxBCmvb68a9irT6568= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= @@ -76,6 +76,8 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/chilts/sid v0.0.0-20190607042430-660e94789ec9 h1:z0uK8UQqjMVYzvk4tiiu3obv2B44+XBsvgEJREQfnO8= +github.com/chilts/sid v0.0.0-20190607042430-660e94789ec9/go.mod h1:Jl2neWsQaDanWORdqZ4emBl50J4/aRBBS4FyyG9/PFo= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= diff --git a/pkg/common/files-com.go b/pkg/common/files-com.go index 9aa4e69..4e7fa48 100644 --- a/pkg/common/files-com.go +++ b/pkg/common/files-com.go @@ -1,9 +1,10 @@ package common import ( + "context" + "os" "path" "path/filepath" - "strings" "time" filessdk "github.com/Files-com/files-sdk-go" @@ -28,18 +29,25 @@ type BaseFilesComClient struct { func (client *BaseFilesComClient) Upload(contents, destinationPath string) (*filessdk.File, error) { log.Infof("Uploading to '%s'", destinationPath) - data := strings.NewReader(contents) - size := int64(data.Len()) - fileEntry, err := client.ApiClient.Upload(data, size, filessdk.FileActionBeginUploadParams{Path: destinationPath}, &file.UploadProgress{}) + tmpfile, err := os.CreateTemp("", "upload") if err != nil { return nil, err } - return &fileEntry, nil + err = os.WriteFile(tmpfile.Name(), []byte(contents), 0644) + if err != nil { + return nil, err + } + defer os.Remove(tmpfile.Name()) + status, err := client.ApiClient.UploadFile(context.Background(), &file.UploadParams{Source: tmpfile.Name(), Destination: destinationPath}) + if err != nil { + return nil, err + } + return &status.Files()[0], nil } func (client *BaseFilesComClient) Download(toDownload *db.File, downloadPath string) (*filessdk.File, error) { log.Infof("Downloading '%s' to '%s'", toDownload.Path, downloadPath) - fileEntry, err := client.ApiClient.DownloadToFile(filessdk.FileDownloadParams{Path: toDownload.Path}, path.Join(downloadPath, filepath.Base(toDownload.Path))) + fileEntry, err := client.ApiClient.DownloadToFile(context.Background(), filessdk.FileDownloadParams{Path: toDownload.Path}, path.Join(downloadPath, filepath.Base(toDownload.Path))) if err != nil { return nil, err } @@ -53,7 +61,7 @@ func (client *BaseFilesComClient) GetFiles(dirs []string) ([]db.File, error) { for _, directory := range dirs { log.Infof("Listing files available on %s", directory) params := filessdk.FolderListForParams{Path: directory} - it, err := newClient.ListFor(params) + it, err := newClient.ListFor(context.Background(), params) if err != nil { return nil, err }