fsutil

package
v5.0.0-preview6 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: MIT Imports: 10 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileExists

func FileExists(fs fs.StatFS, file string) bool

FileExists returns true if the file at the given path exists and is readable. Returns false if the given file is a directory.

func GetFileExtension

func GetFileExtension(filename string) string

GetFileExtension returns the last part of a file name. If the file doesn't have an extension, returns an empty string.

func GetMIMEType

func GetMIMEType(filesystem fs.FS, file string) (contentType string, size int64, err error)

GetMIMEType get the mime type and size of the given file. This function calls `http.DetectContentType`. If the detected content type could not be determined or if it's a text file, `GetMIMEType` will attempt to detect the MIME type based on the file extension. The following extensions are supported:

  • `.jsonld`: "application/ld+json"
  • `.json`: "application/json"
  • `.js` / `.mjs`: "text/javascript"
  • `.css`: "text/css"

If a specific MIME type cannot be determined, returns "application/octet-stream" as a fallback.

func IsDirectory

func IsDirectory(fs fs.StatFS, path string) bool

IsDirectory returns true if the file at the given path exists, is a directory and is readable.

Types

type Embed

type Embed struct {
	embed.FS
}

Embed is an extension of `embed.FS` implementing `fs.StatFS`.

func (Embed) Stat

func (e Embed) Stat(name string) (fileinfo fs.FileInfo, err error)

Stat returns a FileInfo describing the file.

type FS

type FS interface {
	fs.ReadDirFS
	fs.StatFS
}

An FS provides access to a hierarchical file system and implements `io/fs`'s `FS`, `ReadDirFS` and `StatFS` interfaces.

type File

type File struct {
	Header   *multipart.FileHeader
	MIMEType string
}

File represents a file received from client.

func ParseMultipartFiles

func ParseMultipartFiles(headers []*multipart.FileHeader) ([]File, error)

ParseMultipartFiles parse a single file field in a request.

func (*File) Save

func (file *File) Save(fs WritableFS, path string, name string) (filename string, err error)

Save writes the given file on the disk. Appends a timestamp to the given file name to avoid duplicate file names. The file is not readable anymore once saved as its FileReader has already been closed.

Creates directories if needed.

Returns the actual file name.

type MkdirFS

type MkdirFS interface {
	// MkdirAll creates a directory named path,
	// along with any necessary parents, and returns `nil`,
	// or else returns an error.
	// The permission bits perm (before umask) are used for all
	// directories that `MkdirAll` creates.
	// If path is already a directory, `MkdirAll` does nothing
	// and returns `nil`.
	MkdirAll(path string, perm fs.FileMode) error

	// Mkdir creates a new directory with the specified name and permission
	// bits (before umask).
	// If there is an error, it will be of type `*PathError`.
	Mkdir(path string, perm fs.FileMode) error
}

A MkdirFS is a file system with a `Mkdir()` and a `MkdirAll()` methods.

type RemoveFS

type RemoveFS interface {
	// Remove removes the named file or (empty) directory.
	// If there is an error, it will be of type `*PathError`.
	Remove(path string) error

	// RemoveAll removes path and any children it contains.
	// It removes everything it can but returns the first error
	// it encounters. If the path does not exist, `RemoveAll`
	// returns `nil` (no error).
	// If there is an error, it will be of type `*PathError`.
	RemoveAll(path string) error
}

A RemoveFS is a file system with a `Remove()` and a `RemoveAll()` methods.

type WorkingDirFS

type WorkingDirFS interface {
	// Getwd returns a rooted path name corresponding to the
	// current directory. If the current directory can be
	// reached via multiple paths (due to symbolic links),
	// Getwd may return any one of them.
	Getwd() (dir string, err error)
}

A WorkingDirFS is a file system with a `Getwd()` method.

type WritableFS

type WritableFS interface {
	// OpenFile is the generalized open call. It opens the named file with specified flag
	// (`O_RDONLY` etc.). If the file does not exist, and the `O_CREATE` flag
	// is passed, it is created with mode perm (before umask). If successful,
	// methods on the returned file can be used for I/O.
	// If there is an error, it will be of type `*PathError`.
	OpenFile(path string, flag int, perm fs.FileMode) (io.ReadWriteCloser, error)
}

A WritableFS is a file system with a `OpenFile()` method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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