utility

package
v0.1.5-beta Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PublicBadRequest is public message for 400
	PublicBadRequest = "Something Went Wrong With your Request"
	// PublicServerError is public message for 500
	PublicServerError = "Something Went Wrong"
	// PublicNotFoundError is public message for 404
	PublicNotFoundError = "Nothing was Found"
)
View Source
const (
	// HourCache caches for an hour
	HourCache int = int(time.Hour)
	// MinuteCache caches for a minute
	MinuteCache int = int(time.Minute)
)
View Source
const ImageMaxAge = 2 * 7 * 24 * int(time.Hour)

ImageMaxAge is the cache length (a week)

View Source
const ImageSizeLimit = 5000 * 1000

ImageSizeLimit is the size limit of all uploaded images

Variables

This section is empty.

Functions

func AutoCompress

func AutoCompress(mimetype string, img []byte) ([]byte, error)

AutoCompress choices the method to compresses an image

func BtoI32

func BtoI32(val []byte) uint32

BtoI32 converts bytes to uint32

func CompressGIF

func CompressGIF(b []byte, numColors int) ([]byte, error)

CompressGIF compress a GIF image

func CompressJPEG

func CompressJPEG(b []byte, quality int) ([]byte, error)

CompressJPEG compresses a JPEG image

func CompressPNG

func CompressPNG(b []byte, compressionLevel png.CompressionLevel) ([]byte, error)

CompressPNG compresses a PNG image

func DoesFileExist

func DoesFileExist(path string) bool

DoesFileExist check if the file (provided by path) exists using os.Stat

func Error

func Error(w http.ResponseWriter, sentErr HTTPError)

Error writes the HTTPError to the writer

func GZipBytes

func GZipBytes(content []byte) ([]byte, error)

GZipBytes compress a byte array using GZip

func GenerateETag

func GenerateETag(content []byte) (string, error)

GenerateETag generates an ETag from content

func GetFileLastModified

func GetFileLastModified(fileInfo fs.FileInfo) string

GetFileLastModified gets when a file has been last modified

func GetRandFromSlice

func GetRandFromSlice[t any](slice []t) t

GetRandFromSlice gets an random element from a slice

func I32toB

func I32toB(val uint32) []byte

I32toB converts uint32 to bytes

func IsFileValid

func IsFileValid(name string) bool

IsFileValid Checks if a file is legal based on it's url

func IsImageSurported

func IsImageSurported(contentType string) bool

IsImageSurported checks if a content-type is surported

func MimeExpection

func MimeExpection(mtype *mimetype.MIME, path string) string

MimeExpection changes the mime based on path extension

func ResizeImage

func ResizeImage(img image.Image, maxWidth, maxHeight uint) image.Image

ResizeImage scales up the image if its dimensions are smaller than the desired size while maintaining the aspect ratio.

func SendScanErr

func SendScanErr(w http.ResponseWriter, err error, noRow *string)

SendScanErr sends a special http error message when no rows found.

By default the noRow is "Something Went Wrong With your Request"

Types

type FileMime

type FileMime struct {
	Content []byte
	Mime    string
}

FileMime contains a file (bytes) and a mimetype (Content-Type)

type HTTPError

type HTTPError struct {
	Public  string `json:"public"`  // The public error shown in the writer
	Message string `json:"message"` // The more detailed message
	Code    int    `json:"-"`       // The HTTP code
}

HTTPError is a special json and more detailed

func (HTTPError) IsOk

func (err HTTPError) IsOk() bool

IsOk checks if an http code stored by a HTTPError is ok

type ImageAccept

type ImageAccept struct {
	Ok   bool
	Code int
	Msg  string
}

ImageAccept is used by github.com/Blockitifluy/CoffeeCo/utility.CanImageBeAccepted

func CanImageBeAccepted

func CanImageBeAccepted(r *http.Request, mimetype string) ImageAccept

CanImageBeAccepted checks: the size and content-type; for an image

Jump to

Keyboard shortcuts

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