diff options
| author | Bobby <[email protected]> | 2025-07-17 12:39:44 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-07-17 12:39:44 +0530 |
| commit | 46e41088bab946856867253c4fa268f9084b86a5 (patch) | |
| tree | 617b5b31de5b2c0fe0cfd27f29b5580a784d468f /utils/format | |
| parent | b0ba363696a758a8d0637107bd29a0a9ac1382d4 (diff) | |
| download | imageboard-46e41088bab946856867253c4fa268f9084b86a5.tar.xz imageboard-46e41088bab946856867253c4fa268f9084b86a5.zip | |
webp support, decode and transformation functions
Diffstat (limited to 'utils/format')
| -rw-r--r-- | utils/format/files.go | 71 | ||||
| -rw-r--r-- | utils/format/format.go | 32 | ||||
| -rw-r--r-- | utils/format/numbers.go | 20 | ||||
| -rw-r--r-- | utils/format/time.go | 7 |
4 files changed, 98 insertions, 32 deletions
diff --git a/utils/format/files.go b/utils/format/files.go new file mode 100644 index 0000000..82f1546 --- /dev/null +++ b/utils/format/files.go @@ -0,0 +1,71 @@ +package format + +import ( + "bytes" + "fmt" + "image" + _ "image/gif" + "image/jpeg" + "image/png" + "strings" + + "golang.org/x/image/webp" +) + +func init() { + image.RegisterFormat("webp", "RIFF????WEBP", webp.Decode, webp.DecodeConfig) +} + +func FileSize(size int64) string { + const unit = 1024 + if size < unit { + return fmt.Sprintf("%d B", size) + } + div, exp := int64(unit), 0 + for n := size / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + + val := float64(size) / float64(div) + unitStr := "KMGTPE"[exp : exp+1] + if val == float64(int64(val)) { + return fmt.Sprintf("%d %sB", int64(val), unitStr) + } + return fmt.Sprintf("%.2f %sB", val, unitStr) +} + +func RemoveExtension(fileName string) string { + if fileName == "" { + return fileName + } + parts := strings.Split(fileName, ".") + if len(parts) <= 1 { + return fileName + } + return strings.Join(parts[:len(parts)-1], ".") +} + +func DecodeImage(imgData []byte) (image.Image, string, error) { + img, formatName, err := image.Decode(bytes.NewReader(imgData)) + return img, formatName, err +} + +func GetImageFileSize(img image.Image) int64 { + var buf bytes.Buffer + switch img.(type) { + case *image.NRGBA, *image.RGBA, *image.YCbCr: + // Use PNG encoding for lossless compression + err := png.Encode(&buf, img) + if err != nil { + return 0 + } + default: + // Fallback to JPEG encoding for other formats + err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 85}) + if err != nil { + return 0 + } + } + return int64(buf.Len()) +} diff --git a/utils/format/format.go b/utils/format/format.go deleted file mode 100644 index e57bc1f..0000000 --- a/utils/format/format.go +++ /dev/null @@ -1,32 +0,0 @@ -package format - -import "fmt" - -func FileSize(size int64) string { - const unit = 1024 - if size < unit { - return fmt.Sprintf("%d B", size) - } - div, exp := int64(unit), 0 - for n := size / unit; n >= unit; n /= unit { - div *= unit - exp++ - } - - val := float64(size) / float64(div) - unitStr := "KMGTPE"[exp : exp+1] - if val == float64(int64(val)) { - return fmt.Sprintf("%d %sB", int64(val), unitStr) - } - return fmt.Sprintf("%.2f %sB", val, unitStr) -} - -func Count(count int64) string { - if count < 1000 { - return fmt.Sprintf("%d", count) - } else if count < 1000000 { - return fmt.Sprintf("%.1fK", float64(count)/1000) - } else { - return fmt.Sprintf("%.1fM", float64(count)/1000000) - } -} diff --git a/utils/format/numbers.go b/utils/format/numbers.go new file mode 100644 index 0000000..8f546f1 --- /dev/null +++ b/utils/format/numbers.go @@ -0,0 +1,20 @@ +package format + +import "fmt" + +func Count(count int64) string { + if count < 1000 { + return fmt.Sprintf("%d", count) + } else if count < 1000000 { + return fmt.Sprintf("%.1fK", float64(count)/1000) + } else { + return fmt.Sprintf("%.1fM", float64(count)/1000000) + } +} + +func Int64ToString(value int64) string { + if value < 0 { + return fmt.Sprintf("-%d", -value) + } + return fmt.Sprintf("%d", value) +} diff --git a/utils/format/time.go b/utils/format/time.go new file mode 100644 index 0000000..3e65337 --- /dev/null +++ b/utils/format/time.go @@ -0,0 +1,7 @@ +package format + +import "time" + +func GetCurrentTimeAsTimestamp() int64 { + return time.Now().Unix() +} |
