diff --git a/Makefile b/Makefile index cf4dee0..ccb987f 100644 --- a/Makefile +++ b/Makefile @@ -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') diff --git a/internal/fixture/fixture.go b/internal/fixture/fixture.go new file mode 100644 index 0000000..ca44b1e --- /dev/null +++ b/internal/fixture/fixture.go @@ -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 +} diff --git a/internal/handler/v2/onboard/loginadminuser_handler.go b/internal/handler/v2/onboard/loginadminuser_handler.go index 367563e..ecd4dac 100644 --- a/internal/handler/v2/onboard/loginadminuser_handler.go +++ b/internal/handler/v2/onboard/loginadminuser_handler.go @@ -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" @@ -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{ diff --git a/internal/handler/v2/onboard/onboard_update_org_admin_avathar.go b/internal/handler/v2/onboard/onboard_update_org_admin_avathar.go index 614b2a7..5d6a05f 100644 --- a/internal/handler/v2/onboard/onboard_update_org_admin_avathar.go +++ b/internal/handler/v2/onboard/onboard_update_org_admin_avathar.go @@ -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) diff --git a/internal/tenant/single.go b/internal/tenant/single.go index f2a633e..17b4503 100644 --- a/internal/tenant/single.go +++ b/internal/tenant/single.go @@ -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" @@ -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") + } + } diff --git a/resources/assets/cover.jpeg b/resources/assets/cover.jpeg new file mode 100644 index 0000000..960c9cf Binary files /dev/null and b/resources/assets/cover.jpeg differ diff --git a/resources/assets/logo.jpeg b/resources/assets/logo.jpeg new file mode 100644 index 0000000..c3dbe7a Binary files /dev/null and b/resources/assets/logo.jpeg differ diff --git a/resources/docker/production/Dockerfile b/resources/docker/production/Dockerfile index eae65e3..c6a2369 100644 --- a/resources/docker/production/Dockerfile +++ b/resources/docker/production/Dockerfile @@ -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