utils

package
v0.0.0-...-81e723e Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolValue

func BoolValue(b *bool) bool

BoolValue safely dereferences a *bool, returning false if the pointer is nil.

func Contains

func Contains(slice []string, item string) bool

Contains checks if a slice contains a specific string.

func CopyFile

func CopyFile(src, dst string) error

func DecodeImage

func DecodeImage(fileHeader *multipart.FileHeader) (image.Image, string, error)

DecodeImage decodes an image from a multipart file header.

func DurationValue

func DurationValue(d *time.Duration) time.Duration

DurationValue safely dereferences a *time.Duration, returning 0 if the pointer is nil.

func EncodeImage

func EncodeImage(img image.Image, format string) (*bytes.Buffer, error)

EncodeImage encodes an image into a buffer using the detected format.

func ExtractExtension

func ExtractExtension(mimeType string) string

ExtractExtensions converts a slice of MIME types into a comma-separated list of extensions.

func ExtractExtensions

func ExtractExtensions(mimeTypes []string) string

ExtractExtensions converts a slice of MIME types into a comma-separated list of extensions.

func FileAddImports

func FileAddImports(fileContent string, importsToAdd map[string]string) (string, error)

FileAddImport adds an import to the provided Go source code if it doesn't already exist. It parses the source code, adds the import, sorts the imports, and returns the modified code.

func FileExists

func FileExists(path string) bool

func FileStringExists

func FileStringExists(f *ast.File, commentText string) bool

func Float64Value

func Float64Value(f *float64) float64

Float64Value safely dereferences a *float64, returning 0.0 if the pointer is nil.

func GenerateExcelFile

func GenerateExcelFile(fileName string, headerColumns []string, data [][]interface{}) (*excelize.File, error)

func GenerateUniqueFileName

func GenerateUniqueFileName(fileName string, uniqueID string) string

GenerateUniqueFileName appends a unique identifier to the file name

func GetFile

func GetFile(filePath string) string

func GetGoKitRootPath

func GetGoKitRootPath() (string, error)

func GetModuleName

func GetModuleName(rootPath string) (string, error)

func GetProjectRootPath

func GetProjectRootPath() (string, error)

func HumanReadableSize

func HumanReadableSize(size int64) string

HumanReadableSize converts a size in bytes to a human-readable string.

func InsertFields

func InsertFields(fields []ent.Field, insertions map[string][]ent.Field) []ent.Field

func InsertFieldsAfter

func InsertFieldsAfter(fields []ent.Field, afterFieldName string, newFields ...ent.Field) []ent.Field

InsertFieldsAfter inserts one or more new fields after a specified field name. *

  • Usage: // Insert multiple fields after "password2" return gokit.utils.InsertFieldsAfter( schema.User{}.Fields(), "password2", field.String("password3").Optional().Comment("Third password for the user"), field.String("password4").Optional().Comment("Fourth password for the user"), field.String("password5").Optional().Comment("Fifth password for the user"), )

func Int64Value

func Int64Value(i *int64) int64

Int64Value safely dereferences a *int64, returning 0 if the pointer is nil.

func IntValue

func IntValue(i *int) int

IntValue safely dereferences a *int, returning 0 if the pointer is nil.

func IsValidIP

func IsValidIP(ip string) bool

func MergeStruct

func MergeStruct(defaults, overrides interface{}) (interface{}, error)

MergeStruct merges fields from src into dest for the same struct type. Fields in src override those in dest, and the result retains methods.

func ModifyFields

func ModifyFields(fields []ent.Field, condition func(f ent.Field) bool, modify func(result []ent.Field, f ent.Field) []ent.Field) []ent.Field

ModifyFields allows modification of fields based on a condition. `condition` determines whether a field should be modified. `modify` specifies how the field list should be changed. *

  • Usage: // Define the base fields from gokit baseFields := schema.User{}.Fields()

    // Use ModifyFields to insert password3 and username2 return utils.ModifyFields(baseFields, // Condition to find where to modify func(f field.Field) bool { return f.Descriptor().Name == "password2" || f.Descriptor().Name == "username" }, // Modify function to insert new fields func(result []field.Field, f field.Field) []field.Field { result = append(result, f) // Keep the original field if f.Descriptor().Name == "password2" { // Add password3 after password2 result = append(result, field.String("password3").Optional().Comment("Third password for the user")) } if f.Descriptor().Name == "username" { // Add username2 after username result = append(result, field.String("username2").Optional().Comment("Second username for the user")) } return result }, )

*

func ResizeAndUploadImage

func ResizeAndUploadImage(c *gin.Context, fileHeader *multipart.FileHeader, processor *ImageProcessor) (string, error)

ResizeAndUploadImage validates and uploads an image file

func SelectItems

func SelectItems(items []string, message string) ([]string, error)

func StringValue

func StringValue(s *string) string

StringValue safely dereferences a *string, returning an empty string if the pointer is nil.

func TimeValue

func TimeValue(t *time.Time) time.Time

TimeValue safely dereferences a *time.Time, returning the zero time if the pointer is nil.

func UploadFile

func UploadFile(c *gin.Context, fileHeader *multipart.FileHeader) (string, error)

UploadFile validates and uploads a file

func UploadImage

func UploadImage(c *gin.Context, fileHeader *multipart.FileHeader) (string, error)

UploadImage validates and uploads an image file

func ValidateFile

func ValidateFile(c *gin.Context, fileHeader *multipart.FileHeader) error

ValidateFile validates the file type and size

func ValidateImage

func ValidateImage(c *gin.Context, fileHeader *multipart.FileHeader) error

ValidateImage validates that the file is an image

Types

type ImageProcessor

type ImageProcessor struct {
	Width    *int             // Target width (optional)
	Height   *int             // Target height (optional)
	Position imaging.Anchor   // Default for Cover and CoverDown
	Method   ProcessingMethod // Specifies the processing method (Scale, ScaleDown, etc.)
}

func NewImageProcessor

func NewImageProcessor(width, height *int, position imaging.Anchor, method ProcessingMethod) *ImageProcessor

NewImageProcessor creates a new instance of ImageProcessor with default settings.

func (*ImageProcessor) Cover

func (ipc *ImageProcessor) Cover(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)

Cover crops the image to fit the specified dimensions.

func (*ImageProcessor) CoverDown

func (ipc *ImageProcessor) CoverDown(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)

CoverDown scales down and crops the image without exceeding original size.

func (*ImageProcessor) Process

func (ipc *ImageProcessor) Process(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)

Process determines the appropriate method to call based on the `Method` in the config.

func (*ImageProcessor) Resize

func (ipc *ImageProcessor) Resize(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)

Resize resizes the image without maintaining aspect ratio.

func (*ImageProcessor) ResizeDown

func (ipc *ImageProcessor) ResizeDown(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)

ResizeDown resizes the image only if the new dimensions are smaller.

func (*ImageProcessor) Scale

func (ipc *ImageProcessor) Scale(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)

Scale scales the image while maintaining aspect ratio.

func (*ImageProcessor) ScaleDown

func (ipc *ImageProcessor) ScaleDown(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)

ScaleDown scales the image while maintaining aspect ratio, not exceeding the original size.

type ProcessingMethod

type ProcessingMethod string
const (
	MethodScale      ProcessingMethod = "Scale"
	MethodScaleDown  ProcessingMethod = "ScaleDown"
	MethodResize     ProcessingMethod = "Resize"
	MethodResizeDown ProcessingMethod = "ResizeDown"
	MethodCover      ProcessingMethod = "Cover"
	MethodCoverDown  ProcessingMethod = "CoverDown"
)

Jump to

Keyboard shortcuts

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