Documentation ¶
Overview ¶
Package objects provides interfaces and helpers for management of objects (files) either received via our endpoints or created and stored by the system
Index ¶
- Constants
- Variables
- func ComputeChecksum(data io.ReadSeeker) (string, error)
- func CreateURI(scheme, destination, key string) string
- func DetectContentType(data io.ReadSeeker) (string, error)
- func FileUploadMiddleware(config *Objects) func(http.Handler) http.Handler
- func FormatFileSize(size int64) string
- func GetFileIDsFromContext(ctx context.Context) []string
- func ReaderToSeeker(r io.Reader) (io.ReadSeeker, error)
- func StreamToByte(stream io.ReadSeeker) ([]byte, error)
- func WriteFilesToContext(ctx context.Context, f Files) context.Context
- type AppValidator
- type Config
- type ContextKey
- type DownloadFileMetadata
- type DownloadFileOptions
- type ErrResponseHandler
- type File
- type FileUpload
- type Files
- type NameGeneratorFunc
- type Objects
- type Option
- func WithErrorResponseHandler(errHandler ErrResponseHandler) Option
- func WithIgnoreNonExistentKey(ignore bool) Option
- func WithKeys(keys []string) Option
- func WithMaxFileSize(i int64) Option
- func WithMaxMemory(i int64) Option
- func WithNameFuncGenerator(nameFunc NameGeneratorFunc) Option
- func WithSkipper(skipper SkipperFunc) Option
- func WithStorage(store Storage) Option
- func WithUploaderFunc(uploader UploaderFunc) Option
- func WithValidationFunc(validationFunc ValidationFunc) Option
- type Progress
- type SkipperFunc
- type Storage
- type UploadFileOptions
- type UploadOption
- type UploadedFileMetadata
- type UploaderFunc
- type ValidationFunc
Constants ¶
const (
ErrNoFilesUploaded = errorMsg("objects: no uploadable files found in request")
)
Variables ¶
var ( // ErrFilesNotFound is returned when files could not be found in key from http request ErrFilesNotFound = errors.New("files could not be found in key from http request") // ErrFileOpenFailed is returned when a file could not be opened ErrFileOpenFailed = errors.New("could not open file") // ErrInvalidMimeType is returned when a file has an invalid mime type ErrInvalidMimeType = errors.New("invalid mimetype") // ErrValidationFailed is returned when a validation fails ErrValidationFailed = errors.New("validation failed") // ErrUnsupportedMimeType is returned when a file has an unsupported mime type ErrUnsupportedMimeType = errors.New("unsupported mime type uploaded") // ErrMustProvideStorageBackend is returned when a storage backend is not provided ErrMustProvideStorageBackend = errors.New("you must provide a storage backend") // ErrUnexpectedType is returned when an invalid type is provided ErrUnexpectedType = errors.New("unexpected type provided") )
var FileContextKey = &ContextKey{"files"}
FileContextKey is the context key for the files This is the key that is used to store the files in the context, which is then used to retrieve the files in subsequent parts of the request this is different than the `key` in the multipart form, which is the form field name that the file was uploaded with
Functions ¶
func ComputeChecksum ¶
func ComputeChecksum(data io.ReadSeeker) (string, error)
ComputeChecksum calculates the MD5 checksum for the provided data. It expects that the passed io object will be seeked to its beginning and will seek back to the beginning after reading its content.
func DetectContentType ¶
func DetectContentType(data io.ReadSeeker) (string, error)
DetectContentType leverages http.DetectContentType to identify the content type of the provided data. It expects that the passed io object will be seeked to its beginning and will seek back to the beginning after reading its content.
func FileUploadMiddleware ¶
FileUploadMiddleware is a middleware that handles the file upload process this can be added to the middleware chain to handle file uploads prior to the main handler Since gqlgen handles file uploads differently, this middleware is not used in the graphql handler
func FormatFileSize ¶
FormatFileSize converts a file size in bytes to a human-readable string in MB/GB notation.
func GetFileIDsFromContext ¶
GetFileIDsFromContext returns the file IDs from the context that are associated with the request
func ReaderToSeeker ¶
func ReaderToSeeker(r io.Reader) (io.ReadSeeker, error)
ReaderToSeeker function takes an io.Reader as input and returns an io.ReadSeeker which can be used to upload files to the object storage
func StreamToByte ¶
func StreamToByte(stream io.ReadSeeker) ([]byte, error)
StreamToByte function reads the content of the provided io.Reader and returns it as a byte slice
func WriteFilesToContext ¶
WriteFilesToContext retrieves any existing files from the context, appends the new files to the existing files map based on the form field name, then returns a new context with the updated files map stored in it
Types ¶
type AppValidator ¶
type AppValidator struct {
// contains filtered or unexported fields
}
AppValidator is a wrapper around the validator package
func NewValidator ¶
func NewValidator() *AppValidator
NewValidator creates a new instance of the AppValidator
func (*AppValidator) Validate ¶
func (cv *AppValidator) Validate(i interface{}) error
Validate runs the validation against the provided struct
type Config ¶
type Config struct { // Enabled indicates if the store is enabled Enabled bool `json:"enabled" koanf:"enabled" default:"true"` // Provider is the name of the provider Provider string `json:"provider" koanf:"provider"` // AccessKey is the access key for the storage provider AccessKey string `json:"accessKey" koanf:"accessKey"` // Region is the region for the storage provider Region string `json:"region" koanf:"region"` // SecretKey is the secret key for the storage provider SecretKey string `json:"secretKey" koanf:"secretKey"` // CredentialsJSON is the credentials JSON for the storage provider CredentialsJSON string `json:"credentialsJSON" koanf:"credentialsJSON"` // DefaultBucket is the default bucket name for the storage provider DefaultBucket string `json:"defaultBucket" koanf:"defaultBucket"` // Keys is a list of keys to look for in the multipart form on the REST request // if the keys are not found, the request upload will be skipped // this is not used by the graphql handler Keys []string `json:"keys" koanf:"keys" default:"[uploadFile]"` }
type ContextKey ¶
type ContextKey struct {
// contains filtered or unexported fields
}
ContextKey is the key name for the additional context
type DownloadFileMetadata ¶
type DownloadFileMetadata struct { // FolderDestination is the folder that holds the file FolderDestination string `json:"folder_destination,omitempty"` // Key is the unique identifier for the file Key string `json:"key,omitempty"` // Size in bytes of the downloaded file Size int64 `json:"size,omitempty"` }
DownloadFileMetadata is a struct that holds information about a file that was successfully downloaded
type DownloadFileOptions ¶
type DownloadFileOptions struct {
FileName string
}
DownloadFileOptions is a struct that holds the options for downloading a file
type ErrResponseHandler ¶
type ErrResponseHandler func(err error, statusCode int) http.HandlerFunc
ErrResponseHandler is a custom error that should be used to handle errors when an upload fails
type File ¶
type File struct { // ID is the unique identifier for the file ID string `json:"id"` // Name of the file Name string `json:"name"` // Path of the file Path string `json:"path"` // Type of file that was uploaded Type string `json:"type"` // Thumbnail is a URL to the thumbnail of the file Thumbnail *string `json:"thumbnail"` // MD5 hash of the file MD5 []byte `json:"md5"` // CreatedAt is the time the file was created CreatedAt time.Time `json:"created_at"` // UpdatedAt is the time the file was last updated UpdatedAt time.Time `json:"updated_at"` // OwnerID is the ID of the organization or user who created the file OwnerID string `json:"owner_id"` // FieldName denotes the field from the multipart form FieldName string `json:"field_name,omitempty"` // OriginalName of the file from the client side OriginalName string `json:"original_name,omitempty"` // UploadedFileName denotes the name of the file when it was ultimately // uploaded to the storage layer. The distinction is important because of // potential changes to the file name that may be done UploadedFileName string `json:"uploaded_file_name,omitempty"` // FolderDestination is the folder that holds the uploaded file FolderDestination string `json:"folder_destination,omitempty"` // StorageKey can be used to retrieve the file from the storage backend StorageKey string `json:"storage_key,omitempty"` // MimeType of the uploaded file MimeType string `json:"mime_type,omitempty"` // ContentType is the detected content type of the file ContentType string `json:"content_type,omitempty"` // Size in bytes of the uploaded file Size int64 `json:"size,omitempty"` Metadata map[string]string Bucket string PresignedURL string `json:"url"` ProvidedExtension string `json:"provided_extension"` }
File is a struct that holds information about a file - there is no distinction between a File received in a multipart form request or used in a download
type FileUpload ¶
type FileUpload struct { // File is the file to be uploaded File io.ReadSeeker // Filename is the name of the file provided in the multipart form Filename string // Size is the size of the file in bytes Size int64 // ContentType is the content type of the file from the header ContentType string // Key is the field name from the graph input or multipart form Key string }
FileUpload is the object that holds the file information
func NewUploadFile ¶
func NewUploadFile(filePath string) (file FileUpload, err error)
NewUploadFile function reads the content of the provided file path and returns a FileUpload object
type NameGeneratorFunc ¶
NameGeneratorFunc allows you alter the name of the file before it is ultimately uploaded and stored
var OrganizationNameFunc NameGeneratorFunc = func(s string) string {
return s
}
type Objects ¶
type Objects struct { // Storage is the storage backend that will be used to store the uploaded files Storage Storage `json:"-" koanf:"-"` // MaxSize is the maximum size of file uploads to accept MaxSize int64 `json:"maxSize" koanf:"maxSize"` // MaxMemory is the maximum memory to use when parsing a multipart form MaxMemory int64 `json:"maxMemory" koanf:"maxMemory"` // IgnoreNonExistentKeys is a flag that indicates the handler should skip multipart form key values which do not match the configured IgnoreNonExistentKeys bool `json:"ignoreNonExistentKeys" koanf:"ignoreNonExistentKeys"` // Keys is a list of keys to look for in the multipart form on the REST request // if the keys are not found, the request upload will be skipped // this is not used by the graphql handler Keys []string `json:"keys" koanf:"keys" default:"[uploadFile]"` // ValidationFunc is a custom validation function ValidationFunc ValidationFunc `json:"-" koanf:"-"` // NameFuncGenerator is a function that allows you to rename your uploaded files NameFuncGenerator NameGeneratorFunc `json:"-" koanf:"-"` // Uploader is the func that handlers the file upload process and returns the files uploaded Uploader UploaderFunc `json:"-" koanf:"-"` // Skipper defines a function to skip middleware. Skipper SkipperFunc `json:"-" koanf:"-"` // ErrorResponseHandler is a custom error response handler ErrorResponseHandler ErrResponseHandler `json:"-" koanf:"-"` }
Objects is the definition for handling objects and file uploads
func (*Objects) FileUpload ¶
FileUpload uploads the files to the storage and returns the the context with the uploaded files
type Option ¶
type Option func(*Objects)
Option is a function that configures the Objects
func WithErrorResponseHandler ¶
func WithErrorResponseHandler(errHandler ErrResponseHandler) Option
WithErrorResponseHandler allows you to provide a custom error response handler
func WithIgnoreNonExistentKey ¶
WithIgnoreNonExistentKey allows you to configure the handler to skip multipart form key values which do not match the configured
func WithKeys ¶
WithKeys allows you to configure the keys to look for in the multipart form on the REST request
func WithMaxFileSize ¶
WithMaxFileSize allows you limit the size of file uploads to accept
func WithMaxMemory ¶
WithMaxMemory allows you limit the amount of memory to use when parsing a multipart form
func WithNameFuncGenerator ¶
func WithNameFuncGenerator(nameFunc NameGeneratorFunc) Option
WithNameFuncGenerator allows you configure how you'd like to rename your uploaded files
func WithSkipper ¶
func WithSkipper(skipper SkipperFunc) Option
WithSkipper allows you to provide a custom skipper function
func WithStorage ¶
WithStorage allows you to provide a storage backend to the Objects
func WithUploaderFunc ¶
func WithUploaderFunc(uploader UploaderFunc) Option
WithUploaderFunc allows you to provide a custom uploader function
func WithValidationFunc ¶
func WithValidationFunc(validationFunc ValidationFunc) Option
WithValidationFunc allows you to provide a custom validation function
type Progress ¶
type Progress struct { // TotalSize is the total size of the file being uploaded TotalSize int64 // BytesRead is the number of bytes that have been read so far BytesRead int64 }
Progress is used to track the progress of a file upload It implements the io.Writer interface so it can be passed to an io.TeeReader()
type SkipperFunc ¶
SKipperFunc is a function that defines whether to skip the middleware
type Storage ¶
type Storage interface { // Upload is used to upload a file to the storage backend Upload(context.Context, io.Reader, *UploadFileOptions) (*UploadedFileMetadata, error) // ManagerUpload is used to upload multiple files to the storage backend ManagerUpload(context.Context, [][]byte) error // Download is used to download a file from the storage backend Download(context.Context, string, *DownloadFileOptions) (*DownloadFileMetadata, io.ReadCloser, error) // GetPresignedURL is used to get a presigned URL for a file in the storage backend GetPresignedURL(context.Context, string) (string, error) // GetScheme returns the scheme of the storage backend GetScheme() *string io.Closer }
Storage is the primary interface that must be implemented by any storage backend and for interacting with Objects
type UploadFileOptions ¶
type UploadFileOptions struct { FileName string Metadata map[string]string Progress *pb.ProgressBar ProgressOutput io.Writer ProgressFinishMessage string Bucket string ContentType string }
UploadFileOptions is a struct that holds the options for uploading a file
type UploadOption ¶
type UploadOption func(*UploadFileOptions)
UploadOption is a function that configures the UploadFileOptions
func UploadMetadata ¶
func UploadMetadata(mp map[string]interface{}) UploadOption
UploadMetadata allows you to provide metadata for the upload
func UploadProgress ¶
func UploadProgress(p *pb.ProgressBar) UploadOption
UploadProgress allows you to provide a progress bar for the upload
func UploadProgressFinishMessage ¶
func UploadProgressFinishMessage(s string) UploadOption
UploadProgressFinishMessage allows you to provide a message to display when the upload is complete
func UploadProgressOutput ¶
func UploadProgressOutput(out io.Writer) UploadOption
UploadProgressOutput allows you to provide a writer for the progress bar
type UploadedFileMetadata ¶
type UploadedFileMetadata struct { // FolderDestination is the folder that holds the file FolderDestination string `json:"folder_destination,omitempty"` // Key is the unique identifier for the file Key string `json:"key,omitempty"` // Size in bytes of the uploaded file Size int64 `json:"size,omitempty"` // PresignedURL is the URL that can be used to download the file PresignedURL string `json:"presigned_url,omitempty"` }
UploadedFileMetadata is a struct that holds information about a file that was successfully uploaded
type UploaderFunc ¶
UploaderFunc is a function that handles the file upload process and returns the files uploaded
type ValidationFunc ¶
ValidationFunc is a type that can be used to dynamically validate a file
func ChainValidators ¶
func ChainValidators(validators ...ValidationFunc) ValidationFunc
ChainValidators returns a validator that accepts multiple validating criteria
func MimeTypeValidator ¶
func MimeTypeValidator(validMimeTypes ...string) ValidationFunc
MimeTypeValidator makes sure we only accept a valid mimetype. It takes in an array of supported mimes