github.com/puellanivis/breton@v0.2.16/lib/files/local.go (about)

     1  package files
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"net/url"
     7  	"os"
     8  )
     9  
    10  type localFS struct{}
    11  
    12  // Local implements a wrapper from the os functions Open, Create, and Readdir, to the files.FileStore implementation.
    13  var Local FileStore = &localFS{}
    14  
    15  func init() {
    16  	RegisterScheme(Local, "file")
    17  }
    18  
    19  func filename(uri *url.URL) string {
    20  	fname := uri.Path
    21  	if fname == "" {
    22  		fname = uri.Opaque
    23  	}
    24  
    25  	return fname
    26  }
    27  
    28  // Open opens up a local filesystem file specified in the uri.Path for reading.
    29  func (h *localFS) Open(ctx context.Context, uri *url.URL) (Reader, error) {
    30  	return os.Open(filename(uri))
    31  }
    32  
    33  // Create opens up a local filesystem file specified in the uri.Path for writing. It will create a new one if it does not exist.
    34  func (h *localFS) Create(ctx context.Context, uri *url.URL) (Writer, error) {
    35  	return os.Create(filename(uri))
    36  }
    37  
    38  // List returns the whole slice of os.FileInfos for a specific local filesystem at uri.Path.
    39  func (h *localFS) List(ctx context.Context, uri *url.URL) ([]os.FileInfo, error) {
    40  	return ioutil.ReadDir(filename(uri))
    41  }