upload

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoFileUploaded     = NewFileUploadError("NO_FILE", "No file was uploaded", "file")
	ErrFileTooLarge       = NewFileUploadError("FILE_TOO_LARGE", "File size exceeds limit", "file")
	ErrInvalidFileType    = NewFileUploadError("INVALID_TYPE", "File type not allowed", "file")
	ErrTooManyFiles       = NewFileUploadError("TOO_MANY_FILES", "Too many files uploaded", "file")
	ErrInvalidFileContent = NewFileUploadError("INVALID_CONTENT", "File content is invalid", "file")
)

Common file upload errors

Functions

func GetFormValue

func GetFormValue(form *multipart.Form, fieldName string) string

GetFormValue extracts a form value

func NewFileUploadResponse

func NewFileUploadResponse(files FileList, message string) *struct{ Body FileUploadResponseBody }

NewFileUploadResponse creates a standard response from uploaded files

Types

type File

type File struct {
	// Filename is the original name of the uploaded file
	Filename string `json:"filename" doc:"Original filename"`

	// Size is the file size in bytes
	Size int64 `json:"size" doc:"File size in bytes"`

	// ContentType is the MIME type of the uploaded file
	ContentType string `json:"content_type" doc:"MIME type of the file"`

	// Content provides access to the file data
	Content io.ReadCloser `json:"-"`
}

File represents an uploaded file with metadata

func GetFileFromForm

func GetFileFromForm(form *multipart.Form, fieldName string) (*File, error)

GetFileFromForm extracts a single file from multipart form

func NewFile

func NewFile(fh *multipart.FileHeader) (*File, error)

NewFile creates a new File from a multipart file header

func (*File) Close

func (f *File) Close() error

Close closes the file content reader

func (*File) Extension

func (f *File) Extension() string

Extension returns the file extension

func (*File) IsDocument

func (f *File) IsDocument() bool

IsDocument checks if the file is a document

func (*File) IsImage

func (f *File) IsImage() bool

IsImage checks if the file is an image based on content type

func (*File) ReadAll

func (f *File) ReadAll() ([]byte, error)

ReadAll reads all data from the file

type FileInfo

type FileInfo struct {
	Filename    string `json:"filename" doc:"Original filename"`
	Size        int64  `json:"size" doc:"File size in bytes"`
	ContentType string `json:"content_type" doc:"MIME type"`
	URL         string `json:"url,omitempty" doc:"URL to access the file"`
	ID          string `json:"id,omitempty" doc:"Unique file identifier"`
}

FileInfo contains metadata about an uploaded file

type FileList

type FileList []*File

FileList represents a collection of uploaded files

func NewFileList

func NewFileList(fhs []*multipart.FileHeader) (FileList, error)

NewFileList creates a new FileList from a slice of multipart file headers

func (FileList) CloseAll

func (fl FileList) CloseAll() error

CloseAll closes all files in the list

func (FileList) Count

func (fl FileList) Count() int

Count returns the number of files in the list

type FileUploadError

type FileUploadError struct {
	Message string `json:"message"`
	Code    string `json:"code"`
	Field   string `json:"field,omitempty"`
}

FileUploadError represents an error that occurred during file upload

func NewFileUploadError

func NewFileUploadError(code, message, field string) *FileUploadError

NewFileUploadError creates a new file upload error

func (*FileUploadError) Error

func (e *FileUploadError) Error() string

func (*FileUploadError) GetHeaders

func (e *FileUploadError) GetHeaders() map[string][]string

GetHeaders implements huma.HeadersError interface

func (*FileUploadError) GetStatus

func (e *FileUploadError) GetStatus() int

Implement huma.StatusError interface for proper HTTP status codes

type FileUploadResponseBody

type FileUploadResponseBody struct {
	Message    string            `json:"message" doc:"Upload result message"`
	Files      []*FileInfo       `json:"files" doc:"Information about uploaded files"`
	TotalCount int               `json:"total_count" doc:"Total number of files uploaded"`
	TotalSize  int64             `json:"total_size" doc:"Total size of all uploaded files"`
	UploadedAt string            `json:"uploaded_at" doc:"Upload timestamp"`
	Metadata   map[string]string `json:"metadata,omitempty" doc:"Additional metadata"`
}

FileUploadResponse provides a standard response structure for file uploads

Jump to

Keyboard shortcuts

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