Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ImageMimeTypes = []string{
"image/gif",
"image/jpeg",
"image/png",
}
ImageMimeTypes is a predefined list of common image MIME types. These MIME types have decoder registrations in the Go standard library, allowing dimension validation in ExtractFromRequest.
Functions ¶
func ChooseStoredExtension ¶ added in v0.3.55
func ChooseStoredExtension(fileHeader *multipart.FileHeader, detectedMime string) string
chooseStoredExtension determines the appropriate file extension for the stored file based on the detected MIME type and the original file name. It returns the chosen extension, including the leading dot (e.g., ".jpg"). If no suitable extension can be determined, it returns an empty string.
Types ¶
type ExtractParams ¶
type ExtractParams struct {
AllowedMimeTypes []string // list of allowed MIME types for uploaded files, e.g. ["image/jpeg", "image/png"].
MaxSizePerFile int64 // maximum size in bytes for each individual file, e.g. 10 << 20 for 10MB.
ImgMaxHeightPx *int // optional: if an image is uploaded, its maximum height in pixels. If omitted, no maximum height check is performed.
ImgMinHeightPx *int // optional: if an image is uploaded, its minimum height in pixels. If omitted, no minimum height check is performed.
ImgMaxWidthPx *int // optional: if an image is uploaded, its maximum width in pixels. If omitted, no maximum width check is performed.
ImgMinWidthPx *int // optional: if an image is uploaded, its minimum width in pixels. If omitted, no minimum width check is performed.
MaxFiles *int // optional: maximum number of files allowed to be uploaded in a single request. If omitted, defaults to 1.
// contains filtered or unexported fields
}
ExtractParams defines the parameters for extracting uploaded files from a multipart form request.
type StreamFileResult ¶
type StreamFileResult struct {
MimeType string `json:"mime_type"` // MIME type of the file
OriginalName string `json:"original_name"` // original file name as uploaded by the user
SizeBytes int64 `json:"size_bytes"` // size of the file in bytes
StoredName string `json:"stored_name"` // name of the file as stored on the server
}
StreamFileResult represents the result of a single uploaded file streamed to disk.
type StreamResponse ¶
type StreamResponse struct {
FileResults []StreamFileResult `json:"file_results"`
}
StreamResponse represents the response returned after successfully streaming file(s) to disk.
func StreamToDisk ¶
func StreamToDisk(uploadFiles []UploadFile, destPath string, userId int64, logger *slog.Logger) (uploadResp StreamResponse, err error)
type UploadFile ¶
type UploadFile struct {
File multipart.File // file is opened and ready for reading. It must be closed by caller.
FileHeader *multipart.FileHeader // includes the original file name and size.
MimeType string
}
UploadFile represents an uploaded file with its associated metadata.
func ExtractFromRequest ¶
func ExtractFromRequest(r *http.Request, params ExtractParams) (uploadFiles []UploadFile, err error)
ExtractFromRequest extracts uploaded files from a multipart form request and validates them against the provided ExtractParams.
The returned files must be closed after processing, e.g. using defer:
defer func() {
for _, uploadFile := range uploadFiles {
uploadFile.File.Close()
}
}()