Create API for adding an avatar to a user
This commit is contained in:
parent
da7549eeda
commit
ad90f9c95e
7 changed files with 131 additions and 24 deletions
40
platform/avatar.go
Normal file
40
platform/avatar.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/file"
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func AvatarCreate(ctx context.Context, u User, upload file.Upload) error {
|
||||
f, err := file.NewFileReader(file.CollectionAvatar, upload.UUID)
|
||||
// Decode image (supports PNG, JPG, GIF)
|
||||
img, err := imaging.Decode(f)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decode: %w", err)
|
||||
}
|
||||
|
||||
// Resize to 200x200, maintaining aspect ratio
|
||||
avatar := imaging.Fill(img, 200, 200, imaging.Center, imaging.Lanczos)
|
||||
|
||||
// Save or encode
|
||||
//filename := fmt.Sprintf("avatar-%s.jpg", upload.UUID.String())
|
||||
//err = imaging.Save(avatar, filename)
|
||||
//log.Info().Str("filename", filename).Msg("wrote avatar file")
|
||||
// Or encode to buffer: imaging.Encode(writer, avatar, imaging.JPEG)
|
||||
writer := &bytes.Buffer{}
|
||||
err = imaging.Encode(writer, avatar, imaging.PNG)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode: %w", err)
|
||||
}
|
||||
err = file.FileContentWrite(writer, file.CollectionAvatar, upload.UUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("write content: %w", err)
|
||||
}
|
||||
log.Info().Str("uuid", upload.UUID.String()).Msg("wrote avatar file")
|
||||
return nil
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ var collectionToExtension map[Collection]string = map[Collection]string{
|
|||
CollectionAudioNormalized: "ogg",
|
||||
CollectionAudioRaw: "raw",
|
||||
CollectionAudioTranscoded: "ogg",
|
||||
CollectionAvatar: "png",
|
||||
CollectionCSV: "csv",
|
||||
CollectionLogo: "png",
|
||||
CollectionPublicImage: "img",
|
||||
|
|
@ -28,6 +29,7 @@ var collectionToSubdir map[Collection]string = map[Collection]string{
|
|||
CollectionAudioNormalized: "audio-normalized",
|
||||
CollectionAudioRaw: "audio-raw",
|
||||
CollectionAudioTranscoded: "audio-transcoded",
|
||||
CollectionAvatar: "avatar",
|
||||
CollectionCSV: "csv",
|
||||
CollectionLogo: "logo",
|
||||
CollectionPublicImage: "public-image",
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ const (
|
|||
CollectionAudioRaw Collection = iota
|
||||
CollectionAudioNormalized
|
||||
CollectionAudioTranscoded
|
||||
CollectionAvatar
|
||||
CollectionCSV
|
||||
CollectionImageRaw
|
||||
CollectionLogo
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue