storage

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

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

func PutFromRequest(r *http.Request, field string, driver Driver, dir string) (string, error)

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.

func (*LocalDriver) Put

func (d *LocalDriver) Put(path string, src io.Reader) error

Put writes the reader to root/path, creating parent dirs.

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

func NewS3Driver(cfg S3Config) *S3Driver

NewS3Driver creates a new S3-backed storage driver.

func (*S3Driver) Copy

func (d *S3Driver) Copy(src, dst string) error

Copy copies an object from src to dst within the same bucket.

func (*S3Driver) Delete

func (d *S3Driver) Delete(path string) error

Delete removes the object at path.

func (*S3Driver) DeleteMany

func (d *S3Driver) DeleteMany(paths []string) error

DeleteMany removes multiple objects in a single batch request.

func (*S3Driver) Exists

func (d *S3Driver) Exists(path string) (bool, error)

Exists checks if the object exists in the bucket.

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

func (d *S3Driver) LastModified(path string) (time.Time, error)

LastModified returns the last modified time of the object.

func (*S3Driver) List

func (d *S3Driver) List(prefix string) ([]string, error)

List lists objects with the given prefix. Returns keys.

func (*S3Driver) Move

func (d *S3Driver) Move(src, dst string) error

Move moves an object from src to dst (copy + delete).

func (*S3Driver) Put

func (d *S3Driver) Put(path string, src io.Reader) error

Put uploads src to the given path in the bucket.

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) Size

func (d *S3Driver) Size(path string) (int64, error)

Size returns the size in bytes of the object.

func (*S3Driver) TemporaryURL

func (d *S3Driver) TemporaryURL(path string, duration time.Duration) (string, error)

TemporaryURL returns a pre-signed URL valid for the given duration.

func (*S3Driver) TemporaryUploadURL

func (d *S3Driver) TemporaryUploadURL(path string, duration time.Duration) (string, error)

TemporaryUploadURL returns a pre-signed PUT URL for direct uploads.

func (*S3Driver) URL

func (d *S3Driver) URL(path string) string

URL returns the public URL of an object (assumes bucket is publicly accessible or behind CloudFront).

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

func (*SignedURLGenerator) Verify

func (g *SignedURLGenerator) Verify(path, signature string, expires int64) bool

Verify checks if a signed URL is still valid.

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.

Jump to

Keyboard shortcuts

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