Skip to content

Commit

Permalink
Return default profile picture if user has no profile picture
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed May 10, 2024
1 parent 92b82a0 commit 457d10f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion database/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type User struct {
Email string
Username string
Avatar string
Avatar null.String
Password string
CreatedAt time.Time `json:"createdAt"`
UpdatedAt null.Time `json:"updatedAt"`
Expand Down
4 changes: 2 additions & 2 deletions dto/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type User struct {

// InternalUser contains private user info that should not be exposed to clients.
type InternalUser struct {
Avatar string `json:"avatar"`
Password string `json:"password"`
Avatar null.String `json:"avatar"`
Password string `json:"password"`
User
}

Expand Down
8 changes: 7 additions & 1 deletion http/controller/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Service interface {

type StorageService interface {
GetFS() fs.StatFS
GetEmbedImagesFS() fs.StatFS
}

type Controller struct {
Expand Down Expand Up @@ -68,7 +69,12 @@ func (ctrl *Controller) ShowAvatar(response *goyave.Response, request *goyave.Re
return
}

response.File(ctrl.StorageService.GetFS(), user.Avatar)
if !user.Avatar.Valid {
response.File(ctrl.StorageService.GetEmbedImagesFS(), "default_profile_picture.png")
return
}

response.File(ctrl.StorageService.GetFS(), user.Avatar.String)
}

func (ctrl *Controller) Register(response *goyave.Response, request *goyave.Request) {
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func main() {
fmt.Fprintln(os.Stderr, err.(*errors.Error).String())
os.Exit(1)
}
imgFS, err := resources.Sub("resources/img")
if err != nil {
fmt.Fprintln(os.Stderr, err.(*errors.Error).String())
os.Exit(1)
}

opts := goyave.Options{
LangFS: langFS,
Expand All @@ -58,7 +63,7 @@ func main() {
s.Logger.Info("Server is shutting down")
})

registerServices(server)
registerServices(server, imgFS)

server.Logger.Info("Registering routes")
server.RegisterRoutes(route.Register)
Expand All @@ -74,7 +79,7 @@ func main() {
}
}

func registerServices(server *goyave.Server) {
func registerServices(server *goyave.Server, imgFS fsutil.Embed) {
server.Logger.Info("Registering services")

session := session.GORM(server.DB(), nil)
Expand All @@ -86,7 +91,7 @@ func registerServices(server *goyave.Server) {
if err != nil {
panic(errors.New(err))
}
storageService := storage.NewService(storageFS)
storageService := storage.NewService(storageFS, imgFS)

server.RegisterService(storageService)
server.RegisterService(user.NewService(session, server.Logger, userRepo, storageService))
Expand Down
Binary file added resources/img/default_profile_picture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions service/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ type FS interface {
}

type Service struct {
FS FS
FS FS
EmbedImagesFS fs.StatFS
}

func NewService(fs FS) *Service {
func NewService(fs FS, imgFS fs.StatFS) *Service {
return &Service{
FS: fs,
FS: fs,
EmbedImagesFS: imgFS,
}
}

func (s *Service) GetEmbedImagesFS() fs.StatFS {
return s.EmbedImagesFS
}

func (s *Service) GetFS() fs.StatFS {
return s.FS
}
Expand Down
6 changes: 3 additions & 3 deletions service/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func (s *Service) Register(ctx context.Context, registerDTO *dto.RegisterUser) e
if err != nil {
return errors.New(err)
}
user.Avatar = filename
user.Avatar.SetValid(filename)
}

_, err = s.Repository.Create(ctx, user)
if err != nil {
if err := s.StorageService.Delete(user.Avatar); err != nil {
if err != nil && user.Avatar.Valid {
if err := s.StorageService.Delete(user.Avatar.String); err != nil {
s.Logger.Error(errors.New(err))
}
}
Expand Down

0 comments on commit 457d10f

Please sign in to comment.