fileupload

package
v0.0.39 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

templ: version: v0.3.977

Index

Constants

View Source
const RemovePath = "/upload/remove"

RemovePath is the standard handler path for file removal.

View Source
const UploadPath = "/upload/files"

UploadPath is the standard handler path for file uploads.

Variables

This section is empty.

Functions

func DetectMIME added in v0.0.31

func DetectMIME(fh *multipart.FileHeader) (string, error)

DetectMIME opens a multipart file header and reads the first 512 bytes to detect the actual MIME type using http.DetectContentType.

func FileUpload

func FileUpload(props Props) templ.Component

FileUpload renders a file upload component with a DaisyUI file input wrapped in a form. Uploaded files appear in a list below the input, rendered server-side via SSE.

func IsRelativePath added in v0.0.31

func IsRelativePath(s string) bool

IsRelativePath validates that a URL string is a relative path (starts with /) and does not contain a scheme or protocol-relative prefix.

func RemoveHandler

func RemoveHandler(store Store) http.HandlerFunc

RemoveHandler returns an http.HandlerFunc that removes a file from the store and responds with an SSE patch of the updated file list.

Mount at a dedicated POST path:

r.Post(fileupload.RemovePath, fileupload.RemoveHandler(store))

func RemoveQueryParams added in v0.0.30

func RemoveQueryParams(removeURL, componentID, fileID string) string

RemoveQueryParams builds a remove URL with properly escaped query parameters.

func Route added in v0.0.31

func Route(store Store, opts ...HandlerOption) func(chi.Router)

Route returns a RouteOption that registers upload and remove handlers.

func StoreKey added in v0.0.31

func StoreKey(sessionID, componentID string) string

StoreKey builds the composite key used to identify files in the store.

func UploadHandler

func UploadHandler(store Store, opts ...HandlerOption) http.HandlerFunc

UploadHandler returns an http.HandlerFunc that accepts multipart file uploads and responds with an SSE patch of the updated file list.

Mount at a dedicated POST path:

r.Post(fileupload.UploadPath, fileupload.UploadHandler(store))

Types

type FileMeta

type FileMeta struct {
	ID       string
	Name     string
	Size     int64
	MimeType string
}

FileMeta holds metadata about an uploaded file.

type HandlerOption

type HandlerOption func(*handlerConfig)

HandlerOption configures upload validation.

func WithAllowedTypes

func WithAllowedTypes(types ...string) HandlerOption

WithAllowedTypes restricts uploads to files whose Content-Type starts with one of the given prefixes (e.g. "image/", "application/pdf").

func WithMaxFileSize

func WithMaxFileSize(bytes int64) HandlerOption

WithMaxFileSize sets the maximum allowed size per file in bytes.

func WithMaxFiles

func WithMaxFiles(n int) HandlerOption

WithMaxFiles sets the maximum number of files allowed per component.

type MemoryStore added in v0.0.31

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

MemoryStore is a thread-safe in-memory implementation of Store, keyed by "sessionID:componentID".

func NewStore

func NewStore() *MemoryStore

NewStore creates a new empty in-memory file metadata store.

func (*MemoryStore) Add added in v0.0.31

func (s *MemoryStore) Add(key string, meta FileMeta)

Add appends a file to the store under the given key.

func (*MemoryStore) Clear added in v0.0.31

func (s *MemoryStore) Clear(key string)

Clear removes all files under the given key.

func (*MemoryStore) List added in v0.0.31

func (s *MemoryStore) List(key string) []FileMeta

List returns all files stored under the given key.

func (*MemoryStore) Remove added in v0.0.31

func (s *MemoryStore) Remove(key, fileID string)

Remove deletes a file by ID from the store.

type Props

type Props struct {
	// ID uniquely identifies this component instance. Required.
	ID string
	// Class adds additional CSS classes to the container.
	Class string
	// Attributes adds arbitrary HTML attributes.
	Attributes templ.Attributes
	// Multiple allows selecting multiple files.
	Multiple bool
	// Accept restricts file types (e.g. "image/*", ".pdf,.doc").
	Accept string
	// UploadURL is the POST endpoint for file uploads.
	UploadURL string
	// RemoveURL is the POST endpoint for file removal.
	RemoveURL string
}

Props configures a FileUpload component.

type Store

type Store interface {
	Add(key string, meta FileMeta)
	Remove(key, fileID string)
	List(key string) []FileMeta
	Clear(key string)
}

Store is the interface for uploaded file metadata storage.

Jump to

Keyboard shortcuts

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