Browse Source

Valid and use exportDir during sync

Neeraj Gupta 1 year ago
parent
commit
6285220c1b
4 changed files with 16 additions and 29 deletions
  1. 0 16
      pkg/disk.go
  2. 3 6
      pkg/remote_to_disk_album.go
  3. 2 5
      pkg/remote_to_disk_file.go
  4. 11 2
      pkg/sync.go

+ 0 - 16
pkg/disk.go

@@ -3,7 +3,6 @@ package pkg
 import (
 	"cli-go/pkg/model"
 	"cli-go/pkg/model/export"
-	"context"
 	"encoding/json"
 	"errors"
 	"fmt"
@@ -99,21 +98,6 @@ func readJSONFromFile(filePath string, data interface{}) error {
 	return decoder.Decode(data)
 }
 
-func exportHome(ctx context.Context) (string, error) {
-	homeDir, err := os.UserHomeDir()
-	if err != nil {
-		return "", err
-	}
-	path := fmt.Sprintf("%s/%s", homeDir, "photos")
-	if _, err = os.Stat(path); os.IsNotExist(err) {
-		err = os.Mkdir(path, 0755)
-		if err != nil {
-			return "", err
-		}
-	}
-	return path, nil
-}
-
 func validateExportDirectory(dir string) (bool, error) {
 	// Check if the path exists
 	fileInfo, err := os.Stat(dir)

+ 3 - 6
pkg/remote_to_disk_album.go

@@ -1,6 +1,7 @@
 package pkg
 
 import (
+	"cli-go/pkg/model"
 	"cli-go/pkg/model/export"
 	"context"
 	"encoding/json"
@@ -12,12 +13,8 @@ import (
 	"path/filepath"
 )
 
-func (c *ClICtrl) createLocalFolderForRemoteAlbums(ctx context.Context) error {
-	path, pathErr := exportHome(ctx)
-	if pathErr != nil {
-		return pathErr
-	}
-
+func (c *ClICtrl) createLocalFolderForRemoteAlbums(ctx context.Context, account model.Account) error {
+	path := account.ExportDir
 	albums, err := c.getRemoteAlbums(ctx)
 	if err != nil {
 		return err

+ 2 - 5
pkg/remote_to_disk_file.go

@@ -15,12 +15,9 @@ import (
 	"time"
 )
 
-func (c *ClICtrl) syncFiles(ctx context.Context) error {
+func (c *ClICtrl) syncFiles(ctx context.Context, account model.Account) error {
 	log.Printf("Starting sync files")
-	exportRoot, err := exportHome(ctx)
-	if err != nil {
-		return err
-	}
+	exportRoot := account.ExportDir
 	_, albumIDToMetaMap, err := readFolderMetadata(exportRoot)
 	if err != nil {
 		return err

+ 11 - 2
pkg/sync.go

@@ -21,6 +21,15 @@ func (c *ClICtrl) StartSync() error {
 	}
 	for _, account := range accounts {
 		log.SetPrefix(fmt.Sprintf("[%s-%s] ", account.App, account.Email))
+		if account.ExportDir == "" {
+			log.Printf("Skip account %s: no export directory configured", account.Email)
+			continue
+		}
+		_, err = validateExportDirectory(account.ExportDir)
+		if err != nil {
+			log.Printf("Skip export, error: %v while validing exportDir %s\n", err, account.ExportDir)
+			continue
+		}
 		log.Println("start sync")
 		err = c.SyncAccount(account)
 		if err != nil {
@@ -53,12 +62,12 @@ func (c *ClICtrl) SyncAccount(account model.Account) error {
 	if err != nil {
 		log.Printf("Error fetching files: %s", err)
 	}
-	err = c.createLocalFolderForRemoteAlbums(ctx)
+	err = c.createLocalFolderForRemoteAlbums(ctx, account)
 	if err != nil {
 		log.Printf("Error creating local folders: %s", err)
 		return err
 	}
-	err = c.syncFiles(ctx)
+	err = c.syncFiles(ctx, account)
 	if err != nil {
 		log.Printf("Error syncing files: %s", err)
 		return err