2022-09-20 03:55:00 -07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-03-12 18:04:09 -07:00
|
|
|
"crypto/sha256"
|
2022-09-20 03:55:00 -07:00
|
|
|
"encoding/base64"
|
2023-03-12 18:04:09 -07:00
|
|
|
"encoding/hex"
|
2023-03-30 05:44:32 -07:00
|
|
|
"image"
|
|
|
|
_ "image/gif"
|
|
|
|
"image/jpeg"
|
|
|
|
_ "image/png"
|
2022-09-20 03:55:00 -07:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2023-03-30 05:44:32 -07:00
|
|
|
"github.com/disintegration/imaging"
|
2022-09-20 03:55:00 -07:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/rs/xid"
|
2023-03-30 05:44:32 -07:00
|
|
|
|
|
|
|
"github.com/chai2010/webp"
|
2022-09-20 03:55:00 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const ErrInvalidDataURI = errors.Sentinel("invalid data URI")
|
|
|
|
const ErrInvalidContentType = errors.Sentinel("invalid avatar content type")
|
|
|
|
|
|
|
|
// ConvertAvatar parses an avatar from a data URI, converts it to WebP and JPEG, and returns the results.
|
|
|
|
func (db *DB) ConvertAvatar(data string) (
|
2023-03-30 05:44:32 -07:00
|
|
|
webpOut *bytes.Buffer,
|
|
|
|
jpgOut *bytes.Buffer,
|
2022-09-20 03:55:00 -07:00
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
data = strings.TrimSpace(data)
|
|
|
|
if !strings.Contains(data, ",") || !strings.Contains(data, ":") || !strings.Contains(data, ";") {
|
|
|
|
return nil, nil, ErrInvalidDataURI
|
|
|
|
}
|
|
|
|
split := strings.Split(data, ",")
|
|
|
|
|
2023-03-30 05:44:32 -07:00
|
|
|
rawData, err := base64.StdEncoding.DecodeString(split[1])
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "invalid base64 data")
|
|
|
|
}
|
|
|
|
|
2023-03-30 05:44:32 -07:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(rawData))
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
2023-05-06 06:53:07 -07:00
|
|
|
return nil, nil, errors.Wrap(err, "decoding image")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-03-30 05:44:32 -07:00
|
|
|
resized := imaging.Fill(img, 512, 512, imaging.Center, imaging.Linear)
|
2022-09-20 03:55:00 -07:00
|
|
|
|
2023-03-30 05:44:32 -07:00
|
|
|
webpOut = new(bytes.Buffer)
|
|
|
|
err = webp.Encode(webpOut, resized, &webp.Options{
|
|
|
|
Quality: 90,
|
|
|
|
})
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
2023-03-30 05:44:32 -07:00
|
|
|
return nil, nil, errors.Wrap(err, "encoding WebP image")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-03-30 05:44:32 -07:00
|
|
|
jpgOut = new(bytes.Buffer)
|
|
|
|
err = jpeg.Encode(jpgOut, resized, &jpeg.Options{
|
|
|
|
Quality: 80,
|
|
|
|
})
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
2023-03-30 05:44:32 -07:00
|
|
|
return nil, nil, errors.Wrap(err, "encoding JPEG image")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-03-30 05:44:32 -07:00
|
|
|
return webpOut, jpgOut, nil
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) WriteUserAvatar(ctx context.Context,
|
2023-03-12 18:04:09 -07:00
|
|
|
userID xid.ID, webp *bytes.Buffer, jpeg *bytes.Buffer,
|
2022-09-20 03:55:00 -07:00
|
|
|
) (
|
2023-03-12 18:04:09 -07:00
|
|
|
hash string, err error,
|
2022-09-20 03:55:00 -07:00
|
|
|
) {
|
2023-03-12 18:04:09 -07:00
|
|
|
hasher := sha256.New()
|
|
|
|
_, err = hasher.Write(webp.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "hashing webp avatar")
|
|
|
|
}
|
|
|
|
hash = hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
|
|
|
|
_, err = db.minio.PutObject(ctx, db.minioBucket, "/users/"+userID.String()+"/"+hash+".webp", webp, -1, minio.PutObjectOptions{
|
2022-09-20 03:55:00 -07:00
|
|
|
ContentType: "image/webp",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-03-12 18:04:09 -07:00
|
|
|
return "", errors.Wrap(err, "uploading webp avatar")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:04:09 -07:00
|
|
|
_, err = db.minio.PutObject(ctx, db.minioBucket, "/users/"+userID.String()+"/"+hash+".jpg", jpeg, -1, minio.PutObjectOptions{
|
2022-09-20 03:55:00 -07:00
|
|
|
ContentType: "image/jpeg",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-03-12 18:04:09 -07:00
|
|
|
return "", errors.Wrap(err, "uploading jpeg avatar")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:04:09 -07:00
|
|
|
return hash, nil
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
2022-10-03 01:59:30 -07:00
|
|
|
|
|
|
|
func (db *DB) WriteMemberAvatar(ctx context.Context,
|
2023-03-12 18:04:09 -07:00
|
|
|
memberID xid.ID, webp *bytes.Buffer, jpeg *bytes.Buffer,
|
2022-10-03 01:59:30 -07:00
|
|
|
) (
|
2023-03-12 18:04:09 -07:00
|
|
|
hash string, err error,
|
2022-10-03 01:59:30 -07:00
|
|
|
) {
|
2023-03-12 18:04:09 -07:00
|
|
|
hasher := sha256.New()
|
|
|
|
_, err = hasher.Write(webp.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "hashing webp avatar")
|
|
|
|
}
|
|
|
|
hash = hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
|
|
|
|
_, err = db.minio.PutObject(ctx, db.minioBucket, "/members/"+memberID.String()+"/"+hash+".webp", webp, -1, minio.PutObjectOptions{
|
2022-10-03 01:59:30 -07:00
|
|
|
ContentType: "image/webp",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-03-12 18:04:09 -07:00
|
|
|
return "", errors.Wrap(err, "uploading webp avatar")
|
2022-10-03 01:59:30 -07:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:04:09 -07:00
|
|
|
_, err = db.minio.PutObject(ctx, db.minioBucket, "/members/"+memberID.String()+"/"+hash+".jpg", jpeg, -1, minio.PutObjectOptions{
|
2022-10-03 01:59:30 -07:00
|
|
|
ContentType: "image/jpeg",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-03-12 18:04:09 -07:00
|
|
|
return "", errors.Wrap(err, "uploading jpeg avatar")
|
2022-10-03 01:59:30 -07:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:04:09 -07:00
|
|
|
return hash, nil
|
2022-10-03 01:59:30 -07:00
|
|
|
}
|
2023-03-12 18:19:03 -07:00
|
|
|
|
|
|
|
func (db *DB) DeleteUserAvatar(ctx context.Context, userID xid.ID, hash string) error {
|
|
|
|
err := db.minio.RemoveObject(ctx, db.minioBucket, "/users/"+userID.String()+"/"+hash+".webp", minio.RemoveObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting webp avatar")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.minio.RemoveObject(ctx, db.minioBucket, "/users/"+userID.String()+"/"+hash+".jpg", minio.RemoveObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting jpeg avatar")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) DeleteMemberAvatar(ctx context.Context, memberID xid.ID, hash string) error {
|
|
|
|
err := db.minio.RemoveObject(ctx, db.minioBucket, "/members/"+memberID.String()+"/"+hash+".webp", minio.RemoveObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting webp avatar")
|
|
|
|
}
|
|
|
|
|
2023-03-15 07:24:51 -07:00
|
|
|
err = db.minio.RemoveObject(ctx, db.minioBucket, "/members/"+memberID.String()+"/"+hash+".jpg", minio.RemoveObjectOptions{})
|
2023-03-12 18:19:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting jpeg avatar")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-15 07:24:51 -07:00
|
|
|
|
|
|
|
func (db *DB) UserAvatar(ctx context.Context, userID xid.ID, hash string) (io.ReadCloser, error) {
|
|
|
|
obj, err := db.minio.GetObject(ctx, db.minioBucket, "/users/"+userID.String()+"/"+hash+".webp", minio.GetObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "getting object")
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) MemberAvatar(ctx context.Context, memberID xid.ID, hash string) (io.ReadCloser, error) {
|
|
|
|
obj, err := db.minio.GetObject(ctx, db.minioBucket, "/members/"+memberID.String()+"/"+hash+".webp", minio.GetObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "getting object")
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|