uploader

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package uploader stores uploaded multipart files to a local directory or an S3-compatible bucket behind a common Uploader interface.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextUploader added in v1.6.0

type ContextUploader interface {
	Uploader
	UploadContext(ctx context.Context, file multipart.File, filename string) (string, error)
}

ContextUploader is an Uploader whose upload can be cancelled. Both LocalUploader and S3Uploader implement it.

type LocalUploader

type LocalUploader struct {
	Path string
	// contains filtered or unexported fields
}

LocalUploader saves files to a local directory.

func NewLocalUploader

func NewLocalUploader(path string, opts ...Option) *LocalUploader

NewLocalUploader creates a new LocalUploader.

func (*LocalUploader) Upload

func (u *LocalUploader) Upload(file multipart.File, filename string) (string, error)

Upload writes file into the uploader's directory and returns the saved path. An existing file of the same name is replaced unless WithUniqueNames is set.

func (*LocalUploader) UploadContext added in v1.6.0

func (u *LocalUploader) UploadContext(ctx context.Context, file multipart.File, filename string) (string, error)

UploadContext is Upload, abandoned up front if ctx is already done.

type Option

type Option func(*config)

Option configures an uploader.

func WithAllowedMime

func WithAllowedMime(mimes []string) Option

WithAllowedMime rejects uploads whose detected MIME type is not in the list.

func WithBaseURL added in v0.4.0

func WithBaseURL(url string) Option

WithBaseURL makes S3Uploader.Upload return baseURL+key instead of just the key. For S3Uploader only; LocalUploader ignores it.

func WithKeyPrefix added in v0.4.0

func WithKeyPrefix(prefix string) Option

WithKeyPrefix namespaces S3 object keys (e.g. "avatars/"). For S3Uploader only; LocalUploader ignores it.

func WithMaxSize

func WithMaxSize(bytes int64) Option

WithMaxSize rejects uploads larger than the given number of bytes.

func WithUniqueNames added in v1.6.0

func WithUniqueNames() Option

WithUniqueNames prefixes every stored name with a random token ("<16 hex chars>-<name>"). Without it an upload replaces any existing file or object of the same name, so enable it whenever filenames come from users.

type S3Uploader added in v0.4.0

type S3Uploader struct {
	// contains filtered or unexported fields
}

S3Uploader uploads files to an S3-compatible bucket (AWS S3, MinIO, R2, ...).

func NewS3Uploader added in v0.4.0

func NewS3Uploader(client *minio.Client, bucket string, opts ...Option) *S3Uploader

NewS3Uploader creates an uploader backed by an S3-compatible bucket. Build the client with minio.New(endpoint, &minio.Options{Creds: ..., Secure: true}).

Example

Wire an S3-compatible uploader. Upload(file, "photo.png") then returns "https://cdn.example.com/avatars/photo.png".

package main

import (
	"log"

	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"github.com/rahmadafandi/fibr/uploader"
)

func main() {
	client, err := minio.New("s3.amazonaws.com", &minio.Options{
		Creds:  credentials.NewStaticV4("KEY", "SECRET", ""),
		Secure: true,
	})
	if err != nil {
		log.Fatal(err)
	}

	up := uploader.NewS3Uploader(client, "my-bucket",
		uploader.WithKeyPrefix("avatars/"),
		uploader.WithMaxSize(5<<20),
		uploader.WithAllowedMime([]string{"image/png", "image/jpeg"}),
		uploader.WithBaseURL("https://cdn.example.com/"),
	)
	_ = up
}

func (*S3Uploader) Upload added in v0.4.0

func (u *S3Uploader) Upload(file multipart.File, filename string) (string, error)

Upload validates the file, then puts it at <keyPrefix><sanitized-name> in the bucket. It returns the object key, or <baseURL>+key when WithBaseURL is set. An existing object of the same key is replaced unless WithUniqueNames is set.

func (*S3Uploader) UploadContext added in v1.6.0

func (u *S3Uploader) UploadContext(ctx context.Context, file multipart.File, filename string) (string, error)

UploadContext is Upload with a context bounding the put request.

type Uploader

type Uploader interface {
	Upload(file multipart.File, filename string) (string, error)
}

Uploader is an interface for uploading files.

Jump to

Keyboard shortcuts

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