Add support for downloading file
This commit is contained in:
parent
fc0abaff5a
commit
bc7a8418ee
5 changed files with 92 additions and 7 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
47
pkg/download.go
Normal file
47
pkg/download.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue