Documentation
¶
Index ¶
- func AllowedExtensions(u *UploadedFile, exts ...string) bool
- func MaxFileSize(u *UploadedFile, maxBytes int64) bool
- func PutFromRequest(r *http.Request, field string, driver Driver, dir string) (string, error)
- func PutFromRequestAs(r *http.Request, field string, driver Driver, dir, name string) (string, error)
- func ServeSignedFiles(driver Driver, gen *SignedURLGenerator, prefix string) http.HandlerFunc
- type Driver
- type LocalDriver
- type S3Config
- type S3Driver
- func (d *S3Driver) Copy(src, dst string) error
- func (d *S3Driver) Delete(path string) error
- func (d *S3Driver) DeleteMany(paths []string) error
- func (d *S3Driver) Exists(path string) (bool, error)
- func (d *S3Driver) Get(path string) (io.ReadCloser, error)
- func (d *S3Driver) LastModified(path string) (time.Time, error)
- func (d *S3Driver) List(prefix string) ([]string, error)
- func (d *S3Driver) Move(src, dst string) error
- func (d *S3Driver) Put(path string, src io.Reader) error
- func (d *S3Driver) PutWithOptions(path string, src io.Reader, contentType string, acl types.ObjectCannedACL) error
- func (d *S3Driver) Size(path string) (int64, error)
- func (d *S3Driver) TemporaryURL(path string, duration time.Duration) (string, error)
- func (d *S3Driver) TemporaryUploadURL(path string, duration time.Duration) (string, error)
- func (d *S3Driver) URL(path string) string
- type SignedURLGenerator
- type UploadedFile
- func (u *UploadedFile) Extension() string
- func (u *UploadedFile) IsValid() bool
- func (u *UploadedFile) MimeType() (string, error)
- func (u *UploadedFile) Name() string
- func (u *UploadedFile) Open() (multipart.File, error)
- func (u *UploadedFile) Size() int64
- func (u *UploadedFile) Store(driver Driver, dir string) (string, error)
- func (u *UploadedFile) StoreAs(driver Driver, dir, name string) (string, error)
- func (u *UploadedFile) StoreRandomName(driver Driver, dir string) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllowedExtensions ¶
func AllowedExtensions(u *UploadedFile, exts ...string) bool
AllowedExtensions checks if the uploaded file has an allowed extension.
func MaxFileSize ¶
func MaxFileSize(u *UploadedFile, maxBytes int64) bool
MaxFileSize checks if the uploaded file is within the size limit (in bytes).
func PutFromRequest ¶
PutFromRequest extracts a file from a multipart request by field name and stores it using the given driver.
func PutFromRequestAs ¶
func PutFromRequestAs(r *http.Request, field string, driver Driver, dir, name string) (string, error)
PutFromRequestAs extracts a file and stores it with a specific name.
func ServeSignedFiles ¶
func ServeSignedFiles(driver Driver, gen *SignedURLGenerator, prefix string) http.HandlerFunc
ServeSignedFiles returns an http.HandlerFunc that verifies signed URLs and serves files from the given driver.
mux.HandleFunc("/files/", storage.ServeSignedFiles(driver, gen))
Types ¶
type Driver ¶
type Driver interface {
Put(path string, src io.Reader) error
Get(path string) (io.ReadCloser, error)
Delete(path string) error
Exists(path string) (bool, error)
}
Driver is the file storage interface (plan: storage.Put, storage.Get).
type LocalDriver ¶
type LocalDriver struct {
Root string
}
LocalDriver stores files on the local filesystem (driver: local).
func NewLocalDriver ¶
func NewLocalDriver(root string) *LocalDriver
NewLocalDriver returns a driver that stores under root (e.g. "storage/app").
func (*LocalDriver) Delete ¶
func (d *LocalDriver) Delete(path string) error
Delete removes the file at root/path.
func (*LocalDriver) Exists ¶
func (d *LocalDriver) Exists(path string) (bool, error)
Exists returns whether the file exists.
func (*LocalDriver) Get ¶
func (d *LocalDriver) Get(path string) (io.ReadCloser, error)
Get opens the file at root/path for reading.
type S3Config ¶
type S3Config struct {
// Client is a pre-configured *s3.Client.
Client *s3.Client
// Bucket is the S3 bucket name.
Bucket string
// RequestTimeout limits S3 SDK calls made via storage Driver methods.
// Defaults to 15s.
RequestTimeout time.Duration
}
S3Config holds configuration for the S3 driver.
type S3Driver ¶
type S3Driver struct {
// contains filtered or unexported fields
}
S3Driver implements the Driver interface using Amazon S3 (or any S3-compatible store).
func NewS3Driver ¶
NewS3Driver creates a new S3-backed storage driver.
func (*S3Driver) DeleteMany ¶
DeleteMany removes multiple objects in a single batch request.
func (*S3Driver) Get ¶
func (d *S3Driver) Get(path string) (io.ReadCloser, error)
Get downloads the object at path and returns a ReadCloser.
func (*S3Driver) LastModified ¶
LastModified returns the last modified time of the object.
func (*S3Driver) PutWithOptions ¶
func (d *S3Driver) PutWithOptions(path string, src io.Reader, contentType string, acl types.ObjectCannedACL) error
PutWithOptions uploads with explicit content type and ACL.
func (*S3Driver) TemporaryURL ¶
TemporaryURL returns a pre-signed URL valid for the given duration.
func (*S3Driver) TemporaryUploadURL ¶
TemporaryUploadURL returns a pre-signed PUT URL for direct uploads.
type SignedURLGenerator ¶
type SignedURLGenerator struct {
Secret string // HMAC secret key
BaseURL string // e.g. "https://example.com/files"
}
SignedURLGenerator can produce time-limited signed URLs for stored files.
func NewSignedURLGenerator ¶
func NewSignedURLGenerator(secret, baseURL string) *SignedURLGenerator
NewSignedURLGenerator creates a generator with the given secret and base URL.
func (*SignedURLGenerator) TemporaryURL ¶
func (g *SignedURLGenerator) TemporaryURL(path string, expiry time.Duration) string
TemporaryURL generates a signed URL that expires after the given duration.
url := gen.TemporaryURL("avatars/photo.jpg", 15*time.Minute)
// => https://example.com/files/avatars/photo.jpg?expires=1700000000&signature=abc123
type UploadedFile ¶
type UploadedFile struct {
Header *multipart.FileHeader
// contains filtered or unexported fields
}
UploadedFile represents a file received via multipart form upload.
func NewUploadedFile ¶
func NewUploadedFile(fh *multipart.FileHeader) *UploadedFile
NewUploadedFile wraps a multipart file header.
func (*UploadedFile) Extension ¶
func (u *UploadedFile) Extension() string
Extension returns the file extension (e.g. ".jpg").
func (*UploadedFile) IsValid ¶
func (u *UploadedFile) IsValid() bool
IsValid checks if the file is not empty and has a valid header.
func (*UploadedFile) MimeType ¶
func (u *UploadedFile) MimeType() (string, error)
MimeType detects the MIME type by reading the first 512 bytes.
func (*UploadedFile) Name ¶
func (u *UploadedFile) Name() string
Name returns the original filename.
func (*UploadedFile) Open ¶
func (u *UploadedFile) Open() (multipart.File, error)
Open opens the file for reading.
func (*UploadedFile) Size ¶
func (u *UploadedFile) Size() int64
Size returns the file size in bytes.
func (*UploadedFile) Store ¶
func (u *UploadedFile) Store(driver Driver, dir string) (string, error)
Store saves the file to the given driver at the specified directory. Returns the full path where the file was stored.
func (*UploadedFile) StoreAs ¶
func (u *UploadedFile) StoreAs(driver Driver, dir, name string) (string, error)
StoreAs saves the file with a custom name.
func (*UploadedFile) StoreRandomName ¶
func (u *UploadedFile) StoreRandomName(driver Driver, dir string) (string, error)
StoreRandomName saves the file with a randomly generated name, preserving the extension.