upload

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package upload provides the multipart file upload handler for tmux-webui.

Overview

Handler wraps a destination directory and a maximum file size. The HTTP() method returns an http.HandlerFunc that:

  1. Enforces the size cap via http.MaxBytesReader (returns 413 if exceeded).
  2. Parses the "file" field from a multipart/form-data body.
  3. Sanitizes the filename: strips directory components, replaces any character that is not \w, -, or . with an underscore (mirrors the Python re.sub(r"[^\w\-.]", "_", name) one-liner).
  4. Prefixes the sanitized name with a Unix millisecond timestamp to avoid collisions (e.g. "1715000000000_report.pdf").
  5. Streams the upload to disk with io.Copy and returns {"path": "<full destination path>"} on success.

Wire-up (server.go)

uploadH := upload.New(cfg.UploadDir, 50<<20)
mux.HandleFunc("POST /api/upload", uploadH.HTTP())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler is the upload handler bound to a destination directory.

func New

func New(dir string, maxBytes int64) *Handler

New creates a Handler that writes files to dir. Pass maxBytes = 0 to use the 50 MB default.

func (*Handler) HTTP

func (h *Handler) HTTP() http.HandlerFunc

HTTP returns an http.HandlerFunc for POST /api/upload. The request must be multipart/form-data with a "file" field.

Jump to

Keyboard shortcuts

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