Documentation ¶
Index ¶
- func BoolValue(b *bool) bool
- func Contains(slice []string, item string) bool
- func CopyFile(src, dst string) error
- func DecodeImage(fileHeader *multipart.FileHeader) (image.Image, string, error)
- func DurationValue(d *time.Duration) time.Duration
- func EncodeImage(img image.Image, format string) (*bytes.Buffer, error)
- func ExtractExtension(mimeType string) string
- func ExtractExtensions(mimeTypes []string) string
- func FileAddImports(fileContent string, importsToAdd map[string]string) (string, error)
- func FileExists(path string) bool
- func FileStringExists(f *ast.File, commentText string) bool
- func Float64Value(f *float64) float64
- func GenerateExcelFile(fileName string, headerColumns []string, data [][]interface{}) (*excelize.File, error)
- func GenerateUniqueFileName(fileName string, uniqueID string) string
- func GetFile(filePath string) string
- func GetGoKitRootPath() (string, error)
- func GetModuleName(rootPath string) (string, error)
- func GetProjectRootPath() (string, error)
- func HumanReadableSize(size int64) string
- func InsertFields(fields []ent.Field, insertions map[string][]ent.Field) []ent.Field
- func InsertFieldsAfter(fields []ent.Field, afterFieldName string, newFields ...ent.Field) []ent.Field
- func Int64Value(i *int64) int64
- func IntValue(i *int) int
- func IsValidIP(ip string) bool
- func MergeStruct(defaults, overrides interface{}) (interface{}, error)
- func ModifyFields(fields []ent.Field, condition func(f ent.Field) bool, ...) []ent.Field
- func ResizeAndUploadImage(c *gin.Context, fileHeader *multipart.FileHeader, processor *ImageProcessor) (string, error)
- func SelectItems(items []string, message string) ([]string, error)
- func StringValue(s *string) string
- func TimeValue(t *time.Time) time.Time
- func UploadFile(c *gin.Context, fileHeader *multipart.FileHeader) (string, error)
- func UploadImage(c *gin.Context, fileHeader *multipart.FileHeader) (string, error)
- func ValidateFile(c *gin.Context, fileHeader *multipart.FileHeader) error
- func ValidateImage(c *gin.Context, fileHeader *multipart.FileHeader) error
- type ImageProcessor
- func (ipc *ImageProcessor) Cover(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)
- func (ipc *ImageProcessor) CoverDown(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)
- func (ipc *ImageProcessor) Process(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)
- func (ipc *ImageProcessor) Resize(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)
- func (ipc *ImageProcessor) ResizeDown(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)
- func (ipc *ImageProcessor) Scale(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)
- func (ipc *ImageProcessor) ScaleDown(fileHeader *multipart.FileHeader) (*bytes.Buffer, error)
- type ProcessingMethod
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeImage ¶
DecodeImage decodes an image from a multipart file header.
func DurationValue ¶
DurationValue safely dereferences a *time.Duration, returning 0 if the pointer is nil.
func EncodeImage ¶
EncodeImage encodes an image into a buffer using the detected format.
func ExtractExtension ¶
ExtractExtensions converts a slice of MIME types into a comma-separated list of extensions.
func ExtractExtensions ¶
ExtractExtensions converts a slice of MIME types into a comma-separated list of extensions.
func FileAddImports ¶
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 Float64Value ¶
Float64Value safely dereferences a *float64, returning 0.0 if the pointer is nil.
func GenerateExcelFile ¶
func GenerateUniqueFileName ¶
GenerateUniqueFileName appends a unique identifier to the file name
func GetGoKitRootPath ¶
func GetModuleName ¶
func GetProjectRootPath ¶
func HumanReadableSize ¶
HumanReadableSize converts a size in bytes to a human-readable string.
func InsertFields ¶
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 ¶
Int64Value safely dereferences a *int64, returning 0 if the pointer is nil.
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 StringValue ¶
StringValue safely dereferences a *string, returning an empty string if the pointer is nil.
func TimeValue ¶
TimeValue safely dereferences a *time.Time, returning the zero time if the pointer is nil.
func UploadFile ¶
UploadFile validates and uploads a file
func UploadImage ¶
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" )