mirror of
https://github.com/wisplite/raster.git
synced 2026-05-01 06:32:44 -05:00
Add first metadata values and vastly improve gallery tiling
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"mime/multipart"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/wisplite/raster/internal/db"
|
||||
"github.com/wisplite/raster/internal/models"
|
||||
"mime/multipart"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func UploadMedia(file *multipart.FileHeader, albumID string, accessToken string) (models.Media, error) {
|
||||
@@ -24,13 +30,31 @@ func UploadMedia(file *multipart.FileHeader, albumID string, accessToken string)
|
||||
if albumID == "" {
|
||||
albumPath = "root"
|
||||
}
|
||||
|
||||
// Extract metadata (dimensions)
|
||||
var meta datatypes.JSON
|
||||
src, err := file.Open()
|
||||
if err == nil {
|
||||
defer src.Close()
|
||||
cfg, _, err := image.DecodeConfig(src)
|
||||
if err == nil {
|
||||
m := map[string]int{
|
||||
"width": cfg.Width,
|
||||
"height": cfg.Height,
|
||||
}
|
||||
b, _ := json.Marshal(m)
|
||||
meta = datatypes.JSON(b)
|
||||
}
|
||||
}
|
||||
|
||||
mediaID := uuid.New().String()
|
||||
mediaPath := fmt.Sprintf("media/%s/%s.%s", albumPath, mediaID, file.Filename)
|
||||
media := models.Media{
|
||||
ID: mediaID,
|
||||
AlbumID: albumID,
|
||||
Path: mediaPath,
|
||||
Type: file.Header.Get("Content-Type"),
|
||||
ID: mediaID,
|
||||
AlbumID: albumID,
|
||||
Path: mediaPath,
|
||||
Type: file.Header.Get("Content-Type"),
|
||||
Metadata: meta,
|
||||
}
|
||||
result := db.GetDB().Create(&media)
|
||||
if result.Error != nil {
|
||||
|
||||
Reference in New Issue
Block a user