3
0

Embed avatar initials font path and relocate it

This commit is contained in:
Mumbi Francis 2023-03-27 14:09:06 +03:00 committed by Mumbi Francis
parent ba8009125e
commit 3707f7edae
9 changed files with 26 additions and 25 deletions

View File

@ -1305,8 +1305,8 @@
###############################################################################
# Avatar initials font file path
# Type: string
# Default: ./auth/assets/public/fonts/poppins/Poppins-Regular.ttf
# AVATAR_INITIALS_FONT_PATH=./auth/assets/public/fonts/poppins/Poppins-Regular.ttf
# Default: fonts/Poppins-Regular.ttf
# AVATAR_INITIALS_FONT_PATH=fonts/Poppins-Regular.ttf
###############################################################################
# Avatar initials background color

View File

@ -14,7 +14,7 @@ attachment: schema.#optionsGroup & {
description: "Avatar image maximum upload size, default value is 1MB"
}
avatar_initials_font_path: {
defaultValue: "./auth/assets/public/fonts/poppins/Poppins-Regular.ttf"
defaultValue: "fonts/Poppins-Regular.ttf"
description: "Avatar initials font file path"
env: "AVATAR_INITIALS_FONT_PATH"
}

Binary file not shown.

View File

@ -1077,7 +1077,7 @@ func Discovery() (o *DiscoveryOpt) {
func Attachment() (o *AttachmentOpt) {
o = &AttachmentOpt{
AvatarMaxFileSize: 1000000,
AvatarInitialsFontPath: "./auth/assets/public/fonts/poppins/Poppins-Regular.ttf",
AvatarInitialsFontPath: "fonts/Poppins-Regular.ttf",
AvatarInitialsBackgroundColor: "#F3F3F3",
AvatarInitialsColor: "#162425",
}

View File

@ -3,10 +3,11 @@ package service
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/cortezaproject/corteza/server/assets"
"github.com/cortezaproject/corteza/server/pkg/actionlog"
intAuth "github.com/cortezaproject/corteza/server/pkg/auth"
"github.com/cortezaproject/corteza/server/pkg/errors"
files "github.com/cortezaproject/corteza/server/pkg/objstore"
"github.com/cortezaproject/corteza/server/pkg/options"
"github.com/cortezaproject/corteza/server/store"
@ -15,12 +16,12 @@ import (
"github.com/edwvee/exiffix"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"go.uber.org/zap"
"golang.org/x/image/font"
"image"
"image/gif"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
@ -40,6 +41,7 @@ type (
ac attachmentAccessController
store store.Storer
opt options.AttachmentOpt
logger *zap.Logger
}
attachmentAccessController interface {
@ -60,13 +62,14 @@ type (
}
)
func Attachment(store files.Store, opt options.AttachmentOpt) *attachment {
func Attachment(store files.Store, opt options.AttachmentOpt, log *zap.Logger) *attachment {
return &attachment{
files: store,
actionlog: DefaultActionlog,
ac: DefaultAccessControl,
store: DefaultStore,
opt: opt,
logger: log.Named("attachment"),
}
}
@ -523,24 +526,24 @@ func (svc attachment) processImage(original io.ReadSeeker, att *types.Attachment
return svc.files.Save(att.PreviewUrl, buf)
}
// processFontsFile validates the file path provided in the AVATAR_INITIALS_FONT_PATH environment variable,
// It checks if the file exists and has the correct file extension, then reads and returns the file content
// processFontsFile checks if the file exists and has the correct file extension,
// It validates the file path provided in the AVATAR_INITIALS_FONT_PATH environment variable,
// then reads and returns the file content
func (svc attachment) processFontsFile() (fontBytes []byte, err error) {
aux, err := filepath.Glob(svc.opt.AvatarInitialsFontPath)
if err != nil {
return nil, err
}
if aux == nil || len(aux) != 1 {
return nil, errors.New("font file not found, please ensure that the correct AVATAR_INITIALS_FONT_PATH is set")
}
ext := strings.ToLower(filepath.Ext(aux[0]))
ext := strings.ToLower(filepath.Ext(svc.opt.AvatarInitialsFontPath))
if ext != ".ttf" {
return nil, errors.New("invalid font file extension, please provide a truetype font (.ttf) file")
svc.logger.Error("invalid font file extension, please provide a truetype font (.ttf) file")
return
}
fontBytes, err = os.ReadFile(aux[0])
assetsFile := assets.Files(svc.logger, "")
fontFile, err := assetsFile.Open(svc.opt.AvatarInitialsFontPath)
if err != nil {
svc.logger.Error(fmt.Sprintf("%s, please ensure that the correct AVATAR_INITIALS_FONT_PATH is set", err.Error()))
return nil, errors.New(errors.KindInvalidData, "the path to the font file for generating avatar initials is incorrect or not set")
}
fontBytes, err = io.ReadAll(fontFile)
if err != nil {
return nil, err
}

View File

@ -205,7 +205,7 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, ws websock
DefaultAuthNotification = AuthNotification(CurrentSettings, DefaultRenderer, c.Auth)
DefaultAuth = Auth(AuthOptions{LimitUsers: c.Limit.SystemUsers})
DefaultAuthClient = AuthClient(DefaultStore, DefaultAccessControl, DefaultActionlog, eventbus.Service(), c.Auth)
DefaultAttachment = Attachment(DefaultObjectStore, c.Attachment)
DefaultAttachment = Attachment(DefaultObjectStore, c.Attachment, DefaultLogger)
DefaultUser = User(UserOptions{LimitUsers: c.Limit.SystemUsers})
DefaultCredentials = Credentials()
DefaultReport = Report(DefaultStore, DefaultAccessControl, DefaultActionlog, eventbus.Service())

View File

@ -55,7 +55,6 @@ func InitTestApp() {
service.DefaultStore = app.Store
eventbus.Set(eventBus)
app.Opt.Attachment.AvatarInitialsFontPath = "../../auth/assets/public/fonts/poppins/Poppins-Regular.ttf"
return nil
})

View File

@ -99,7 +99,6 @@ func InitTestApp() {
sm = request.NewSessionManager(service.DefaultStore, app.Opt.Auth, service.DefaultLogger)
app.Opt.Attachment.AvatarInitialsFontPath = "../../auth/assets/public/fonts/poppins/Poppins-Regular.ttf"
return nil
})

View File

@ -71,7 +71,7 @@ func TestMain(m *testing.M) {
app.Opt.ActionLog.WorkflowFunctionsEnabled = true
defStore = app.Store
eventbus.Set(eventBus)
app.Opt.Attachment.AvatarInitialsFontPath = "../../auth/assets/public/fonts/poppins/Poppins-Regular.ttf"
return nil
})