Skip to content

Commit

Permalink
Upd: Add default cover, logo and organisation admin avatar images
Browse files Browse the repository at this point in the history
Signed-off-by: George J Padayatti <[email protected]>
  • Loading branch information
georgepadayatti committed Nov 2, 2023
1 parent d6d56e0 commit a78e129
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ KAFKA_TOPIC_NAME = "example_topic"
CONTAINER_DEFAULT_RUN_FLAGS := \
--rm $(TERM_FLAGS) \
$(EXTRA_RUN_ARGS) \
--env GOOGLE_APPLICATION_CREDENTIALS=/opt/bb-consent/api/kubernetes-config/keyfile.json \
-v "$(CURDIR)":/go/$(PROJECT_PACKAGE) \
-v $(CURDIR)/resources/config/:/opt/bb-consent/api/config/:ro \
-v $(CURDIR)/resources/kubernetes-config/:/opt/bb-consent/api/kubernetes-config/:ro \
-v $(CURDIR)/resources/assets/:/opt/bb-consent/api/assets/:ro \
-w /go/$(PROJECT_PACKAGE)

GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD | sed -E 's/[^a-zA-Z0-9]+/-/g')
Expand Down
146 changes: 146 additions & 0 deletions internal/fixture/fixture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package fixture

import (
"io"
"os"
"strings"

"github.com/bb-consent/api/internal/image"
"github.com/bb-consent/api/internal/org"
"github.com/bb-consent/api/internal/user"
)

const AssetsPath = "/opt/bb-consent/api/assets/"

func loadImageAndReturnBytes(imagePath string) ([]byte, error) {
// Open the JPEG file
file, err := os.Open(imagePath)
if err != nil {
return nil, err
}
defer file.Close()

// Read the file content
data, err := io.ReadAll(file)
if err != nil {
return nil, err
}

return data, nil

}

func saveImageToDb(data []byte) (string, error) {
// Save image to db
imageId, err := image.Add(data)
if err != nil {
return "", err
}

return imageId, nil
}

func loadCoverImageAssets(orgId string) error {
coverImagePath := AssetsPath + "cover.jpeg"

// Convert cover image to bytes
coverImageBytes, err := loadImageAndReturnBytes(coverImagePath)
if err != nil {
return err
}

// Save cover image to db
coverImageId, err := saveImageToDb(coverImageBytes)
if err != nil {
return err
}

// Update cover image for organisation
_, err = org.UpdateCoverImage(orgId, coverImageId)
if err != nil {
return err
}

return nil
}

func loadLogoImageAssets(orgId string) error {
logoImagePath := AssetsPath + "logo.jpeg"

// Convert logo image to bytes
logoImageBytes, err := loadImageAndReturnBytes(logoImagePath)
if err != nil {
return err
}

// Save logo image to db
logoImageId, err := saveImageToDb(logoImageBytes)
if err != nil {
return err
}

// Update logo image for organisation
_, err = org.UpdateLogoImage(orgId, logoImageId)
if err != nil {
return err
}

return nil
}

func LoadOrganisationAdminAvatarImageAssets(u user.User, hostUrl string) (user.User, error) {
// Check if avatar image is present
if len(strings.TrimSpace(u.ImageID)) != 0 {
return u, nil
}

avatarImagePath := AssetsPath + "logo.jpeg"

// Convert avatar image to bytes
avatarImageBytes, err := loadImageAndReturnBytes(avatarImagePath)
if err != nil {
return user.User{}, err
}

// Save avatar image to db
avatarImageId, err := saveImageToDb(avatarImageBytes)
if err != nil {
return user.User{}, err
}

// Update avatar image for organisation
u.ImageURL = "https://" + hostUrl + "/v2/onboard/admin/avatarimage"
u.ImageID = avatarImageId
u, err = user.Update(u.ID.Hex(), u)
if err != nil {
return user.User{}, err
}

return u, nil
}

func LoadImageAssetsForSingleTenantConfiguration() error {
// Get first organisation
o, err := org.GetFirstOrganization()
if err != nil {
return err
}

// Check if cover image is present
if len(strings.TrimSpace(o.CoverImageID)) == 0 {
err = loadCoverImageAssets(o.ID.Hex())
if err != nil {
return err
}
}

// Check if logo image is present
if len(strings.TrimSpace(o.LogoImageID)) == 0 {
err = loadLogoImageAssets(o.ID.Hex())
if err != nil {
return err
}
}

return nil
}
8 changes: 8 additions & 0 deletions internal/handler/v2/onboard/loginadminuser_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/bb-consent/api/internal/actionlog"
"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/fixture"
"github.com/bb-consent/api/internal/iam"
"github.com/bb-consent/api/internal/token"
"github.com/bb-consent/api/internal/user"
Expand Down Expand Up @@ -75,6 +76,13 @@ func LoginAdminUser(w http.ResponseWriter, r *http.Request) {
return
}

// Load default user image
u, err = fixture.LoadOrganisationAdminAvatarImageAssets(u, r.Host)
if err != nil {
common.HandleErrorV2(w, http.StatusUnauthorized, "Failed to load default avatar image for admin", err)
return
}

actionLog := fmt.Sprintf("%v logged in", u.Email)
actionlog.LogOrgSecurityCalls(u.ID.Hex(), u.Email, u.Roles[0].OrgID, actionLog)
lResp := loginResp{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func OnboardUpdateOrganisationAdminAvatar(w http.ResponseWriter, r *http.Request
return
}

imageURL := "https://" + r.Host + "/onboard/admin/avatharimage"
imageURL := "https://" + r.Host + "/onboard/admin/avatarimage"
u.ImageID = imageID
u.ImageURL = imageURL
u, err = user.Update(u.ID.Hex(), u)
Expand Down
7 changes: 7 additions & 0 deletions internal/tenant/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/fixture"
"github.com/bb-consent/api/internal/org"
"github.com/bb-consent/api/internal/orgtype"
"github.com/bb-consent/api/internal/user"
Expand Down Expand Up @@ -138,4 +139,10 @@ func SingleTenantConfiguration(config *config.Configuration) {
// Create organisation
createOrganisation(config, orgType, organisationAdminId)

// Load image assets for organisation
err := fixture.LoadImageAssetsForSingleTenantConfiguration()
if err != nil {
log.Println("Error occured while loading image assets for organisation")
}

}
Binary file added resources/assets/cover.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/assets/logo.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions resources/docker/production/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ FROM frolvlad/alpine-glibc:glibc-2.30
# copy artifacts
COPY dist/linux_amd64/bb-consent-api /app/bin/bb-consent-api
COPY resources/config/config-development.json /app
COPY resources/assets/logo.jpeg /opt/bb-consent/api/assets/
COPY resources/assets/cover.jpeg /opt/bb-consent/api/assets/

# start API
EXPOSE 80
Expand Down

0 comments on commit a78e129

Please sign in to comment.