Skip to content

Commit

Permalink
update user fix
Browse files Browse the repository at this point in the history
- wrong url comparison
- add preprocessed domain for
comparison
- delete user media when the
request is empty

Author: [email protected]
  • Loading branch information
thanhpp committed Jan 21, 2022
1 parent 4985b3d commit 2734d88
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
4 changes: 4 additions & 0 deletions internal/laclongquan/application/userhandler_setinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ func (u UserHandler) SetUserInfo(
}
user.UpdateAvatar(avatar.ID())
// logger.Debugf("set user info - update avatar: %s", avatar.ID())
} else {
user.UpdateAvatar("")
}

if coverImage != nil {
if !coverImage.IsOwner(user) {
return nil, ErrCanNotUseMedia
}
user.UpdateCoverImage(coverImage.ID())
} else {
user.UpdateCoverImage("")
}

go func(esUser entity.User) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func (ctrl UserController) SetUserInfo(c *gin.Context) {
}

avatarURL, coverURL := ctrl.formUserMediaUrlFn(user.User)
if avatarURL == req.Avatar {
// logger.Debugf("avatarURL: %s, coverURL: %s", avatarURL, coverURL)
// logger.Debugf("req.avatarURL: %s, req.coverURL: %s", req.Avatar, req.CoverImage)
if avatarURL != req.Avatar {
// logger.Debugf("avatar url: %s", req.Avatar)
avaPostID, avaMediaID, err := ctrl.resolveMediaUrlFn(req.Avatar)
if err != nil && !errors.Is(err, ErrEmptyMediaURL) {
logger.Errorf("resolve media url (avatar) error: %v", err)
Expand All @@ -57,21 +60,23 @@ func (ctrl UserController) SetUserInfo(c *gin.Context) {
if len(avaPostID)+len(avaMediaID) != 0 {
avatarMedia, err = ctrl.postHdl.GetMedia(c, userID.String(), avaPostID, avaMediaID)
if err != nil {
logger.Errorf("get media (avatar) error: %v", err)
logger.Errorf("get media (avatar) error from postID + mediaID: %v", err)
ginAbortInternalError(c, responsevalue.CodeInvalidParameterValue, responsevalue.MsgInvalidRequest, nil)
return
}
}
} else {
avatarMedia, err = ctrl.postHdl.GetMediaByID(c, user.User.Avatar)
if err != nil {
logger.Errorf("get media (avatar) error: %v", err)
ginAbortInternalError(c, responsevalue.CodeInvalidParameterValue, responsevalue.MsgInvalidRequest, nil)
return
if len(req.Avatar) != 0 {
avatarMedia, err = ctrl.postHdl.GetMediaByID(c, user.User.Avatar)
if err != nil {
logger.Errorf("get media (avatar) error: %v", err)
ginAbortInternalError(c, responsevalue.CodeInvalidParameterValue, responsevalue.MsgInvalidRequest, nil)
return
}
}
}

if coverURL == req.CoverImage {
if req.CoverImage != coverURL {
coverPostID, coverMediaID, err := ctrl.resolveMediaUrlFn(req.CoverImage)
if err != nil && !errors.Is(err, ErrEmptyMediaURL) {
logger.Errorf("resolve media url (cover image) error: %v", err)
Expand All @@ -81,17 +86,19 @@ func (ctrl UserController) SetUserInfo(c *gin.Context) {
if len(coverPostID)+len(coverMediaID) != 0 {
coverMedia, err = ctrl.postHdl.GetMedia(c, userID.String(), coverPostID, coverMediaID)
if err != nil {
logger.Errorf("get media (cover image) error: %v", err)
logger.Errorf("get media (cover image) postID + mediaID: %v", err)
ginAbortInternalError(c, responsevalue.CodeInvalidParameterValue, responsevalue.MsgInvalidRequest, nil)
return
}
}
} else {
coverMedia, err = ctrl.postHdl.GetMediaByID(c, user.User.CoverImg)
if err != nil {
logger.Errorf("get media (cover image) error: %v", err)
ginAbortInternalError(c, responsevalue.CodeInvalidParameterValue, responsevalue.MsgInvalidRequest, nil)
return
if len(req.CoverImage) != 0 {
coverMedia, err = ctrl.postHdl.GetMediaByID(c, user.User.CoverImg)
if err != nil {
logger.Errorf("get media (cover image) error: %v", err)
ginAbortInternalError(c, responsevalue.CodeInvalidParameterValue, responsevalue.MsgInvalidRequest, nil)
return
}
}
}

Expand Down
24 changes: 18 additions & 6 deletions internal/laclongquan/infrastructure/port/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,26 @@ import (
)

type HTTPServer struct {
cfg *shared.HTTPServerConfig
app application.Application
auth *auth.AuthService
cfg *shared.HTTPServerConfig
app application.Application
auth *auth.AuthService
processedDomain string
}

func NewHTTPServer(cfg *shared.HTTPServerConfig, app application.Application, authSrv *auth.AuthService) *HTTPServer {
return &HTTPServer{

newHTTPServer := &HTTPServer{
cfg: cfg,
app: app,
auth: authSrv,
}
processedDomain, err := newHTTPServer.preProcessURL(cfg.Domain)
if err != nil {
logger.Fatalf("invalid domain %s", cfg.Domain)
}
newHTTPServer.processedDomain = processedDomain

return newHTTPServer
}

func (s *HTTPServer) Start() (booting.Daemon, error) {
Expand Down Expand Up @@ -92,14 +101,17 @@ func (s HTTPServer) resolveMediaURL(url string) (postID, mediaID string, err err

urlComponent := strings.Split(url, "/")
if len(urlComponent) != 5 {
logger.Errorf("invalid media url len %s", url)
return "", "", controller.ErrInvalidMediaURL
}

if urlComponent[0] != s.cfg.Domain {
if urlComponent[0] != s.processedDomain {
logger.Errorf("invalid media url domain %s", url)
return "", "", controller.ErrInvalidMediaURL
}

if urlComponent[1] != "post" || urlComponent[3] != "media" {
logger.Errorf("invalid media url component %s", url)
return "", "", controller.ErrInvalidMediaURL
}

Expand Down Expand Up @@ -132,7 +144,7 @@ func (s HTTPServer) resolveUserMediaURL(url string) (userID, mediaID string, err
return "", "", controller.ErrInvalidMediaURL
}

if urlComponents[0] != s.cfg.Domain {
if urlComponents[0] != s.processedDomain {
return "", "", controller.ErrInvalidMediaURL
}

Expand Down

0 comments on commit 2734d88

Please sign in to comment.