feat: hashes in avatar file names (closes #19)
This commit is contained in:
parent
e36bd247f5
commit
163e7c3fd6
17 changed files with 133 additions and 77 deletions
|
@ -3,7 +3,9 @@ package db
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
@ -23,8 +25,8 @@ 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) (
|
||||
webp io.Reader,
|
||||
jpg io.Reader,
|
||||
webp *bytes.Buffer,
|
||||
jpg *bytes.Buffer,
|
||||
err error,
|
||||
) {
|
||||
data = strings.TrimSpace(data)
|
||||
|
@ -142,53 +144,59 @@ func (db *DB) ConvertAvatar(data string) (
|
|||
}
|
||||
|
||||
func (db *DB) WriteUserAvatar(ctx context.Context,
|
||||
userID xid.ID, webp io.Reader, jpeg io.Reader,
|
||||
userID xid.ID, webp *bytes.Buffer, jpeg *bytes.Buffer,
|
||||
) (
|
||||
webpLocation string,
|
||||
jpegLocation string,
|
||||
err error,
|
||||
hash string, err error,
|
||||
) {
|
||||
_, err = db.minio.PutObject(ctx, db.minioBucket, "/users/"+userID.String()+".webp", webp, -1, minio.PutObjectOptions{
|
||||
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{
|
||||
ContentType: "image/webp",
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err, "uploading webp avatar")
|
||||
return "", errors.Wrap(err, "uploading webp avatar")
|
||||
}
|
||||
|
||||
_, err = db.minio.PutObject(ctx, db.minioBucket, "/users/"+userID.String()+".jpg", jpeg, -1, minio.PutObjectOptions{
|
||||
_, err = db.minio.PutObject(ctx, db.minioBucket, "/users/"+userID.String()+"/"+hash+".jpg", jpeg, -1, minio.PutObjectOptions{
|
||||
ContentType: "image/jpeg",
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err, "uploading jpeg avatar")
|
||||
return "", errors.Wrap(err, "uploading jpeg avatar")
|
||||
}
|
||||
|
||||
return db.baseURL.JoinPath("/media/users/" + userID.String() + ".webp").String(),
|
||||
db.baseURL.JoinPath("/media/users/" + userID.String() + ".jpg").String(),
|
||||
nil
|
||||
return hash, nil
|
||||
}
|
||||
|
||||
func (db *DB) WriteMemberAvatar(ctx context.Context,
|
||||
memberID xid.ID, webp io.Reader, jpeg io.Reader,
|
||||
memberID xid.ID, webp *bytes.Buffer, jpeg *bytes.Buffer,
|
||||
) (
|
||||
webpLocation string,
|
||||
jpegLocation string,
|
||||
err error,
|
||||
hash string, err error,
|
||||
) {
|
||||
_, err = db.minio.PutObject(ctx, db.minioBucket, "/members/"+memberID.String()+".webp", webp, -1, minio.PutObjectOptions{
|
||||
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{
|
||||
ContentType: "image/webp",
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err, "uploading webp avatar")
|
||||
return "", errors.Wrap(err, "uploading webp avatar")
|
||||
}
|
||||
|
||||
_, err = db.minio.PutObject(ctx, db.minioBucket, "/members/"+memberID.String()+".jpg", jpeg, -1, minio.PutObjectOptions{
|
||||
_, err = db.minio.PutObject(ctx, db.minioBucket, "/members/"+memberID.String()+"/"+hash+".jpg", jpeg, -1, minio.PutObjectOptions{
|
||||
ContentType: "image/jpeg",
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err, "uploading jpeg avatar")
|
||||
return "", errors.Wrap(err, "uploading jpeg avatar")
|
||||
}
|
||||
|
||||
return db.baseURL.JoinPath("/media/members/" + memberID.String() + ".webp").String(),
|
||||
db.baseURL.JoinPath("/media/members/" + memberID.String() + ".jpg").String(),
|
||||
nil
|
||||
return hash, nil
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ type Member struct {
|
|||
Name string
|
||||
DisplayName *string
|
||||
Bio *string
|
||||
AvatarURLs []string `db:"avatar_urls"`
|
||||
Avatar *string
|
||||
Links []string
|
||||
Names []FieldEntry
|
||||
Pronouns []PronounEntry
|
||||
|
@ -61,7 +61,7 @@ func (db *DB) UserMember(ctx context.Context, userID xid.ID, memberRef string) (
|
|||
|
||||
// UserMembers returns all of a user's members, sorted by name.
|
||||
func (db *DB) UserMembers(ctx context.Context, userID xid.ID) (ms []Member, err error) {
|
||||
sql, args, err := sq.Select("id", "user_id", "name", "display_name", "bio", "avatar_urls", "names", "pronouns").
|
||||
sql, args, err := sq.Select("id", "user_id", "name", "display_name", "bio", "avatar", "names", "pronouns").
|
||||
From("members").Where("user_id = ?", userID).
|
||||
OrderBy("name", "id").ToSql()
|
||||
if err != nil {
|
||||
|
@ -141,9 +141,9 @@ func (db *DB) UpdateMember(
|
|||
tx pgx.Tx, id xid.ID,
|
||||
name, displayName, bio *string,
|
||||
links *[]string,
|
||||
avatarURLs []string,
|
||||
avatar *string,
|
||||
) (m Member, err error) {
|
||||
if name == nil && displayName == nil && bio == nil && links == nil && avatarURLs == nil {
|
||||
if name == nil && displayName == nil && bio == nil && links == nil && avatar == nil {
|
||||
// get member
|
||||
sql, args, err := sq.Select("*").From("members").Where("id = ?", id).ToSql()
|
||||
if err != nil {
|
||||
|
@ -183,8 +183,12 @@ func (db *DB) UpdateMember(
|
|||
builder = builder.Set("links", *links)
|
||||
}
|
||||
|
||||
if avatarURLs != nil {
|
||||
builder = builder.Set("avatar_urls", avatarURLs)
|
||||
if avatar != nil {
|
||||
if *avatar == "" {
|
||||
builder = builder.Set("avatar", nil)
|
||||
} else {
|
||||
builder = builder.Set("avatar", avatar)
|
||||
}
|
||||
}
|
||||
|
||||
sql, args, err := builder.ToSql()
|
||||
|
|
|
@ -19,8 +19,8 @@ type User struct {
|
|||
DisplayName *string
|
||||
Bio *string
|
||||
|
||||
AvatarURLs []string `db:"avatar_urls"`
|
||||
Links []string
|
||||
Avatar *string
|
||||
Links []string
|
||||
|
||||
Names []FieldEntry
|
||||
Pronouns []PronounEntry
|
||||
|
@ -208,9 +208,9 @@ func (db *DB) UpdateUser(
|
|||
tx pgx.Tx, id xid.ID,
|
||||
displayName, bio *string,
|
||||
links *[]string,
|
||||
avatarURLs []string,
|
||||
avatar *string,
|
||||
) (u User, err error) {
|
||||
if displayName == nil && bio == nil && links == nil && avatarURLs == nil {
|
||||
if displayName == nil && bio == nil && links == nil && avatar == nil {
|
||||
sql, args, err := sq.Select("*").From("users").Where("id = ?", id).ToSql()
|
||||
if err != nil {
|
||||
return u, errors.Wrap(err, "building sql")
|
||||
|
@ -243,8 +243,12 @@ func (db *DB) UpdateUser(
|
|||
builder = builder.Set("links", *links)
|
||||
}
|
||||
|
||||
if avatarURLs != nil {
|
||||
builder = builder.Set("avatar_urls", avatarURLs)
|
||||
if avatar != nil {
|
||||
if *avatar == "" {
|
||||
builder = builder.Set("avatar", nil)
|
||||
} else {
|
||||
builder = builder.Set("avatar", avatar)
|
||||
}
|
||||
}
|
||||
|
||||
sql, args, err := builder.ToSql()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue