utils

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnSupportedMediaType     = fiber.NewError(http.StatusUnsupportedMediaType, "unsupported media type")
	ErrEmptyFile                = fiber.NewError(http.StatusUnsupportedMediaType, "empty file")
	ErrUnSupportedFileExtension = fiber.NewError(http.StatusBadRequest, "unsupported file extension")
)
View Source
var (

	// Image MIME Types
	ImageGIF  = "image/gif"
	ImageJPEG = "image/jpeg"
	ImagePNG  = "image/png"
	ImageBMP  = "image/bmp"
	ImageTIFF = "image/tiff"
	ImageWebP = "image/webp"
	ImageAVIF = "image/avif"

	// Document MIME Types
	DocumentPDF  = "application/pdf"
	DocumentDOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
	DocumentDOC  = "application/msword"
	DocumentXLS  = "application/vnd.ms-excel"
	DocumentXLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
	DocumentPPT  = "application/vnd.ms-powerpoint"
	DocumentPPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation"

	// Compressed MIME Types
	CompressedGZIP  = "application/gzip"
	CompressedBZIP2 = "application/x-bzip2"
	CompressedRar   = "application/x-rar"
	CompressedZip   = "application/zip"
	Compressed7Z    = "application/x-7z-compressed"

	// Audo formats
	AudioAAC  = "audio/aac"
	AudioFLAC = "audio/flac"
	AudioMP3  = "audio/mpeg"
	AudioOGG  = "audio/ogg"
	AudioWAV  = "audio/x-wav"
	AudioWMA  = "audio/x-ms-wma"

	// Video formats
	VideoMP4 = "video/mp4"
	VideoAVI = "video/x-msvideo"
	VideoMKV = "video/x-matroska"
	VideoMOV = "video/quicktime"
	VideoMPG = "video/mpeg"
	VideoWMV = "video/x-ms-wmv"

	// Plain Text MIME Types
	PlainText = "text/plain"

	// Executable Formats
	Executable = "application/octet-stream"

	// Font Formats
	FontOpenType = "application/vnd.ms-opentype"
)

Functions

func ParseMultipleUploads

func ParseMultipleUploads(c *fiber.Ctx, config Config, staticPrefix string) (map[string][]Upload, error)

Extracts uploads from the multipart form. Only the specified file types are allowed.

maxSize: Maximum allowed size for an upload in bytes.

validTypes: List of allowed file types.

invalidExtensions: Explicit file extensions that are not allowed. This caters for files with complex mimetypes. e.g .exe, .bat, .msi etc

staticPrefix: Prefix where the files are to be stored. This is the string you used for app.Static e.g form app.Static("/uploads",....) staticPrefix is `uploads`.

Returns: A map of slices of uploads with each upload containing the title, database path and file system path. The key for the map are the form field names. This means you can upload multiple files with different and similar field names and they will be processed properly.

Usage:

// fiber router to handle uploads
app.Post("/uploads", func(c *fiber.Ctx) error {
	uploads, err := utils.ParseMultipleUploads(c, utils.DefaultConfig, "uploads")
	if err != nil {
		return err
	}

	fmt.Println(uploads)
	return c.JSON(uploads)
})

func SaveMultipartFiles

func SaveMultipartFiles(c *fiber.Ctx, uploads []Upload) (err error)

Saves multipart files to disk using c.SaveFile.

Types

type Config

type Config struct {
	MaxSize               int64    // Maximum file size in the uploads
	ValidTypes            []string // slice of mime types that are allowed.
	InvalidExtensions     []string // slice of file extensions not allowed
	DisableSizeCheck      bool     // Whether to perfom checks on file size
	DisableMimeCheck      bool     // Whether to perform checks on mimetypes
	DisableExtensionCheck bool     // Whether to perform check of file extensions
}

Config allows you to pass options for validating uploads.

type Upload

type Upload struct {
	Title  string `json:"title"`   // defaults to the original filename
	DbPath string `json:"db_path"` // database path stripped of file system infomation
	FsPath string `json:"fs_path"` // filesystem path where to save the upload
	Size   int64  `json:"size"`    // fileSize in bytes

	FileHeader *multipart.FileHeader `json:"file_header"` // pointer to fileHeader.
}

Stores information about an uploaded file.

Jump to

Keyboard shortcuts

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