uploader

package
v2.0.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 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(ctx context.Context, file multipart.File, filename string) (string, error)

Upload writes file into the uploader's directory and returns the saved path. The file is stored under a unique name ("<random>-<name>") unless WithOriginalNames is set. It is 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

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

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 WithOriginalNames

func WithOriginalNames() Option

WithOriginalNames stores each file under its sanitized original name instead of the default "<16 hex chars>-<name>". An upload then replaces any existing file or object of the same name, so use it only when the names are yours (not user-supplied) and overwriting is intended.

type S3Uploader

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

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

func NewS3Uploader

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(ctx, file, "photo.png") then returns "https://cdn.example.com/avatars/<random>-photo.png".

package main

import (
	"log"

	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"github.com/rahmadafandi/fibr/v2/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

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

Upload validates the file, then puts it at <keyPrefix><name> in the bucket. It returns the object key, or <baseURL>+key when WithBaseURL is set. The name is made unique ("<random>-<name>") unless WithOriginalNames is set. ctx bounds the put request.

type Uploader

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

Uploader stores an uploaded file and returns where it was stored. ctx bounds the upload.

Jump to

Keyboard shortcuts

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