Documentation ¶
Index ¶
- Variables
- type BytesReader
- type File
- type FileReader
- type MultipartReader
- type PathReader
- type System
- func (s *System) Attributes(fileKey string) (*blob.Attributes, error)
- func (s *System) Close() error
- func (s *System) Copy(srcKey, dstKey string) error
- func (s *System) CreateThumb(originalKey string, thumbKey, thumbSize string) error
- func (s *System) Delete(fileKey string) error
- func (s *System) DeletePrefix(prefix string) []error
- func (s *System) Exists(fileKey string) (bool, error)
- func (s *System) GetFile(fileKey string) (*blob.Reader, error)
- func (s *System) IsEmptyDir(dir string) bool
- func (s *System) List(prefix string) ([]*blob.ListObject, error)
- func (s *System) Serve(res http.ResponseWriter, req *http.Request, fileKey string, name string) error
- func (s *System) SetContext(ctx context.Context)
- func (s *System) Upload(content []byte, fileKey string) error
- func (s *System) UploadFile(file *File, fileKey string) error
- func (s *System) UploadMultipart(fh *multipart.FileHeader, fileKey string) error
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("blob not found")
var ThumbSizeRegex = regexp.MustCompile(`^(\d+)x(\d+)(t|b|f)?$`)
Functions ¶
This section is empty.
Types ¶
type BytesReader ¶ added in v0.11.0
type BytesReader struct {
Bytes []byte
}
BytesReader defines a FileReader from bytes content.
func (*BytesReader) Open ¶ added in v0.11.0
func (r *BytesReader) Open() (io.ReadSeekCloser, error)
Open implements the filesystem.FileReader interface.
type File ¶ added in v0.10.0
type File struct { Reader FileReader `form:"-" json:"-" xml:"-"` Name string `form:"name" json:"name" xml:"name"` OriginalName string `form:"originalName" json:"originalName" xml:"originalName"` Size int64 `form:"size" json:"size" xml:"size"` }
File defines a single file io.ReadSeekCloser resource.
The file could be from a local path, multipart/form-data header, etc.
func NewFileFromBytes ¶ added in v0.11.0
NewFileFromBytes creates a new File instance from the provided byte slice.
func NewFileFromMultipart ¶ added in v0.10.0
func NewFileFromMultipart(mh *multipart.FileHeader) (*File, error)
NewFileFromMultipart creates a new File from the provided multipart header.
func NewFileFromPath ¶ added in v0.10.0
NewFileFromPath creates a new File instance from the provided local file path.
func NewFileFromURL ¶ added in v0.23.0
NewFileFromURL creates a new File from the provided url by downloading the resource and load it as BytesReader.
Example
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() file, err := filesystem.NewFileFromURL(ctx, "https://example.com/image.png")
type FileReader ¶ added in v0.10.0
type FileReader interface {
Open() (io.ReadSeekCloser, error)
}
FileReader defines an interface for a file resource reader.
type MultipartReader ¶ added in v0.10.0
type MultipartReader struct {
Header *multipart.FileHeader
}
MultipartReader defines a FileReader from multipart.FileHeader.
func (*MultipartReader) Open ¶ added in v0.10.0
func (r *MultipartReader) Open() (io.ReadSeekCloser, error)
Open implements the filesystem.FileReader interface.
type PathReader ¶ added in v0.10.0
type PathReader struct {
Path string
}
PathReader defines a FileReader from a local file path.
func (*PathReader) Open ¶ added in v0.10.0
func (r *PathReader) Open() (io.ReadSeekCloser, error)
Open implements the filesystem.FileReader interface.
type System ¶
type System struct {
// contains filtered or unexported fields
}
func NewLocal ¶
NewLocal initializes a new local filesystem instance.
NB! Make sure to call `Close()` after you are done working with it.
func NewS3 ¶
func NewS3( bucketName string, region string, endpoint string, accessKey string, secretKey string, s3ForcePathStyle bool, ) (*System, error)
NewS3 initializes an S3 filesystem instance.
NB! Make sure to call `Close()` after you are done working with it.
func (*System) Attributes ¶
func (s *System) Attributes(fileKey string) (*blob.Attributes, error)
Attributes returns the attributes for the file with fileKey path.
If the file doesn't exist it returns ErrNotFound.
func (*System) Copy ¶ added in v0.20.0
Copy copies the file stored at srcKey to dstKey.
If srcKey file doesn't exist, it returns ErrNotFound.
If dstKey file already exists, it is overwritten.
func (*System) CreateThumb ¶
CreateThumb creates a new thumb image for the file at originalKey location. The new thumb file is stored at thumbKey location.
thumbSize is in the format: - 0xH (eg. 0x100) - resize to H height preserving the aspect ratio - Wx0 (eg. 300x0) - resize to W width preserving the aspect ratio - WxH (eg. 300x100) - resize and crop to WxH viewbox (from center) - WxHt (eg. 300x100t) - resize and crop to WxH viewbox (from top) - WxHb (eg. 300x100b) - resize and crop to WxH viewbox (from bottom) - WxHf (eg. 300x100f) - fit inside a WxH viewbox (without cropping)
func (*System) Delete ¶
Delete deletes stored file at fileKey location.
If the file doesn't exist returns ErrNotFound.
func (*System) DeletePrefix ¶
DeletePrefix deletes everything starting with the specified prefix.
The prefix could be subpath (ex. "/a/b/") or filename prefix (ex. "/a/b/file_").
func (*System) Exists ¶
Exists checks if file with fileKey path exists or not.
If the file doesn't exist returns false and ErrNotFound.
func (*System) GetFile ¶ added in v0.12.0
GetFile returns a file content reader for the given fileKey.
NB! Make sure to call Close() on the file after you are done working with it.
If the file doesn't exist returns ErrNotFound.
func (*System) IsEmptyDir ¶ added in v0.22.24
Checks if the provided dir prefix doesn't have any files.
A trailing slash will be appended to a non-empty dir string argument to ensure that the checked prefix is a "directory".
Returns "false" in case the has at least one file, otherwise - "true".
func (*System) List ¶ added in v0.16.0
func (s *System) List(prefix string) ([]*blob.ListObject, error)
List returns a flat list with info for all files under the specified prefix.
func (*System) Serve ¶
func (s *System) Serve(res http.ResponseWriter, req *http.Request, fileKey string, name string) error
Serve serves the file at fileKey location to an HTTP response.
If the `download` query parameter is used the file will be always served for download no matter of its type (aka. with "Content-Disposition: attachment").
Internally this method uses http.ServeContent so Range requests, If-Match, If-Unmodified-Since, etc. headers are handled transparently.
func (*System) SetContext ¶ added in v0.16.0
SetContext assigns the specified context to the current filesystem.
func (*System) UploadFile ¶ added in v0.10.0
UploadFile uploads the provided File to the fileKey location.
func (*System) UploadMultipart ¶ added in v0.8.0
func (s *System) UploadMultipart(fh *multipart.FileHeader, fileKey string) error
UploadMultipart uploads the provided multipart file to the fileKey location.