github.com/code-to-go/safepool.lib@v0.0.0-20221205180519-ee25e63c226e/transport/exchanger.go (about)

     1  package transport
     2  
     3  import (
     4  	"io"
     5  	"io/fs"
     6  	"os"
     7  
     8  	"github.com/code-to-go/safepool.lib/core"
     9  )
    10  
    11  type Source struct {
    12  	Name   string
    13  	Data   []byte
    14  	Reader io.Reader
    15  	Size   int64
    16  }
    17  
    18  const SizeAll = -1
    19  
    20  type ListOption uint32
    21  
    22  const (
    23  	// IncludeHiddenFiles includes hidden files in a list operation
    24  	IncludeHiddenFiles ListOption = 1
    25  	// IsPrefix matches all the files starting with the path
    26  	IsPrefix = 2
    27  )
    28  
    29  type Range struct {
    30  	From int64
    31  	To   int64
    32  }
    33  
    34  // Exchanger is a low level interface to storage services such as S3 or SFTP
    35  type Exchanger interface {
    36  
    37  	//Touched returns true when some data has been written to the exchanger since the last time Touched was called
    38  	Touched(name string) bool
    39  
    40  	// Read reads data from a file into a writer
    41  	Read(name string, rang *Range, dest io.Writer) error
    42  
    43  	// Write writes data to a file name. An existing file is overwritten
    44  	Write(name string, source io.Reader) error
    45  
    46  	//ReadDir returns the entries of a folder content
    47  	ReadDir(name string, opts ListOption) ([]fs.FileInfo, error)
    48  
    49  	// Stat provides statistics about a file
    50  	Stat(name string) (os.FileInfo, error)
    51  
    52  	// Rename a file. Overwrite an existing file if present
    53  	Rename(old, new string) error
    54  
    55  	// Delete deletes a file
    56  	Delete(name string) error
    57  
    58  	// Close releases resources
    59  	Close() error
    60  
    61  	// String returns a human-readable representation of the storer (e.g. sftp://user@host.cc/path)
    62  	String() string
    63  }
    64  
    65  // NewExchanger creates a new exchanger giving a provided configuration
    66  func NewExchanger(c Config) (Exchanger, error) {
    67  	switch {
    68  	case c.SFTP != nil:
    69  		return NewSFTP(*c.SFTP)
    70  	case c.S3 != nil:
    71  		return NewS3(*c.S3)
    72  	case c.Local != nil:
    73  		return NewLocal(*c.Local)
    74  	}
    75  
    76  	return nil, core.ErrNoDriver
    77  }