Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade files.com sdk #149

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion cmd/files.com-test/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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')
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
22 changes: 15 additions & 7 deletions pkg/common/files-com.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package common

import (
"context"
"os"
"path"
"path/filepath"
"strings"
"time"

filessdk "github.com/Files-com/files-sdk-go"
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
Loading