fileutil

package
v0.0.0-...-5a762db Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package fileutil contains routines and clients for moving bytes around between object stores and files, or files and files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFile

func CopyFile(dest, source string) error

CopyFile copies bytes from the source filename to the dest filename. If they are determined to be the same file, it does nothing and returns a nil error. If the destination directory does not exist, it is created via os.MkdirAll with permissions 0775.

func FileType

func FileType(path string) (string, error)

FileType returns the mime type string for a given file path, e.g., "image/jpeg" for a JPEG image. Returns "inode/directory" if the referenced path is a directory name.

func FileTypes

func FileTypes(paths []string) ([]string, error)

FileTypes returns the mime type for every file specified. See FileType.

func HashFile

func HashFile(fname string) (string, error)

HashFile returns the hex string of the SHA1 hash of the bytes in the named file.

func IsVideo

func IsVideo(path string) bool

IsVideo returns whether the given path looks like a video based on extension.

func MimeTypeOrGeneric

func MimeTypeOrGeneric(path string) string

MimeTypeOrGeneric produces a mime type from the path extension, else octet-stream.

func TranslateSubPath

func TranslateSubPath(src, target, path string) (string, error)

TranslateSubPath finds the components of path that are under the source path, and appends them to the target path.

Types

type FileUtil

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

FileUtil provides operations for accessing files and object stores and performing common operations between them.

func New

func New(opts ...FileUtilOption) (*FileUtil, error)

New produces a new FileUtil that can access an object store with the given credentials. This allows them to be omitted in object store URLs.

func (*FileUtil) PullToFile

func (f *FileUtil) PullToFile(ctx context.Context, dest, uri string) error

PullToFile reads the bytes given by the uri (can be a file name) and writes them to the file named by dest. The destination must be a complete file name, not a directory.

func (*FileUtil) PushFromFile

func (f *FileUtil) PushFromFile(ctx context.Context, uri, source string) error

PushFromFile reads the byte at the source file and attempts to write them to the given URI. If the URI represents a file, it is treated as a file name. If it is a file name and ends in /, it is treated as a directory and the name will come from the source file name.

type FileUtilOption

type FileUtilOption func(*fileUtilMod)

FileUtilOption is an option that modifies behavior of the FileUtil when passed to New.

func WithS3Credentials

func WithS3Credentials(user, pass string) FileUtilOption

WithS3Credentials creates a FileUtilOption that sets the S3 user and password for S3 API operations.

type Stager

type Stager func(destPath, srcURI string) error

Stager is a function that gets a file from a source URI and pushes it to a destination (local) file path.

type Translator

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

Translator translates path names that are under a common source directory. The source is removed and replaced with the target.

func NewTranslator

func NewTranslator(source, target string, opts ...TranslatorOption) *Translator

NewTranslator creates a path translator from the given source and target paths. The file name to be translated must be under the source tree for translation to work properly. This works best when both source and target paths are absolute or relative to the same parent.

func (*Translator) Translate

func (t *Translator) Translate(path string) (string, error)

Translate finds the source prefix and removes it, replacing it with the target value.

func (*Translator) Untranslate

func (t *Translator) Untranslate(path string) (string, error)

Untranslate removes the target prefix and replaces it with the source. It undoes the translate operation.

type TranslatorOption

type TranslatorOption func(*Translator)

TranslatorOption provides options to the file path translator.

func WithStager

func WithStager(s Stager) TranslatorOption

WithStage instructs the translator to not only translate paths, but to stage files during the translate processes. For example, it may be that an input translator should pull files from an HTTP service and store them on disk. Turning this on allows that to happen. Otherwise it merely does path translation.

This does not change the behavior of Untranslate.

func WithUnstager

func WithUnstager(u Unstager) TranslatorOption

WithUnstage instructs the translator to copy files while untranslating, e.g., to push a file to an originating service. See WithStage for details.

This does not change the behavior of Translate.

type Unstager

type Unstager func(destURI, srcPath string) error

Unstager is a function that gets a local file and pushes it ot a destination URI.

Jump to

Keyboard shortcuts

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