Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add support for downloading file

Neeraj Guptabc7a8418ee85
diff --git a/internal/api/client.go b/internal/api/client.go
index 7cd28c4302..38756f7f93 100644
--- a/internal/api/client.go
+++ b/internal/api/client.go
@@ -19,6 +19,8 @@ var tokenMap map[string]string = make(map[string]string)
type Client struct {
restClient *resty.Client
+ // use separate client for downloading files
+ downloadClient *resty.Client
}
type Params struct {
@@ -34,6 +36,7 @@ func readValueFromContext(ctx context.Context, key string) interface{} {
func NewClient(p Params) *Client {
enteAPI := resty.New()
+
if p.Trace {
enteAPI.EnableTrace()
}
@@ -43,12 +46,7 @@ func NewClient(p Params) *Client {
panic("app not set in context")
}
req.Header.Set(ClientPkgHeader, StringToApp(app.(string)).ClientPkg())
- accountId := readValueFromContext(req.Context(), "account_id")
- if accountId != nil && accountId != "" {
- if token, ok := tokenMap[accountId.(string)]; ok {
- req.SetHeader(TokenHeader, token)
- }
- }
+ attachToken(req)
return nil
})
if p.Debug {
@@ -68,7 +66,17 @@ func NewClient(p Params) *Client {
enteAPI.SetBaseURL(EnteAPIEndpoint)
}
return &Client{
- restClient: enteAPI,
+ restClient: enteAPI,
+ downloadClient: resty.New(),
+ }
+}
+
+func attachToken(req *resty.Request) {
+ accountId := readValueFromContext(req.Context(), "account_id")
+ if accountId != nil && accountId != "" {
+ if token, ok := tokenMap[accountId.(string)]; ok {
+ req.SetHeader(TokenHeader, token)
+ }
}
}
diff --git a/internal/api/files.go b/internal/api/files.go
index 778f64ec17..2e4af77015 100644
--- a/internal/api/files.go
+++ b/internal/api/files.go
@@ -1 +1,25 @@
package api
+
+import (
+ "context"
+ "strconv"
+)
+
+var (
+ downloadHost = "https://files.ente.io/?fileID="
+)
+
+func (c *Client) DownloadFile(ctx context.Context, fileID int64, absolutePath string) error {
+ req := c.downloadClient.R().
+ SetContext(ctx).
+ SetOutput(absolutePath)
+ attachToken(req)
+ r, err := req.Get(downloadHost + strconv.FormatInt(fileID, 10))
+ if r.IsError() {
+ return &ApiError{
+ StatusCode: r.StatusCode(),
+ Message: r.String(),
+ }
+ }
+ return err
+}
diff --git a/pkg/collections.go b/pkg/collections.go
index cf12c5e5de..3791149642 100644
--- a/pkg/collections.go
+++ b/pkg/collections.go
@@ -109,6 +109,7 @@ func (c *ClICtrl) fetchRemoteFiles(ctx context.Context) error {
}
return nil
}
+
func (c *ClICtrl) getRemoteAlbums(ctx context.Context) ([]model.Album, error) {
albums := make([]model.Album, 0)
albumBytes, err := c.GetAllValues(ctx, model.RemoteAlbums)
diff --git a/pkg/download.go b/pkg/download.go
new file mode 100644
index 0000000000..c747eae801
--- /dev/null
+++ b/pkg/download.go
@@ -0,0 +1,47 @@
+package pkg
+
+import (
+ "cli-go/pkg/model"
+ "context"
+ "encoding/json"
+ "fmt"
+ "log"
+ "os"
+)
+
+func (c *ClICtrl) initiateDownload(ctx context.Context) error {
+ files, err := c.getRemoteFiles(ctx)
+ if err != nil {
+ return err
+ }
+ dir, err := os.MkdirTemp("", "photos-download")
+ if err != nil {
+ return err
+ }
+ for _, file := range files {
+ downloadPath := fmt.Sprintf("%s/%d", dir, file.ID)
+ log.Printf("Downloading file %d to %s", file.ID, downloadPath)
+ //err = c.Client.DownloadFile(ctx, file.ID, downloadPath)
+ //if err != nil {
+ // return err
+ //}
+ }
+ return nil
+}
+
+func (c *ClICtrl) getRemoteFiles(ctx context.Context) ([]model.PhotoFile, error) {
+ files := make([]model.PhotoFile, 0)
+ fileBytes, err := c.GetAllValues(ctx, model.RemoteFiles)
+ if err != nil {
+ return nil, err
+ }
+ for _, fileJson := range fileBytes {
+ file := model.PhotoFile{}
+ err = json.Unmarshal(fileJson, &file)
+ if err != nil {
+ return nil, err
+ }
+ files = append(files, file)
+ }
+ return files, nil
+}
diff --git a/pkg/remote_sync.go b/pkg/remote_sync.go
index 759b4661b6..f2e3c5c8c0 100644
--- a/pkg/remote_sync.go
+++ b/pkg/remote_sync.go
@@ -28,6 +28,11 @@ func (c *ClICtrl) SyncAccount(account model.Account) error {
if err != nil {
log.Printf("Error fetching files: %s", err)
}
+ downloadErr := c.initiateDownload(ctx)
+ if downloadErr != nil {
+ log.Printf("Error downloading files: %s", downloadErr)
+ return downloadErr
+ }
return nil
}