zfile

package
v0.0.0-...-d1c648a Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: BSD-3-Clause Imports: 24 Imported by: 4

Documentation

Index

Constants

View Source
const (
	PNGExt  = ".png"
	JPEGExt = ".jpeg"
	JPGExt  = ".jpg"
)

Variables

View Source
var ImageExtensions = []string{PNGExt, JPEGExt, JPGExt}

Functions

func AppendToFile

func AppendToFile(fpath, str string) error

func CalcMD5

func CalcMD5(filepath string) (data []byte, err error)

func CanOpenInFS

func CanOpenInFS(f fs.FS, filename string) bool

func ChangedExtension

func ChangedExtension(spath, ext string) string

func CloneFile

func CloneFile(dest, source string) error

func CopyFile

func CopyFile(dest, source string) (err error)

func CountBytesOfFileInFS

func CountBytesOfFileInFS(f fs.FS, name string) (int64, error)

func CreateSanitizedShortNameWithHash

func CreateSanitizedShortNameWithHash(name string) string

func CreateTempFile

func CreateTempFile(name string) (file *os.File, fpath string, err error)

func CreateTempFilePath

func CreateTempFilePath(name string) string

func DeleteOldInSubFolders

func DeleteOldInSubFolders(dir string, sleep time.Duration, before time.Time, deleteRatio float32, progress func(p float32, count, total int)) error

func Exists

func Exists(fpath string) bool

func ExpandTildeInFilepath

func ExpandTildeInFilepath(path string) string

func ForAllFileLines

func ForAllFileLines(path string, skipEmpty bool, line func(str string) bool) error

func GetFilesFromPath

func GetFilesFromPath(path, wildcards string, opts WalkOptions) (files []string, err error)

GetFilesFromPath returns a list of names of files inside path.

func IsFolder

func IsFolder(fpath string) bool

func JoinPathParts

func JoinPathParts(parts ...any) string

func MakeDirAllIfNotExists

func MakeDirAllIfNotExists(dir string) error

func MakePathRelativeTo

func MakePathRelativeTo(path, rel string) string

func MimeToExtension

func MimeToExtension(smime string) string

func Modified

func Modified(filepath string) time.Time

func NotExists

func NotExists(fpath string) bool

func PeriodicFileBackup

func PeriodicFileBackup(filepath, suffixForOld string, maxMB int)

PeriodicFileBackup checks if *filepath* is larger than maxMB megabytes every *checkHours*. If so, the file is moved to a file in the same directory with a suffix before extension. "path/file_suffix.log".

func ReadBytesFromFileInFS

func ReadBytesFromFileInFS(f fs.FS, name string) ([]byte, error)

func ReadFromURLToFilepath

func ReadFromURLToFilepath(surl, filepath string, maxBytes int64) (path string, err error)

ReadFromURLToFilepath http.Get's the file at surl, and stores it in a file at filepath. If maxBytes != 0, it only downloads that many bytes. If filepath == "", it creates a temporary file using name from surl. The stored file is returned in path, or err if error.

func ReadLastLine

func ReadLastLine(fpath string, pos int64) (line string, startpos, newpos int64, err error)

ReadLastLine reads a file from end, until it encounters ascii 10/13, consuming them too. *startpos* is where it started reading at. *newpos* is where it ended. if pos is not zero, it starts at pos. zero means start from end.

func ReadStringFromFile

func ReadStringFromFile(sfile string) (string, error)

func ReadStringFromFileInFS

func ReadStringFromFileInFS(f fs.FS, name string) (string, error)

func ReaderAtFromFileInFS

func ReaderAtFromFileInFS(f fs.FS, name string) (reader io.ReaderAt, length int64, err error)

ReaderAtFromFileInFS reads entire file to memory to be able to create a io.ReaderAt

func ReaderFromFileInFS

func ReaderFromFileInFS(f fs.FS, name string) (reader io.Reader, err error)

func RemoveAllQuicklyWithRename

func RemoveAllQuicklyWithRename(dir string) error

func RemoveContents

func RemoveContents(dir string) error

func RemoveFolderWithContents

func RemoveFolderWithContents(dir string) error

func RemoveOldFilesFromFolder

func RemoveOldFilesFromFolder(folder, wildcard string, opt WalkOptions, olderThan time.Duration)

func RemovedExtension

func RemovedExtension(spath string) string

func ReplaceHomeDirPrefixWithTilde

func ReplaceHomeDirPrefixWithTilde(path string) string

func SanitizeStringForFilePath

func SanitizeStringForFilePath(s string) string

func SetModified

func SetModified(fpath string, t time.Time) error

func SetOwnerAndMainGroup

func SetOwnerAndMainGroup(fpath, owner string) error

func Size

func Size(fpath string) int64

func Split

func Split(spath string) (dir, name, stub, ext string)

func TruncateFile

func TruncateFile(fpath string, maxBytes int64, reduce float64, fromEnd bool) error

if a file is > maxBytes, TruncateFile removes bytes from start or end to make it maxBytes*reduce large. This method is not atomical, more bytes can be added to fpath while it is working, and these will be lost, so a mutex or something should be used for appending to fpath if possible.

func Walk

func Walk(folder, wildcards string, opts WalkOptions, got func(fpath string, info os.FileInfo) error) error

Walk walks though the contents of folder, calling got on each, matching names with tab-separated wildcards (if any). folders are entered without wildcard matching. info.IsDir can be checked to see if the content is a folder, and path.SkipDir / path.SkipAll can be returned to abort a sub-folder or all. Any other error returned from got stops all and returns that error.

func WorkingDirPathToAbsolute

func WorkingDirPathToAbsolute(wpath string) string

WorkingDirPathToAbsolute prefixes wpath with working dir. If wpath is absolute, it returns it as-is.

func WriteBytesToFile

func WriteBytesToFile(data []byte, sfile string) error

func WriteStringToFile

func WriteStringToFile(str, sfile string) error

func WriteToFileAtomically

func WriteToFileAtomically(fpath string, write func(file io.Writer) error) error

WriteToFileAtomically opens a temporary file in same directory as fpath, calls write with its file, closes it, and renames it to fpath

Types

type MultiFS

type MultiFS []multiRow

func (*MultiFS) Add

func (m *MultiFS) Add(f fs.FS, fsname string)

func (*MultiFS) InsertFirst

func (m *MultiFS) InsertFirst(f fs.FS, fsname string)

func (MultiFS) IsOpenable

func (m MultiFS) IsOpenable(filename string) (bool, string)

func (MultiFS) Open

func (m MultiFS) Open(filename string) (fs.File, error)

func (MultiFS) OpenReturningFSName

func (m MultiFS) OpenReturningFSName(filename string) (fs.File, string, error)

func (MultiFS) Stat

func (m MultiFS) Stat(filename string) (fs.FileInfo, string, error)

Stat does not work for embeded file systems, so is not a good way to detect if file present.

type WalkOptions

type WalkOptions int
const (
	WalkOptionsNone     WalkOptions = 0
	WalkOptionRecursive WalkOptions = 1 << iota
	WalkOptionGiveFolders
	WalkOptionGiveHidden
	WalkOptionGiveNameOnly
	WalkOptionRelativePath
)

Jump to

Keyboard shortcuts

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