Browse Source

Add support for downloading file

Neeraj Gupta 1 year ago
parent
commit
bc7a8418ee
5 changed files with 92 additions and 7 deletions
  1. 15 7
      internal/api/client.go
  2. 24 0
      internal/api/files.go
  3. 1 0
      pkg/collections.go
  4. 47 0
      pkg/download.go
  5. 5 0
      pkg/remote_sync.go

+ 15 - 7
internal/api/client.go

@@ -19,6 +19,8 @@ var tokenMap map[string]string = make(map[string]string)
 
 
 type Client struct {
 type Client struct {
 	restClient *resty.Client
 	restClient *resty.Client
+	// use separate client for downloading files
+	downloadClient *resty.Client
 }
 }
 
 
 type Params struct {
 type Params struct {
@@ -34,6 +36,7 @@ func readValueFromContext(ctx context.Context, key string) interface{} {
 
 
 func NewClient(p Params) *Client {
 func NewClient(p Params) *Client {
 	enteAPI := resty.New()
 	enteAPI := resty.New()
+
 	if p.Trace {
 	if p.Trace {
 		enteAPI.EnableTrace()
 		enteAPI.EnableTrace()
 	}
 	}
@@ -43,12 +46,7 @@ func NewClient(p Params) *Client {
 			panic("app not set in context")
 			panic("app not set in context")
 		}
 		}
 		req.Header.Set(ClientPkgHeader, StringToApp(app.(string)).ClientPkg())
 		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
 		return nil
 	})
 	})
 	if p.Debug {
 	if p.Debug {
@@ -68,7 +66,17 @@ func NewClient(p Params) *Client {
 		enteAPI.SetBaseURL(EnteAPIEndpoint)
 		enteAPI.SetBaseURL(EnteAPIEndpoint)
 	}
 	}
 	return &Client{
 	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)
+		}
 	}
 	}
 }
 }
 
 

+ 24 - 0
internal/api/files.go

@@ -1 +1,25 @@
 package api
 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
+}

+ 1 - 0
pkg/collections.go

@@ -109,6 +109,7 @@ func (c *ClICtrl) fetchRemoteFiles(ctx context.Context) error {
 	}
 	}
 	return nil
 	return nil
 }
 }
+
 func (c *ClICtrl) getRemoteAlbums(ctx context.Context) ([]model.Album, error) {
 func (c *ClICtrl) getRemoteAlbums(ctx context.Context) ([]model.Album, error) {
 	albums := make([]model.Album, 0)
 	albums := make([]model.Album, 0)
 	albumBytes, err := c.GetAllValues(ctx, model.RemoteAlbums)
 	albumBytes, err := c.GetAllValues(ctx, model.RemoteAlbums)

+ 47 - 0
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
+}

+ 5 - 0
pkg/remote_sync.go

@@ -28,6 +28,11 @@ func (c *ClICtrl) SyncAccount(account model.Account) error {
 	if err != nil {
 	if err != nil {
 		log.Printf("Error fetching files: %s", err)
 		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
 	return nil
 }
 }