fileupload

package
v0.0.29 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

templ: version: v0.3.977

Index

Constants

View Source
const RemovePath = "/api/upload/remove"

RemovePath is the standard handler path for file removal.

View Source
const UploadPath = "/api/upload/files"

UploadPath is the standard handler path for file uploads.

Variables

This section is empty.

Functions

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 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 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 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 struct {
	// contains filtered or unexported fields
}

Store is a thread-safe in-memory store for uploaded file metadata, keyed by "sessionID:componentID".

func NewStore

func NewStore() *Store

NewStore creates a new empty file metadata store.

func (*Store) Add

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

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

func (*Store) Clear

func (s *Store) Clear(key string)

Clear removes all files under the given key.

func (*Store) List

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

List returns all files stored under the given key.

func (*Store) Remove

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

Remove deletes a file by ID from the store.

Jump to

Keyboard shortcuts

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