httputils

package
v0.0.0-...-c01e8ca Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canon

func Canon(k string) string

Canon returns the canonical MIME header key (e.g., "content-type" -> "Content-Type").

func ContentTypeByExt

func ContentTypeByExt(extOrName string) string

ContentTypeByExt returns a content-type for a filename extension like ".png" or "png". It uses mime.TypeByExtension and falls back to a few common types. Empty string if unknown.

func DetectContentType

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

DetectContentType returns the MIME type by sniffing up to the first 512 bytes.

func DetectContentTypeFromFile

func DetectContentTypeFromFile(fileName string, first512 []byte) string

DetectContentTypeFromFile tries to determine a content-type from a file name, optionally sniffing up to the first 512 bytes if provided. If no good guess is available, returns application/octet-stream.

func DetectContentTypeFromHeader

func DetectContentTypeFromHeader(fileHeader *multipart.FileHeader) (string, error)

func DetectMIMEType

func DetectMIMEType(fileHeader *multipart.FileHeader) (string, error)

func Disposition

func Disposition(fileName string, inline bool) string

Disposition builds a Content-Disposition header value. If inline==true => "inline; filename*=utf-8”<url-escaped>"; else "attachment; ...".

func Extension

func Extension(fh *multipart.FileHeader) string

Extension returns the file extension (lowercase, no dot).

func OpenAndDetect

func OpenAndDetect(fh *multipart.FileHeader) (io.ReadCloser, string, error)

OpenAndDetect opens the file and also returns its content type (first 512 bytes read). The returned io.ReadCloser starts *after* the bytes used for detection.

func ReadAll

func ReadAll(fh *multipart.FileHeader) ([]byte, error)

ReadAll reads the entire file content into memory. Use with caution for large uploads.

func ReadChunked

func ReadChunked(fh *multipart.FileHeader, bufSize int, fn func([]byte) error) error

ReadChunked calls fn for each chunk read from the file. Useful for processing large files without loading into memory.

func SanitizeFilename

func SanitizeFilename(name string) string

SanitizeFilename removes potentially dangerous path separators and control chars.

func SaveTo

func SaveTo(fh *multipart.FileHeader, dstPath string, perm os.FileMode) error

SaveTo saves the file to the given destination path. It creates parent directories if needed.

func SaveToDir

func SaveToDir(fh *multipart.FileHeader, dir string, perm os.FileMode) (string, error)

SaveToDir saves the file into dir, using its original filename (sanitized).

func ValidateContentType

func ValidateContentType(fh *multipart.FileHeader, allowed []string) (string, error)

ValidateContentType ensures the file's content type is one of the allowed list.

func ValidateMaxSize

func ValidateMaxSize(fh *multipart.FileHeader, maxBytes int64) error

ValidateMaxSize checks if the file size is within maxBytes.

Types

This section is empty.

Jump to

Keyboard shortcuts

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