Add file info at top of post images

This commit is contained in:
Dominic Ferrando
2025-04-21 00:57:35 -04:00
parent 65a3c155ce
commit 4e4b5614e0
4 changed files with 79 additions and 3 deletions
+60
View File
@@ -8,6 +8,7 @@ import (
"log"
"mime/multipart"
"os"
"path"
"path/filepath"
"strings"
@@ -111,3 +112,62 @@ func SumUniquePostIps(posts []database.Post) int {
}
return len(uniqueIpHashes)
}
type PostImageInfo struct {
Size int64
Height int
Width int
}
func GetPostImageInfo(mediaPath string) PostImageInfo {
var result PostImageInfo
imagePath := path.Join(POST_IMG_FULL_PATH, mediaPath)
fileInfo, err := os.Stat(imagePath)
if err != nil {
log.Printf("Failed to get image file info at %s: %v", mediaPath, err)
return PostImageInfo{}
}
file, err := os.Open(imagePath)
if err != nil {
log.Printf("Failed to open image at %s: %v", mediaPath, err)
return PostImageInfo{}
}
defer file.Close()
cfg, _, err := image.DecodeConfig(file)
if err != nil {
log.Printf("Failed to decode image at %s: %v", mediaPath, err)
return PostImageInfo{}
}
result.Size = fileInfo.Size()
result.Width = cfg.Width
result.Height = cfg.Height
return result
}
func FormatBytes(bytes int64) string {
const (
KB = 1 << 10 // 1024
MB = 1 << 20
GB = 1 << 30
TB = 1 << 40
)
switch {
case bytes >= TB:
return fmt.Sprintf("%.2f TB", float64(bytes)/float64(TB))
case bytes >= GB:
return fmt.Sprintf("%.2f GB", float64(bytes)/float64(GB))
case bytes >= MB:
return fmt.Sprintf("%.2f MB", float64(bytes)/float64(MB))
case bytes >= KB:
return fmt.Sprintf("%.2f KB", float64(bytes)/float64(KB))
default:
return fmt.Sprintf("%d B", bytes)
}
}