fsimpl

package
v0.0.0-...-c37df24 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 11 Imported by: 6

Documentation

Overview

fsimpl contains helper functions for implementing a fs.FileSystem

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanPath

func CleanPath(p, separator string) string

func DirEntryFromFileInfo

func DirEntryFromFileInfo(info iofs.FileInfo) iofs.DirEntry

DirEntryFromFileInfo wraps a io/fs.FileInfo as io/fs.DirEntry.

func DropboxContentHash

func DropboxContentHash(ctx context.Context, reader io.Reader) (string, error)

DropboxContentHash returns a Dropbox compatible 64 hex character content hash by reading from an io.Reader until io.EOF or the ctx gets cancelled. See https://www.dropbox.com/developers/reference/content-hash

func Ext

func Ext(filePath, separator string) string

Ext returns the extension of filePath including the point, or an empty string. Example:

Ext("image.png", "/") == ".png"
Ext("image.png/file", "/") == ""
Example
fmt.Println(Ext("image.png", "/"))
fmt.Println(Ext("image.png", ""))
fmt.Println(Ext("image.66.png", "/"))
fmt.Println(Ext("file", "/") == "")
fmt.Println(Ext("dir.with.ext/file", "/") == "")
fmt.Println(Ext("dir.with.ext/file.ext", "/"))
fmt.Println(Ext("dir.with.ext/file", "\\"))
fmt.Println(Ext("dir.with.ext/file", ""))
Output:

.png
.png
.png
true
true
.ext
.ext/file
.ext/file

func JoinCleanPath

func JoinCleanPath(uriParts []string, trimPrefix, separator string) string

func MatchAnyPattern

func MatchAnyPattern(name string, patterns []string) (bool, error)

MatchAnyPattern returns true if name matches any of patterns, or if len(patterns) == 0. The match per pattern is checked via path.Match. FileSystem implementations can use this function to implement FileSystem.MatchAnyPattern they use "/" as path separator.

func RandomString

func RandomString() string

RandomString returns a 120 bit randum number encoded as URL compatible base64 string with a length of 20 characters.

func SplitDirAndName

func SplitDirAndName(filePath string, volumeLen int, separator string) (dir, name string)

SplitDirAndName is a generic helper for FileSystem.SplitDirAndName implementations. path.Split or filepath.Split don't have the wanted behaviour when given a path ending in a separator. SplitDirAndName returns the parent directory of filePath and the name with that directory of the last filePath element. If filePath is the root of the file systeme, then an empty string will be returned as name. If filePath does not contain a separator before the name part, then "." will be returned as dir.

func SplitPath

func SplitPath(filePath, prefix, separator string) []string

func TrimExt

func TrimExt(filePath, separator string) string

TrimExt returns a filePath with a path where the extension is removed.

Example
fmt.Println(TrimExt("image.png", "/"))
fmt.Println(TrimExt("image.png", ""))
fmt.Println(TrimExt("image.66.png", "/"))
fmt.Println(TrimExt("file", "/"))
fmt.Println(TrimExt("dir.with.ext/file", "/"))
fmt.Println(TrimExt("dir.with.ext/file.ext", "/"))
fmt.Println(TrimExt("dir.with.ext/file", "\\"))
fmt.Println(TrimExt("dir.with.ext/file", ""))
Output:

image
image
image.66
file
dir.with.ext/file
dir.with.ext/file
dir.with
dir.with

Types

type FileBuffer

type FileBuffer struct {
	ReadonlyFileBuffer
}

FileBuffer is a memory buffer that implements ReadWriteSeekCloser which combines the interfaces io.Reader io.ReaderAt io.Writer io.WriterAt io.Seeker io.Closer

func NewFileBuffer

func NewFileBuffer(data []byte) *FileBuffer

NewFileBuffer returns a new FileBuffer

func NewFileBufferWithClose

func NewFileBufferWithClose(data []byte, close func() error) *FileBuffer

NewFileBufferWithClose returns a new FileBuffer

func (*FileBuffer) Write

func (buf *FileBuffer) Write(p []byte) (n int, err error)

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early.

func (*FileBuffer) WriteAt

func (buf *FileBuffer) WriteAt(p []byte, off int64) (n int, err error)

WriteAt writes len(p) bytes from p to the underlying data stream at offset off. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. WriteAt must return a non-nil error if it returns n < len(p).

type ReadonlyFileBuffer

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

ReadonlyFileBuffer is a memory buffer that implements ReadSeekCloser which combines the interfaces

io/fs.File
io.Reader
io.ReaderAt
io.Seeker
io.Closer

func NewReadonlyFileBuffer

func NewReadonlyFileBuffer(data []byte, info iofs.FileInfo) *ReadonlyFileBuffer

NewReadonlyFileBuffer returns a new ReadonlyFileBuffer

func NewReadonlyFileBufferReadAll

func NewReadonlyFileBufferReadAll(reader io.Reader, info iofs.FileInfo) (*ReadonlyFileBuffer, error)

func NewReadonlyFileBufferWithClose

func NewReadonlyFileBufferWithClose(data []byte, info iofs.FileInfo, close func() error) *ReadonlyFileBuffer

NewReadonlyFileBufferWithClose returns a new ReadonlyFileBuffer

func (*ReadonlyFileBuffer) Bytes

func (buf *ReadonlyFileBuffer) Bytes() []byte

Bytes returns the bytes of the buffer.

func (*ReadonlyFileBuffer) Close

func (buf *ReadonlyFileBuffer) Close() (err error)

Close will free the internal buffer

func (*ReadonlyFileBuffer) Read

func (buf *ReadonlyFileBuffer) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.

func (*ReadonlyFileBuffer) ReadAt

func (buf *ReadonlyFileBuffer) ReadAt(p []byte, off int64) (n int, err error)

ReadAt reads len(p) bytes into p starting at offset off in the underlying input source. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered.

func (*ReadonlyFileBuffer) Seek

func (buf *ReadonlyFileBuffer) Seek(offset int64, whence int) (newPos int64, err error)

Seek sets the offset for the next Read or Write to offset, interpreted according to whence: SeekStart means relative to the start of the file, SeekCurrent means relative to the current offset, and SeekEnd means relative to the end. Seek returns the new offset relative to the start of the file and an error, if any.

Seeking to an offset before the start of the file is an error. Seeking to any positive offset is legal, but the behavior of subsequent I/O operations on the underlying object is implementation-dependent.

func (*ReadonlyFileBuffer) Size

func (buf *ReadonlyFileBuffer) Size() int64

Size returns the size of buffered file in bytes.

func (*ReadonlyFileBuffer) Stat

func (buf *ReadonlyFileBuffer) Stat() (iofs.FileInfo, error)

Jump to

Keyboard shortcuts

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