common

package
v0.32.0-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertFileToPNG

func ConvertFileToPNG(ctx context.Context, inputPath string, outputPath string) (string, error)

ConvertFileToPNG converts an image file to PNG format and saves it to the specified output path. If outputPath is empty, it will replace the original file extension with .png.

func ConvertToPNG

func ConvertToPNG(ctx context.Context, input any) ([]byte, error)

ConvertToPNG converts an image from various formats (JPEG, GIF, etc.) to PNG. It accepts either a byte slice containing the image data or a file path. Returns the PNG image as a byte slice.

func ConvertURLImageToPNG

func ConvertURLImageToPNG(ctx context.Context, url string) ([]byte, error)

ConvertURLImageToPNG downloads an image from a URL and converts it to PNG format. Returns the PNG image as a byte slice.

func DownloadFile

func DownloadFile(sourceURL string) (string, error)

DownloadFile downloads a file from a URL and saves it to a temporary directory with security validation. It follows redirects up to a maximum of 10 times. The filename is determined in the following order: 1. From Content-Disposition header if present and valid 2. From the final URL after redirects if valid 3. Falls back to a timestamp-based name if both sources are invalid

The function implements several security measures: - Validates filenames to prevent directory traversal - Restricts filenames to alphanumeric characters, dots, hyphens, underscores, and spaces - Ensures final path remains within temporary directory - Verifies path safety after normalization

Returns the path to the downloaded file and any error encountered.

func DownloadImage

func DownloadImage(url string) ([]byte, error)

DownloadImage downloads an image from a given URL and returns it as a byte slice with retries

func GetImageFormat

func GetImageFormat(data []byte) (string, error)

GetImageFormat detects the format of an image from its bytes. Returns the format as a string (e.g., "png", "jpeg", "gif").

func IsPNG

func IsPNG(data []byte) bool

IsPNG checks if the provided image data is already in PNG format.

func SaveImageAsPNG

func SaveImageAsPNG(data []byte, outputPath string) error

SaveImageAsPNG saves image data as a PNG file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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