wfs

package
v0.0.0-...-406c194 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

Web File System - core interface

File system abstraction with access management.

API provides common file operations for some folder on local drive. Any operations outside of the folder will be blocked. Also, it possible to configure a custom policy for read/write operations.

Can be used as backend for Webix File Manager https://webix.com/filemanager

API

Initialization
import (
	"github.com/xbsoftware/wfs-local"
)

fs, err := wfs.NewLocalDrive("./sandbox", nil)
Get data
//get files in a folder
files, err := fs.List("/subfolder");

//get files in a folder and subfolders as plain list
files, err = fs.List("/subfolder", &wfs.ListConfig{ SubFolders: true });

//get files in a folder and subfolders as nested structure
files, err = fs.List("/subfolder", &wfs.ListConfig{ SubFolders: true, Nested:true });

//get folder only
files, err = fs.List("/subfolder", &wfs.ListConfig{ SkipFiles: true });

//get files that match a mask
files, err = fs.List("/subfolder", &wfs.ListConfig{
    Include: func(file string) bool { return strings.HasSufix(file, ".txt") },
});

//ignore some files
files, err = fs.List("/subfolder", &wfs.ListConfig{
    Exclude: func(file string) bool { return file == ".git" },
});

//get info about a single file
info, err = fs.Info("some.txt");

//check if file exists
check := fs.Exists("some.txt");
Modify files
//make folder
fs.Make("/", "sub2", true);

//make file
fs.Make("/", "my.txt", false);

//remove
fs.Remove("some.txt");

//copy
fs.Copy("some.txt", "/sub/", "");

//copy as
fs.Copy("some.txt", "/sub/", "other.txt");

//move
fs.Move("some.txt", "/data/", "");

//rename
fs.Move("some.txt", "", "some-data.txt");

//read
reader, err := fs.Read("some.txt");

//write
fs.Write("some.txt", writer)
Configuration
// Access policies
// ForceRoot policy is added automatically
fs, err := wfs.NewLocalDrive("./sandbox", &wfs.DriveConfig{
    Policy: &ReadOnlyPolicy{},
})

// Logging
fs, err := wfs.NewLocalDrive("./sandbox", &wfs.DriveConfig{
    Verbose:true
})

// Exec operation with different config
path, err := fs.WithOperationConfig(&OperationConfig{
    PreventNameCollision:true,
}).Make("/", "some", true)
License

MIT

Documentation

Index

Constants

View Source
const (
	ReadOperation int = iota
	WriteOperation
)

Supported operation modes

Variables

This section is empty.

Functions

func GetType

func GetType(name string, isFolder bool) string

Types

type Adapter

type Adapter interface {
	// implements Policy
	Comply(FileID, int) bool

	// converts client id <-> server id
	ToFileID(id string) FileID
	GetParent(f FileID) FileID

	// file operations
	List(id FileID) ([]FileInfo, error)
	Search(id FileID, search string) ([]FileInfo, error)
	Remove(id FileID) error
	Read(id FileID) (io.ReadSeeker, error)
	Write(id FileID, data io.Reader) error
	Make(id FileID, name string, isFolder bool) (FileID, error)
	Copy(source, target FileID, name string, isFolder bool) (FileID, error)
	Move(source, target FileID, name string, isFolder bool) (FileID, error)
	Info(id FileID) (FileInfo, error)
	Exists(id FileID, name string) bool
	Stats() (uint64, uint64, error)
}

type AllowPolicy

type AllowPolicy struct{}

AllowPolicy allows all operations

func (AllowPolicy) Comply

func (p AllowPolicy) Comply(path FileID, operation int) bool

Comply method returns true

type CombinedPolicy

type CombinedPolicy struct {
	Policies []Policy
}

CombinedPolicy allows to join multiple policies together

func (CombinedPolicy) Comply

func (p CombinedPolicy) Comply(path FileID, operation int) bool

Comply method returns true only when all joined policies are complied

type DenyPolicy

type DenyPolicy struct{}

DenyPolicy allows all operations

func (DenyPolicy) Comply

func (p DenyPolicy) Comply(path FileID, operation int) bool

Comply method returns false

type Drive

type Drive interface {
	List(id string, config ...*ListConfig) ([]File, error)
	Search(id, search string, config ...*ListConfig) ([]File, error)
	Remove(id string) error
	Read(id string) (io.ReadSeeker, error)
	Write(id string, data io.Reader) error
	Exists(id string) bool
	Info(id string) (File, error)
	Make(id, name string, isFolder bool) (string, error)
	Copy(source, target, name string) (string, error)
	Move(source, target, name string) (string, error)
	Stats() (uint64, uint64, error)
}

File stores info about single file

func NewDrive

func NewDrive(adapter Adapter, config *DriveConfig) Drive

NewLocalDrive returns new LocalDrive object which represents the file folder on local drive due to ForceRootPolicy all operations outside of the root folder will be blocked

type DriveConfig

type DriveConfig struct {
	Verbose   bool
	List      *ListConfig
	Operation *OperationConfig
	Policy    *Policy
}

DriveConfig contains drive configuration

type DriveFacade

type DriveFacade struct {
	// contains filtered or unexported fields
}

Drive represents an isolated file system

func (*DriveFacade) Copy

func (d *DriveFacade) Copy(source, target, name string) (string, error)

Copy makes a copy of file or a folder

func (*DriveFacade) Exists

func (d *DriveFacade) Exists(id string) bool

Exists checks is file / folder with defined path does exist

func (*DriveFacade) Info

func (d *DriveFacade) Info(id string) (File, error)

Info returns info about a single file / folder

func (*DriveFacade) List

func (d *DriveFacade) List(id string, config ...*ListConfig) ([]File, error)

List method returns array of files from the target folder

func (*DriveFacade) Make

func (d *DriveFacade) Make(id, name string, isFolder bool) (string, error)

Mkdir creates a new folder

func (*DriveFacade) Move

func (d *DriveFacade) Move(source, target, name string) (string, error)

Move renames(moves) a file or a folder

func (*DriveFacade) Read

func (d *DriveFacade) Read(id string) (io.ReadSeeker, error)

Read returns content of a file

func (*DriveFacade) Remove

func (d *DriveFacade) Remove(id string) error

Remove deletes a file or a folder

func (*DriveFacade) Search

func (d *DriveFacade) Search(id, search string, config ...*ListConfig) ([]File, error)

func (*DriveFacade) Stats

func (d *DriveFacade) Stats() (uint64, uint64, error)

func (*DriveFacade) WithOperationConfig

func (d *DriveFacade) WithOperationConfig(config *OperationConfig) Drive

WithOperationConfig makes a copy of drive with new operation config

func (*DriveFacade) Write

func (d *DriveFacade) Write(id string, data io.Reader) error

Write saves content to a file

type File

type File struct {
	Name  string `json:"value"`
	ID    string `json:"id"`
	Size  int64  `json:"size"`
	Date  int64  `json:"date"`
	Type  string `json:"type"`
	Files []File `json:"data,omitempty"`
}

File stores info about single file

type FileID

type FileID interface {
	GetPath() string
	ClientID() string
	IsFolder() bool
	Contains(FileID) bool
}

type FileInfo

type FileInfo interface {
	os.FileInfo
	File() FileID
}

type ListConfig

type ListConfig struct {
	SkipFiles  bool
	SubFolders bool
	Nested     bool
	Exclude    MatcherFunc
	Include    MatcherFunc
}

ListConfig contains file listing options

type MatcherFunc

type MatcherFunc func(string) bool

MatcherFunc receives path and returns true if path matches the rule

type OperationConfig

type OperationConfig struct {
	PreventNameCollision bool
}

OperationConfig contains file operation options

type Policy

type Policy interface {
	// Comply method returns true is operation for the path is allowed
	Comply(FileID, int) bool
}

Policy is a rule which allows or denies operation

type ReadOnlyPolicy

type ReadOnlyPolicy struct{}

ReadOnlyPolicy allows read access and blocks any modifications

func (ReadOnlyPolicy) Comply

func (p ReadOnlyPolicy) Comply(path FileID, operation int) bool

Comply method returns true for read operations

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL