78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
package persistence
|
|
|
|
import (
|
|
entity "RenewCMS/internal/infrastructure/persistence/models"
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
stdimage "image"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
_ "golang.org/x/image/webp"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var extensionContentTypes = map[string]string{
|
|
".png": "image/png",
|
|
".jpg": "image/jpeg",
|
|
".jpeg": "image/jpeg",
|
|
".webp": "image/webp",
|
|
".svg": "image/svg+xml",
|
|
}
|
|
|
|
func BackfillImageMetadata(db *gorm.DB, uploadDir string) error {
|
|
var pending []entity.Image
|
|
err := db.Where("filename = '' OR filename IS NULL").Order("id asc").Find(&pending).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, row := range pending {
|
|
metadata, ok := readStoredImage(uploadDir, row.Path)
|
|
if !ok {
|
|
continue
|
|
}
|
|
err := db.Model(&entity.Image{}).Where("id = ?", row.ID).UpdateColumns(metadata).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func readStoredImage(uploadDir string, storedPath string) (map[string]any, bool) {
|
|
fileName := filepath.Base(storedPath)
|
|
contentType, known := extensionContentTypes[filepath.Ext(fileName)]
|
|
if !known {
|
|
return nil, false
|
|
}
|
|
|
|
data, err := os.ReadFile(filepath.Join(uploadDir, fileName))
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
|
|
width, height := 0, 0
|
|
if contentType != "image/svg+xml" {
|
|
config, _, err := stdimage.DecodeConfig(bytes.NewReader(data))
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
width, height = config.Width, config.Height
|
|
}
|
|
|
|
checksum := sha256.Sum256(data)
|
|
return map[string]any{
|
|
"filename": fileName,
|
|
"size": int64(len(data)),
|
|
"content_type": contentType,
|
|
"width": width,
|
|
"height": height,
|
|
"sha256": hex.EncodeToString(checksum[:]),
|
|
}, true
|
|
}
|