Skip to content

Commit

Permalink
feat(BE-70):Set default Profile picture to Generic Avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
noornee committed Dec 2, 2022
1 parent 3b5efd7 commit a149a49
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
5 changes: 5 additions & 0 deletions internal/constants/s3bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

const (
S3_generic_avatar_key = "default"
)
16 changes: 9 additions & 7 deletions internal/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
)

type User struct {
ID primitive.ObjectID `bson:"_id, omitempty"`
Username string `bson:"username" json:"username" validate:"required"`
FirstName string `bson:"first_name" json:"first_name"`
LastName string `bson:"last_name" json:"last_name"`
Email string `bson:"email" json:"email" validate:"required,email"`
Password string `bson:"password" json:"password" validate:"required"`
ID primitive.ObjectID `bson:"_id, omitempty"`
Username string `bson:"username" json:"username" validate:"required"`
FirstName string `bson:"first_name" json:"first_name"`
LastName string `bson:"last_name" json:"last_name"`
Email string `bson:"email" json:"email" validate:"required,email"`
Password string `bson:"password" json:"password" validate:"required"`
ProfileKey string `bson:"profile_key" json:"profile_key"`
ProfileUrl string `bson:"profile_url" json:"profile_url"`
}

type UserResponse struct {
Expand All @@ -35,5 +37,5 @@ type PasswordReset struct {
}

type PasswordForgot struct {
Email string `bson:"email" json:"email" validate:"required"`
Email string `bson:"email" json:"email" validate:"required"`
}
51 changes: 51 additions & 0 deletions pkg/repository/storage/s3/s3upload.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package s3

import (
"fmt"
"io"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/google/uuid"
"github.com/workshopapps/pictureminer.api/internal/config"
"github.com/workshopapps/pictureminer.api/internal/constants"
)

var AccessKeyID string
Expand Down Expand Up @@ -64,3 +70,48 @@ func UploadImage(file io.ReadCloser, filename string) (string, error) {

return filepath, nil
}

// This checks if an s3 bucket key exists
func keyExists(key string) (bool, error) {
svc := s3.New(s3session)
MyBucket = config.GetConfig().S3.BucketName

_, err := svc.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(MyBucket),
Key: aws.String(key),
})

if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case "NotFound":
return false, nil
default:
return false, err
}
}
}
return true, nil
}

// This sets up an s3 bucket key named "default" for the client
// it also sets up a default profile picture for new users.
func DefaultProfile() (profile_url, profile_key string) {
//func DefaultProfile() (string, string) {
cwd, _ := os.Getwd()
file, _ := os.Open(fmt.Sprintf("%s/static/avatar.jpg", cwd))
defer file.Close()

profile_url = fmt.Sprintf("https://miner-pictures.s3.amazonaws.com/%s", constants.S3_generic_avatar_key)
profile_key = uuid.New().String()

exists, _ := keyExists(constants.S3_generic_avatar_key)

// if the s3 bucket key "default" doesnt exist, create one
if !exists {
profile_url, _ := UploadImage(file, constants.S3_generic_avatar_key)
return profile_url, profile_key
}

return profile_url, profile_key
}
8 changes: 7 additions & 1 deletion service/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/workshopapps/pictureminer.api/internal/constants"
"github.com/workshopapps/pictureminer.api/internal/model"
"github.com/workshopapps/pictureminer.api/pkg/repository/storage/mongodb"
"github.com/workshopapps/pictureminer.api/pkg/repository/storage/s3"
"github.com/workshopapps/pictureminer.api/utility"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand All @@ -24,8 +25,13 @@ func SignUpUser(user model.User) (model.UserResponse, string, int, error) {
}

hash, _ := bcrypt.GenerateFromPassword([]byte(user.Password), 10)

profile_url, profile_key := s3.DefaultProfile()

user.Password = string(hash)
user.ID = primitive.NewObjectID()
user.ProfileUrl = profile_url
user.ProfileKey = profile_key

// save to DB
database := config.GetConfig().Mongodb.Database
Expand Down Expand Up @@ -121,7 +127,7 @@ func ForgotPassword(reqBody model.PasswordForgot) (int, error) {
if err != nil {
return 404, fmt.Errorf("user does not exist: %s", err.Error())
}

var w http.ResponseWriter
var r *http.Request

Expand Down
Binary file added static/avatar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a149a49

Please sign in to comment.