fsutil

package
v0.5.13 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 9 Imported by: 110

Documentation

Index

Constants

View Source
const (
	FsCWAFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND // create, append write-only
	FsCWTFlags = os.O_CREATE | os.O_WRONLY | os.O_TRUNC  // create, override write-only
	FsCWFlags  = os.O_CREATE | os.O_WRONLY               // create, write-only
	FsRFlags   = os.O_RDONLY                             // read-only
)

some flag consts for open file

View Source
const (
	// MimeSniffLen sniff Length, use for detect file mime type
	MimeSniffLen = 512
)

Variables

View Source
var (
	// DefaultDirPerm perm and flags for create log file
	DefaultDirPerm   os.FileMode = 0775
	DefaultFilePerm  os.FileMode = 0665
	OnlyReadFilePerm os.FileMode = 0444

	// DefaultFileFlags for create and write
	DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND
	// OnlyReadFileFlags open file for read
	OnlyReadFileFlags = os.O_RDONLY
)
View Source
var (
	DirExist  = IsDir
	FileExist = IsFile
	PathExist = PathExists
)

alias methods

View Source
var (
	// MustRm removes the named file or (empty) directory.
	MustRm = MustRemove
	// QuietRm removes the named file or (empty) directory.
	QuietRm = QuietRemove
)

alias methods

View Source
var ImageMimeTypes = map[string]string{
	"bmp": "image/bmp",
	"gif": "image/gif",
	"ief": "image/ief",
	"jpg": "image/jpeg",

	"jpeg": "image/jpeg",
	"png":  "image/png",
	"svg":  "image/svg+xml",
	"ico":  "image/x-icon",
	"webp": "image/webp",
}

ImageMimeTypes refer net/http package

Functions

func CopyFile added in v0.3.8

func CopyFile(srcPath, dstPath string) error

CopyFile copy a file to another file path.

func CreateFile added in v0.2.10

func CreateFile(fpath string, filePerm, dirPerm os.FileMode, fileFlag ...int) (*os.File, error)

CreateFile create file if not exists

Usage:

CreateFile("path/to/file.txt", 0664, 0666)

func DeleteIfExist added in v0.3.11

func DeleteIfExist(fPath string) error

DeleteIfExist removes the named file or (empty) directory on exists.

func DeleteIfFileExist added in v0.2.10

func DeleteIfFileExist(fPath string) error

DeleteIfFileExist removes the named file on exists.

func Dir added in v0.3.10

func Dir(fpath string) string

Dir get dir path, without last name.

func DiscardReader added in v0.4.5

func DiscardReader(src io.Reader)

DiscardReader anything from the reader

func Expand added in v0.5.3

func Expand(pathStr string) string

Expand will parse first `~` as user home dir path.

func ExpandPath added in v0.3.9

func ExpandPath(pathStr string) string

ExpandPath will parse `~` as user home dir path.

func FileExists

func FileExists(path string) bool

FileExists reports whether the named file or directory exists.

func FileExt added in v0.3.8

func FileExt(fpath string) string

FileExt get filename ext. alias of path.Ext()

func FindInDir added in v0.5.3

func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)

FindInDir code refer the go pkg: path/filepath.glob() filters: return false will skip the file.

func GetContents added in v0.5.3

func GetContents(in interface{}) []byte

GetContents read contents from path or io.Reader, will panic on error

func GlobWithFunc added in v0.5.2

func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)

GlobWithFunc handle matched file

func IsAbsPath

func IsAbsPath(aPath string) bool

IsAbsPath is abs path.

func IsDir added in v0.2.1

func IsDir(path string) bool

IsDir reports whether the named directory exists.

func IsFile added in v0.2.1

func IsFile(path string) bool

IsFile reports whether the named file or directory exists.

func IsImageFile added in v0.2.5

func IsImageFile(path string) bool

IsImageFile check file is image file.

func IsZipFile

func IsZipFile(filepath string) bool

IsZipFile check is zip file. from https://blog.csdn.net/wangshubo1989/article/details/71743374

func MimeType added in v0.2.5

func MimeType(path string) (mime string)

MimeType get File Mime Type name. eg "image/png"

func MkParentDir added in v0.3.11

func MkParentDir(fpath string) error

MkParentDir quick create parent dir

func Mkdir added in v0.2.2

func Mkdir(dirPath string, perm os.FileMode) error

Mkdir alias of os.MkdirAll()

func MustCopyFile added in v0.3.8

func MustCopyFile(srcPath, dstPath string)

MustCopyFile copy file to another path.

func MustCreateFile added in v0.3.8

func MustCreateFile(filePath string, filePerm, dirPerm os.FileMode) *os.File

MustCreateFile create file, will panic on error

func MustReadFile added in v0.3.7

func MustReadFile(filePath string) []byte

MustReadFile read file contents, will panic on error

func MustReadReader added in v0.4.3

func MustReadReader(r io.Reader) []byte

MustReadReader read contents from io.Reader, will panic on error

func MustRemove added in v0.3.11

func MustRemove(fPath string)

MustRemove removes the named file or (empty) directory. NOTICE: will panic on error

func Name added in v0.3.10

func Name(fpath string) string

Name get file/dir name from full path

func OSTempDir added in v0.3.12

func OSTempDir(pattern string) (string, error)

OSTempDir creates a new temp dir on os.TempDir and return the temp dir path

Usage:

fsutil.OSTempDir("example.*")

func OSTempFile added in v0.3.12

func OSTempFile(pattern string) (*os.File, error)

OSTempFile create a temp file on os.TempDir()

Usage:

fsutil.OSTempFile("example.*.txt")

func OpenFile added in v0.3.7

func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error)

OpenFile like os.OpenFile, but will auto create dir.

func OpenReadFile added in v0.5.3

func OpenReadFile(filepath string) (*os.File, error)

OpenReadFile like os.OpenFile, open file for read contents

func PathExists added in v0.2.1

func PathExists(path string) bool

PathExists reports whether the named file or directory exists.

func PathName added in v0.5.0

func PathName(fpath string) string

PathName get file/dir name from full path

func PutContents added in v0.5.3

func PutContents(filePath string, data interface{}, fileFlag ...int) (int, error)

PutContents create file and write contents to file at once.

data type allow: string, []byte, io.Reader

Tip: file flag default is FsCWAFlags

Usage:

fsutil.PutContents(filePath, contents, fsutil.FsCWTFlags)

func QuickOpenFile added in v0.3.8

func QuickOpenFile(filepath string, fileFlag ...int) (*os.File, error)

QuickOpenFile like os.OpenFile, open for write, if not exists, will create it.

Tip: file flag default is FsCWAFlags

func QuietRemove added in v0.3.11

func QuietRemove(fPath string)

QuietRemove removes the named file or (empty) directory.

NOTICE: will ignore error

func ReadExistFile added in v0.3.11

func ReadExistFile(filePath string) []byte

ReadExistFile read file contents if existed, will panic on error

func ReaderMimeType added in v0.2.5

func ReaderMimeType(r io.Reader) (mime string)

ReaderMimeType get the io.Reader mimeType

Usage:

file, err := os.Open(filepath)
if err != nil {
	return
}
mime := ReaderMimeType(file)

func Realpath added in v0.4.0

func Realpath(pathStr string) string

Realpath returns the shortest path name equivalent to path by purely lexical processing.

func Remove added in v0.5.0

func Remove(fPath string) error

Remove removes the named file or (empty) directory.

func RmFileIfExist added in v0.5.3

func RmFileIfExist(fPath string) error

RmFileIfExist removes the named file on exists.

func RmIfExist added in v0.5.3

func RmIfExist(fPath string) error

RmIfExist removes the named file or (empty) directory on exists.

func SplitPath added in v0.5.6

func SplitPath(pathStr string) (dir, name string)

SplitPath splits path immediately following the final Separator, separating it into a directory and file name component

func Suffix added in v0.3.8

func Suffix(fpath string) string

Suffix get filename ext. alias of path.Ext()

func TempDir added in v0.3.12

func TempDir(dir, pattern string) (string, error)

TempDir creates a new temp dir and return the temp dir path

Usage:

fsutil.TempDir("", "example.*")
fsutil.TempDir("testdata", "example.*")

func TempFile added in v0.3.12

func TempFile(dir, pattern string) (*os.File, error)

TempFile is like ioutil.TempFile, but can custom temp dir.

Usage:

fsutil.TempFile("", "example.*.txt")

func Unzip

func Unzip(archive, targetDir string) (err error)

Unzip a zip archive from https://blog.csdn.net/wangshubo1989/article/details/71743374

func WriteFile added in v0.5.8

func WriteFile(filePath string, data interface{}, perm os.FileMode, fileFlag ...int) error

WriteFile create file and write contents to file, can set perm for file.

data type allow: string, []byte, io.Reader

Tip: file flag default is FsCWTFlags

Usage:

fsutil.WriteFile(filePath, contents, 0666, fsutil.FsCWAFlags)

Types

type FilterFunc added in v0.3.8

type FilterFunc func(fPath string, fi os.FileInfo) bool

filter and handle func for FindInDir

type HandleFunc added in v0.5.3

type HandleFunc func(fPath string, fi os.FileInfo) error

filter and handle func for FindInDir

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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