github.com/Files-com/files-sdk-go/v2@v2.1.2/file/syncactions.go (about)

     1  package file
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"syscall"
    10  
    11  	"github.com/Files-com/files-sdk-go/v2/lib"
    12  
    13  	files_sdk "github.com/Files-com/files-sdk-go/v2"
    14  	"github.com/Files-com/files-sdk-go/v2/directory"
    15  	"github.com/Files-com/files-sdk-go/v2/file/status"
    16  	"github.com/Files-com/files-sdk-go/v2/lib/direction"
    17  )
    18  
    19  // DeleteSource files after a sync
    20  //
    21  //	job.RegisterFileEvent(func(file status.File) {
    22  //			log, err := file.DeleteSource{Direction: f.Direction, Config: config}.Call(ctx, f)
    23  //	}, status.Complete, status.Skipped)
    24  type DeleteSource struct {
    25  	direction.Direction
    26  	Config files_sdk.Config
    27  }
    28  
    29  func (ad DeleteSource) Call(f status.File, opts ...files_sdk.RequestResponseOption) (status.Log, error) {
    30  	switch f.Direction {
    31  	case direction.UploadType:
    32  		return status.Log{Path: f.LocalPath, Action: "delete source"}, os.Remove(f.LocalPath)
    33  	case direction.DownloadType:
    34  		client := Client{Config: ad.Config}
    35  		err := client.Delete(files_sdk.FileDeleteParams{Path: f.RemotePath}, opts...)
    36  		return status.Log{Path: f.RemotePath, Action: "delete source"}, err
    37  	default:
    38  		panic(fmt.Sprintf("unknown direction %v", f.Direction))
    39  	}
    40  }
    41  
    42  // MoveSource files after a sync
    43  //
    44  //	job.RegisterFileEvent(func(file status.File) {
    45  //			log, err := file.MoveSource{Direction: f.Direction, Config: config}.Call(ctx, f)
    46  //	}, status.Complete, status.Skipped)
    47  type MoveSource struct {
    48  	direction.Direction
    49  	Path   string
    50  	Config files_sdk.Config
    51  }
    52  
    53  func (am MoveSource) Call(f status.File, opts ...files_sdk.RequestResponseOption) (status.Log, error) {
    54  	var err error
    55  	log := status.Log{Action: "move source"}
    56  	log.Path = am.movePath(f)
    57  
    58  	switch f.Direction {
    59  	case direction.UploadType:
    60  		dir, _ := filepath.Split(log.Path)
    61  		err = os.MkdirAll(dir, 0755)
    62  		if err != nil && !errors.Is(err, syscall.EEXIST) {
    63  			return log, err
    64  		}
    65  		err = os.Rename(f.LocalPath, log.Path)
    66  		if err != nil && errors.Is(err, syscall.EEXIST) {
    67  			err = os.Remove(log.Path)
    68  			if err != nil {
    69  				return log, err
    70  			}
    71  			return am.Call(f, opts...)
    72  		}
    73  		return log, err
    74  	case direction.DownloadType:
    75  		client := &Client{Config: am.Config}
    76  		_, err := client.Move(
    77  			files_sdk.FileMoveParams{Path: f.RemotePath, Destination: lib.Path{Path: log.Path}.NormalizePathSystemForAPI().String()},
    78  			opts...,
    79  		)
    80  		rErr, ok := err.(files_sdk.ResponseError)
    81  		if ok && rErr.Type == "processing-failure/destination-parent-does-not-exist" {
    82  			err := (&FS{}).Init(am.Config, true).WithContext(files_sdk.ContextOption(opts)).(*FS).MkdirAll(filepath.Dir(log.Path), 0755)
    83  			if err != nil {
    84  				return log, err
    85  			}
    86  			return am.Call(f, opts...)
    87  		}
    88  		if ok && rErr.Type == "processing-failure/destination-exists" {
    89  			err := client.Delete(files_sdk.FileDeleteParams{Path: lib.Path{Path: log.Path}.NormalizePathSystemForAPI().String()}, opts...)
    90  			if err != nil {
    91  				return log, err
    92  			}
    93  			return am.Call(f, opts...)
    94  		}
    95  		return log, err
    96  	default:
    97  		panic(fmt.Sprintf("unknown direction %v", f.Direction))
    98  	}
    99  }
   100  
   101  func (am MoveSource) movePath(f status.File) string {
   102  	switch f.Job.Type {
   103  	case directory.Dir:
   104  		return filepath.Join(
   105  			append([]string{am.Path}, strings.Split(strings.TrimPrefix(f.RemotePath, f.Job.RemotePath), "/")...)...,
   106  		)
   107  	case directory.File:
   108  		return am.Path
   109  	default:
   110  		panic("")
   111  	}
   112  }