Documentation
¶
Overview ¶
templ: version: v0.3.977
Index ¶
- Constants
- func DetectMIME(fh *multipart.FileHeader) (string, error)
- func FileUpload(props Props) templ.Component
- func IsRelativePath(s string) bool
- func RemoveHandler(store Store) http.HandlerFunc
- func RemoveQueryParams(removeURL, componentID, fileID string) string
- func Route(store Store, opts ...HandlerOption) func(chi.Router)
- func StoreKey(sessionID, componentID string) string
- func UploadHandler(store Store, opts ...HandlerOption) http.HandlerFunc
- type FileMeta
- type HandlerOption
- type MemoryStore
- type Props
- type Store
Constants ¶
const RemovePath = "/upload/remove"
RemovePath is the standard handler path for file removal.
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 ¶
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
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
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
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 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.