uploader

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 8 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(file multipart.File, filename string) (string, error)

Upload writes file into the uploader's directory and returns the saved path.

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.

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.

The Uploader interface carries no context, so the put uses context.Background(); a per-call context would require an interface change.

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