Documentation
¶
Index ¶
- func CombineUri(uri string, parts ...string) string
- func GetUriPath(uri string) (path string, err error)
- func ParseUri(uri string) (schema, path, file, ext string, err error)
- type HttpFile
- func (f *HttpFile) Close() error
- func (f *HttpFile) Copy(wc io.WriteCloser) (int64, error)
- func (f *HttpFile) Delete() error
- func (f *HttpFile) Exists() bool
- func (f *HttpFile) Read(p []byte) (int, error)
- func (f *HttpFile) ReadAll() ([]byte, error)
- func (f *HttpFile) Rename(pattern string) (string, error)
- func (f *HttpFile) URI() string
- func (f *HttpFile) Write(p []byte) (int, error)
- func (f *HttpFile) WriteAll(b []byte) (int, error)
- type HttpFileStore
- func (f *HttpFileStore) Apply(filter string, action func(string)) error
- func (f *HttpFileStore) Close() error
- func (f *HttpFileStore) Delete(uri string) error
- func (f *HttpFileStore) Exists(uri string) bool
- func (f *HttpFileStore) List(filter string) ([]IFile, error)
- func (f *HttpFileStore) URI() string
- type IFile
- type IFileStore
- type LocalFile
- func (f *LocalFile) Close() error
- func (f *LocalFile) Copy(wc io.WriteCloser) (int64, error)
- func (f *LocalFile) Delete() error
- func (f *LocalFile) Exists() bool
- func (f *LocalFile) Read(p []byte) (int, error)
- func (f *LocalFile) ReadAll() ([]byte, error)
- func (f *LocalFile) Rename(pattern string) (string, error)
- func (f *LocalFile) URI() string
- func (f *LocalFile) Write(p []byte) (int, error)
- func (f *LocalFile) WriteAll(b []byte) (int, error)
- type LocalFileStore
- func (f *LocalFileStore) Apply(filter string, action func(string)) error
- func (f *LocalFileStore) Close() error
- func (f *LocalFileStore) Delete(uri string) error
- func (f *LocalFileStore) Exists(uri string) bool
- func (f *LocalFileStore) List(filter string) ([]IFile, error)
- func (f *LocalFileStore) URI() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CombineUri ¶
CombineUri constructs a URI by joining a base URI with one or more path segments. It ensures that there are no trailing slashes in the base URI or parts before joining.
Parameters:
uri: The base URI. parts: A variadic slice of strings representing the path segments to append.
Returns:
A single string representing the combined URI.
func GetUriPath ¶
GetUriPath extracts the path component from a given URI.
Parameters:
uri: The URI string to parse.
Returns:
The path component of the URI as a string. An error if the URI cannot be parsed.
func ParseUri ¶
ParseUri decomposes a URI into its constituent parts: schema, path, file name, and extension.
Parameters:
uri: The URI string to parse.
Returns:
The schema (e.g., "http", "file"). The path of the resource. The file name without the extension. The file extension. An error if the URI cannot be parsed.
Types ¶
type HttpFile ¶
type HttpFile struct {
// contains filtered or unexported fields
}
HttpFile provides a concrete implementation of the IFile interface for files accessible via HTTP. It allows for reading from and writing to URLs, as well as checking for existence and deleting files.
func (*HttpFile) Close ¶
Close releases any resources associated with the file. For HttpFile, this is a no-op as HTTP connections are typically managed by the net/http package on a per-request basis.
Returns:
Always returns nil.
func (*HttpFile) Copy ¶
func (f *HttpFile) Copy(wc io.WriteCloser) (int64, error)
Copy copies the content of the file from the HTTP URL to an io.WriteCloser.
Parameters:
wc: The destination writer.
Returns:
The number of bytes copied. An error if the HTTP GET request fails or the copy operation fails.
func (*HttpFile) Delete ¶
Delete removes the file at the HTTP URL by sending a DELETE request.
Returns:
An error if the HTTP DELETE request fails.
func (*HttpFile) Exists ¶
Exists checks if the file exists at the HTTP URL by sending a HEAD request.
Returns:
True if the server responds with a 200 OK status, false otherwise.
func (*HttpFile) Read ¶
Read is part of the io.Reader interface. For HttpFile, this method is not supported as it's designed for streaming reads, which are better handled by ReadAll or Copy.
Returns:
An error indicating that the operation is not supported.
func (*HttpFile) ReadAll ¶
ReadAll reads the entire content of the file from the HTTP URL into a byte slice.
Returns:
A byte slice containing the file's content. An error if the HTTP GET request fails or the status code is not 200 OK.
func (*HttpFile) Rename ¶
Rename is not supported for HttpFile, as renaming files via HTTP is not a standard operation.
Returns:
An error indicating that the operation is not supported.
func (*HttpFile) URI ¶
URI returns the resource's URI (in this case, the URL).
Returns:
The URI as a string.
type HttpFileStore ¶
type HttpFileStore struct {
// contains filtered or unexported fields
}
HttpFileStore provides a concrete implementation of the IFileStore interface for files accessible via HTTP. While it implements the interface, some operations like listing and applying actions are not supported due to the nature of HTTP, which doesn't typically provide a standard way to list directory-like contents.
func (*HttpFileStore) Apply ¶
func (f *HttpFileStore) Apply(filter string, action func(string)) error
Apply is not supported for HttpFileStore. Since listing files is not supported, applying an action to a set of files is also not feasible.
Returns:
An error indicating that the operation is not supported.
func (*HttpFileStore) Close ¶ added in v1.2.124
func (f *HttpFileStore) Close() error
Close releases any resources held by the file store. For HttpFileStore, this is a no-op.
Returns:
Always returns nil.
func (*HttpFileStore) Delete ¶
func (f *HttpFileStore) Delete(uri string) error
Delete removes a file at a given URI. It delegates the deletion to an HttpFile instance.
Parameters:
uri: The full URI of the file to delete.
Returns:
An error if the deletion fails.
func (*HttpFileStore) Exists ¶
func (f *HttpFileStore) Exists(uri string) bool
Exists checks if a file at a given URI exists. It delegates this check to an HttpFile instance.
Parameters:
uri: The full URI of the file to check.
Returns:
True if the file exists, false otherwise.
func (*HttpFileStore) List ¶
func (f *HttpFileStore) List(filter string) ([]IFile, error)
List is not supported for HttpFileStore. HTTP does not have a standard protocol for listing files in a directory-like manner.
Returns:
An error indicating that the operation is not supported.
func (*HttpFileStore) URI ¶
func (f *HttpFileStore) URI() string
URI returns the base URI of the file store.
Returns:
The base URI as a string.
type IFile ¶
type IFile interface {
// ReadWriteCloser embeds the io.ReadWriteCloser interface, providing the Read, Write, and Close methods.
io.ReadWriteCloser
// URI returns the Uniform Resource Identifier (URI) of the file, including the schema.
// The schema indicates the type of file storage, e.g., "file" for local files,
// "gcs" for Google Cloud Storage, or "http" for files accessible via HTTP.
//
// Returns:
// A string representing the URI of the file.
URI() string
// Exists checks if the file exists at the specified URI.
//
// Returns:
// A boolean value, true if the file exists, false otherwise.
Exists() (result bool)
// Rename changes the name of the file based on a given pattern.
// The pattern can be an absolute new name or a template that uses parts of the original file name,
// such as its path, name, or extension. For example, a pattern could be "{path}/{name}_new.{ext}".
//
// Parameters:
// pattern: The pattern to use for renaming the file.
//
// Returns:
// The new name of the file as a string.
// An error if the renaming process fails.
Rename(pattern string) (result string, err error)
// Delete removes the file from its storage.
//
// Returns:
// An error if the deletion fails.
Delete() (err error)
// ReadAll reads the entire content of the file into a byte slice.
// This is a convenience method for reading the whole file in a single operation.
//
// Returns:
// A byte slice containing the file's content.
// An error if reading fails.
ReadAll() (b []byte, err error)
// WriteAll writes a byte slice to the file, overwriting any existing content.
// This is a convenience method for writing the entire content in a single operation.
//
// Parameters:
// b: The byte slice to write to the file.
//
// Returns:
// The number of bytes written.
// An error if writing fails.
WriteAll(b []byte) (n int, err error)
// Copy copies the content of the file to an io.WriteCloser.
// This is useful for streaming the file's content to another destination.
//
// Parameters:
// wc: The io.WriteCloser to which the file content will be copied.
//
// Returns:
// The total number of bytes written.
// An error if the copy operation fails.
Copy(wc io.WriteCloser) (written int64, err error)
}
IFile defines the interface for a file, abstracting the underlying storage mechanism. This allows for concrete implementations for various file types, such as local files, HTTP-accessible files, or files in cloud storage like Google Cloud Storage (GCS) or AWS S3.
func NewHttpFile ¶
NewHttpFile is a factory method that creates a new HttpFile instance.
Parameters:
uri: The HTTP or HTTPS URL of the file.
Returns:
An IFile instance representing the file at the given URI.
func NewLocalFile ¶
NewLocalFile is a factory method that creates a new LocalFile instance.
Parameters:
uri: The URI of the local file, which should have the "file" schema (e.g., "file:///path/to/file").
Returns:
An IFile instance representing the local file.
type IFileStore ¶
type IFileStore interface {
// URI returns the base Uniform Resource Identifier (URI) of the file store,
// including the schema (e.g., "file", "gcs", "http").
//
// Returns:
// A string representing the URI of the file store.
URI() string
// List retrieves a list of files within the store that match a given filter.
// The filter is typically a regular expression used to match file names.
//
// Parameters:
// filter: A regular expression string to filter the files.
//
// Returns:
// A slice of IFile instances that match the filter.
// An error if the listing operation fails.
List(filter string) (result []IFile, err error)
// Apply executes a given action on each file in the file store that matches a filter.
// The action is a function that takes the file's URI as a string.
//
// Parameters:
// filter: A regular expression string to filter the files.
// action: A function to apply to each matching file's URI.
//
// Returns:
// An error if the apply operation fails.
Apply(filter string, action func(string)) error
// Exists checks if a file with the specified URI exists within the store.
//
// Parameters:
// uri: The URI of the file to check.
//
// Returns:
// A boolean value, true if the file exists, false otherwise.
Exists(uri string) (result bool)
// Delete removes a file with the specified URI from the store.
//
// Parameters:
// uri: The URI of the file to delete.
//
// Returns:
// An error if the deletion fails.
Delete(uri string) (err error)
// Close releases any resources associated with the file store, such as network connections.
//
// Returns:
// An error if closing fails.
Close() error
}
IFileStore defines the interface for a file store, which is a repository for files. This interface provides methods for listing, managing, and interacting with files in a way that is agnostic to the underlying storage system (e.g., local file system, HTTP server, Google Cloud Storage, AWS S3).
func NewHttpFileStore ¶
func NewHttpFileStore(uri string) IFileStore
NewHttpFileStore is a factory method that creates a new HttpFileStore instance.
Parameters:
uri: The base HTTP or HTTPS URL for the file store.
Returns:
An IFileStore instance.
func NewLocalFileStore ¶
func NewLocalFileStore(uri string) IFileStore
NewLocalFileStore is a factory method that creates a new LocalFileStore instance.
Parameters:
uri: The URI of the base directory for the file store, which should have the "file" schema (e.g., "file:///path/to/directory").
Returns:
An IFileStore instance representing the local file store.
type LocalFile ¶
type LocalFile struct {
// contains filtered or unexported fields
}
LocalFile provides a concrete implementation of the IFile interface for files on the local file system. It wraps the standard `os.File` and provides methods for interacting with files using URIs.
func (*LocalFile) Close ¶
Close releases the file resource by closing the underlying `os.File`. If the file is not open, it does nothing.
Returns:
An error if closing the file fails.
func (*LocalFile) Copy ¶
func (f *LocalFile) Copy(wc io.WriteCloser) (int64, error)
Copy copies the content of the file to an io.WriteCloser.
Parameters:
wc: The destination writer.
Returns:
The number of bytes copied. An error if the copy operation fails.
func (*LocalFile) Delete ¶
Delete removes the file from the local file system.
Returns:
An error if the deletion fails.
func (*LocalFile) Exists ¶
Exists checks if the file exists on the local file system.
Returns:
True if the file exists, false otherwise.
func (*LocalFile) Read ¶
Read reads up to len(p) bytes into p. It returns the number of bytes read and any error encountered. It ensures the file is open before reading.
Returns:
The number of bytes read. An error if the file cannot be opened or if the read operation fails.
func (*LocalFile) ReadAll ¶
ReadAll reads the entire content of the file into a byte slice.
Returns:
A byte slice containing the file's content. An error if reading fails.
func (*LocalFile) Rename ¶
Rename changes the name of the file using a specified pattern. The pattern can include placeholders like {{path}}, {{file}}, and {{ext}} to reuse parts of the original name.
Parameters:
pattern: The new name pattern.
Returns:
The new path of the file. An error if renaming fails.
func (*LocalFile) Write ¶
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. It ensures the file is open for writing before the operation.
Returns:
The number of bytes written. An error if the file cannot be opened or if the write operation fails.
type LocalFileStore ¶
type LocalFileStore struct {
// contains filtered or unexported fields
}
LocalFileStore provides a concrete implementation of the IFileStore interface for the local file system. It allows for listing and managing files within a specified directory.
func (*LocalFileStore) Apply ¶
func (f *LocalFileStore) Apply(filter string, action func(string)) error
Apply executes a given action on each file in the file store that matches a regex filter.
Parameters:
filter: A regular expression to match against file paths. action: A function to apply to each matching file's URI.
Returns:
An error if the apply operation fails.
func (*LocalFileStore) Close ¶ added in v1.2.124
func (f *LocalFileStore) Close() error
Close releases any resources held by the file store. For LocalFileStore, this is a no-op.
Returns:
Always returns nil.
func (*LocalFileStore) Delete ¶
func (f *LocalFileStore) Delete(uri string) error
Delete removes a file from the store. The URI can be absolute or relative to the store's base URI.
Parameters:
uri: The URI of the file to delete.
Returns:
An error if the deletion fails.
func (*LocalFileStore) Exists ¶
func (f *LocalFileStore) Exists(uri string) bool
Exists checks if a file exists within the store. The URI can be absolute or relative to the store's base URI.
Parameters:
uri: The URI of the file to check.
Returns:
True if the file exists, false otherwise.
func (*LocalFileStore) List ¶
func (f *LocalFileStore) List(filter string) ([]IFile, error)
List retrieves a list of files within the store that match a given regex filter.
Parameters:
filter: A regular expression to match against file paths.
Returns:
A slice of IFile instances that match the filter. An error if the listing operation fails.
func (*LocalFileStore) URI ¶
func (f *LocalFileStore) URI() string
URI returns the base URI of the file store.
Returns:
The base URI as a string.