Open platform to collect and prioritize feedback
enhancement: migrate uploads to blobs (#687)
Guilherme Oenning7ca3c004cb05
diff --git a/app/actions/oauth.go b/app/actions/oauth.goindex bcd77095b..c4124092d 100644--- a/app/actions/oauth.go+++ b/app/actions/oauth.go@@ -17,6 +17,7 @@ type CreateEditOAuthConfig struct {// Initialize the modelfunc (input *CreateEditOAuthConfig) Initialize() interface{} {input.Model = new(models.CreateEditOAuthConfig)+ input.Model.Logo = &models.ImageUpload{}return input.Model}@@ -36,6 +37,7 @@ func (input *CreateEditOAuthConfig) Validate(user *models.User, services *app.Se}input.Model.ID = config.ID+ input.Model.Logo.BlobKey = config.LogoBlobKeyif input.Model.ClientSecret == "" {input.Model.ClientSecret = config.ClientSecret}diff --git a/app/actions/oauth_test.go b/app/actions/oauth_test.goindex 910c4ff2b..83df15ed5 100644--- a/app/actions/oauth_test.go+++ b/app/actions/oauth_test.go@@ -48,6 +48,14 @@ func TestCreateEditOAuthConfig_InvalidInput(t *testing.T) {}}+func TestCreateEditOAuthConfig_Initialize(t *testing.T) {+ RegisterT(t)++ action := &actions.CreateEditOAuthConfig{}+ action.Initialize()+ Expect(action.Model.Logo.BlobKey).Equals("")+}+func TestCreateEditOAuthConfig_AddNew_ValidInput(t *testing.T) {RegisterT(t)@@ -78,33 +86,35 @@ func TestCreateEditOAuthConfig_EditExisting_NewSecret(t *testing.T) {RegisterT(t)services.Tenants.SaveOAuthConfig(&models.CreateEditOAuthConfig{- ID: 4,+ ID: 4,+ Logo: &models.ImageUpload{+ BlobKey: "hello-world.png",+ },Provider: "_NAME",DisplayName: "My Provider",ClientSecret: "MY_OLD_SECRET",})- input := &models.CreateEditOAuthConfig{- Provider: "_NAME",- DisplayName: "My Provider",- Status: models.OAuthConfigDisabled,- ClientID: "823187ahjjfdha8fds7yfdashfjkdsa",- ClientSecret: "jijads78d76cn347768x3t4668q275@ˆ&Tnycasdgsacuyhij",- AuthorizeURL: "http://provider/oauth/authorize",- TokenURL: "http://provider/oauth/token",- Scope: "profile email",- ProfileURL: "http://provider/profile/me",- JSONUserIDPath: "user.id",- JSONUserNamePath: "user.name",- JSONUserEmailPath: "user.email",- }- action := &actions.CreateEditOAuthConfig{- Model: input,- }+ action := &actions.CreateEditOAuthConfig{}+ action.Initialize()+ action.Model.Provider = "_NAME"+ action.Model.DisplayName = "My Provider"+ action.Model.Status = models.OAuthConfigDisabled+ action.Model.ClientID = "823187ahjjfdha8fds7yfdashfjkdsa"+ action.Model.ClientSecret = "jijads78d76cn347768x3t4668q275@ˆ&Tnycasdgsacuyhij"+ action.Model.AuthorizeURL = "http://provider/oauth/authorize"+ action.Model.TokenURL = "http://provider/oauth/token"+ action.Model.Scope = "profile email"+ action.Model.ProfileURL = "http://provider/profile/me"+ action.Model.JSONUserIDPath = "user.id"+ action.Model.JSONUserNamePath = "user.name"+ action.Model.JSONUserEmailPath = "user.email"+result := action.Validate(nil, services)ExpectSuccess(result)- Expect(input.ID).Equals(4)- Expect(input.ClientSecret).Equals("jijads78d76cn347768x3t4668q275@ˆ&Tnycasdgsacuyhij")+ Expect(action.Model.ID).Equals(4)+ Expect(action.Model.Logo.BlobKey).Equals("hello-world.png")+ Expect(action.Model.ClientSecret).Equals("jijads78d76cn347768x3t4668q275@ˆ&Tnycasdgsacuyhij")}func TestCreateEditOAuthConfig_EditExisting_OmitSecret(t *testing.T) {@@ -112,52 +122,48 @@ func TestCreateEditOAuthConfig_EditExisting_OmitSecret(t *testing.T) {services.Tenants.SaveOAuthConfig(&models.CreateEditOAuthConfig{ID: 5,+ Logo: &models.ImageUpload{},Provider: "_NAME2",DisplayName: "My Provider",ClientSecret: "MY_OLD_SECRET",})- input := &models.CreateEditOAuthConfig{- Provider: "_NAME2",- DisplayName: "My Provider",- Status: models.OAuthConfigDisabled,- ClientID: "823187ahjjfdha8fds7yfdashfjkdsa",- AuthorizeURL: "http://provider/oauth/authorize",- TokenURL: "http://provider/oauth/token",- Scope: "profile email",- ProfileURL: "http://provider/profile/me",- JSONUserIDPath: "user.id",- JSONUserNamePath: "user.name",- JSONUserEmailPath: "user.email",- }- action := &actions.CreateEditOAuthConfig{- Model: input,- }+ action := &actions.CreateEditOAuthConfig{}+ action.Initialize()+ action.Model.Provider = "_NAME2"+ action.Model.DisplayName = "My Provider"+ action.Model.Status = models.OAuthConfigDisabled+ action.Model.ClientID = "823187ahjjfdha8fds7yfdashfjkdsa"+ action.Model.AuthorizeURL = "http://provider/oauth/authorize"+ action.Model.TokenURL = "http://provider/oauth/token"+ action.Model.Scope = "profile email"+ action.Model.ProfileURL = "http://provider/profile/me"+ action.Model.JSONUserIDPath = "user.id"+ action.Model.JSONUserNamePath = "user.name"+ action.Model.JSONUserEmailPath = "user.email"+result := action.Validate(nil, services)ExpectSuccess(result)- Expect(input.ID).Equals(5)- Expect(input.ClientSecret).Equals("MY_OLD_SECRET")+ Expect(action.Model.ID).Equals(5)+ Expect(action.Model.ClientSecret).Equals("MY_OLD_SECRET")}func TestCreateEditOAuthConfig_EditNonExisting(t *testing.T) {RegisterT(t)- input := &models.CreateEditOAuthConfig{- Provider: "_MY_NEW_PROVIDER",- DisplayName: "My Provider",- Status: models.OAuthConfigDisabled,- ClientID: "823187ahjjfdha8fds7yfdashfjkdsa",- AuthorizeURL: "http://provider/oauth/authorize",- TokenURL: "http://provider/oauth/token",- Scope: "profile email",- ProfileURL: "http://provider/profile/me",- JSONUserIDPath: "user.id",- JSONUserNamePath: "user.name",- JSONUserEmailPath: "user.email",- }- action := &actions.CreateEditOAuthConfig{- Model: input,- }+ action := &actions.CreateEditOAuthConfig{}+ action.Initialize()+ action.Model.Provider = "_MY_NEW_PROVIDER"+ action.Model.DisplayName = "My Provider"+ action.Model.Status = models.OAuthConfigDisabled+ action.Model.ClientID = "823187ahjjfdha8fds7yfdashfjkdsa"+ action.Model.AuthorizeURL = "http://provider/oauth/authorize"+ action.Model.TokenURL = "http://provider/oauth/token"+ action.Model.Scope = "profile email"+ action.Model.ProfileURL = "http://provider/profile/me"+ action.Model.JSONUserIDPath = "user.id"+ action.Model.JSONUserNamePath = "user.name"+ action.Model.JSONUserEmailPath = "user.email"result := action.Validate(nil, services)Expect(result.Err).Equals(app.ErrNotFound)Expect(result.Ok).IsFalse()diff --git a/app/actions/tenant.go b/app/actions/tenant.goindex ef1b07633..5a31d8d31 100644--- a/app/actions/tenant.go+++ b/app/actions/tenant.go@@ -84,6 +84,7 @@ type UpdateTenantSettings struct {// Initialize the modelfunc (input *UpdateTenantSettings) Initialize() interface{} {input.Model = new(models.UpdateTenantSettings)+ input.Model.Logo = &models.ImageUpload{}return input.Model}@@ -96,6 +97,9 @@ func (input *UpdateTenantSettings) IsAuthorized(user *models.User, services *appfunc (input *UpdateTenantSettings) Validate(user *models.User, services *app.Services) *validate.Result {result := validate.Success()+ if services.Tenants.Current() != nil {+ input.Model.Logo.BlobKey = services.Tenants.Current().LogoBlobKey+ }messages, err := validate.ImageUpload(input.Model.Logo, 200, 200, 100)if err != nil {return validate.Error(err)diff --git a/app/actions/tenant_test.go b/app/actions/tenant_test.goindex f805abf98..194abf456 100644--- a/app/actions/tenant_test.go+++ b/app/actions/tenant_test.go@@ -116,6 +116,7 @@ func TestCreateTenant_EmptySubdomain(t *testing.T) {result := action.Validate(nil, services)ExpectFailed(result, "subdomain")}+func TestUpdateTenantSettings_Unauthorized(t *testing.T) {RegisterT(t)@@ -133,7 +134,8 @@ func TestUpdateTenantSettings_Unauthorized(t *testing.T) {func TestUpdateTenantSettings_EmptyTitle(t *testing.T) {RegisterT(t)- action := actions.UpdateTenantSettings{Model: &models.UpdateTenantSettings{}}+ action := actions.UpdateTenantSettings{}+ action.Initialize()result := action.Validate(nil, services)ExpectFailed(result, "title")}@@ -161,3 +163,19 @@ func TestUpdateTenantSettings_LargeInvitation(t *testing.T) {result := action.Validate(nil, services)ExpectFailed(result, "invitation")}++func TestUpdateTenantSettings_ExistingTenant_WithLogo(t *testing.T) {+ RegisterT(t)+ services.SetCurrentTenant(&models.Tenant{+ ID: 1,+ LogoBlobKey: "hello-world.png",+ })++ action := actions.UpdateTenantSettings{}+ action.Initialize()+ action.Model.Title = "OK"+ action.Model.Invitation = "Share your ideas!"+ result := action.Validate(nil, services)+ ExpectSuccess(result)+ Expect(action.Model.Logo.BlobKey).Equals("hello-world.png")+}diff --git a/app/cmd/routes.go b/app/cmd/routes.goindex a82a0c8d6..8ebfb84e0 100644--- a/app/cmd/routes.go+++ b/app/cmd/routes.go@@ -67,8 +67,8 @@ func routes(r *web.Engine) *web.Engine {tenantAssets.Get("/avatars/:size/:id/:name", handlers.Avatar())tenantAssets.Use(middlewares.ClientCache(30 * 24 * time.Hour))- tenantAssets.Get("/images/:size/:id/favicon", handlers.Favicon())- tenantAssets.Get("/images/:size/:id", handlers.ViewUploadedImage())+ tenantAssets.Get("/favicon/:size/*bkey", handlers.Favicon())+ tenantAssets.Get("/images/:size/*bkey", handlers.ViewUploadedImage())tenantAssets.Get("/custom/:md5.css", func(c web.Context) error {return c.Blob(http.StatusOK, "text/css", []byte(c.Tenant().CustomCSS))})diff --git a/app/handlers/admin.go b/app/handlers/admin.goindex 02c100a82..dbfda1aab 100644--- a/app/handlers/admin.go+++ b/app/handlers/admin.go@@ -2,6 +2,7 @@ package handlersimport ("github.com/getfider/fider/app/actions"+ "github.com/getfider/fider/app/pkg/errors""github.com/getfider/fider/app/pkg/web")@@ -36,7 +37,12 @@ func UpdateSettings() web.HandlerFunc {return c.HandleValidation(result)}- err := c.Services().Tenants.UpdateSettings(input.Model)+ err := handleImageUpload(c, input.Model.Logo)+ if err != nil {+ return errors.Wrap(err, "failed to upload new tenant logo")+ }++ err = c.Services().Tenants.UpdateSettings(input.Model)if err != nil {return c.Failure(err)}@@ -135,7 +141,12 @@ func SaveOAuthConfig() web.HandlerFunc {return c.HandleValidation(result)}- err := c.Services().Tenants.SaveOAuthConfig(input.Model)+ err := handleImageUpload(c, input.Model.Logo)+ if err != nil {+ return errors.Wrap(err, "failed to upload new OAuth logo")+ }++ err = c.Services().Tenants.SaveOAuthConfig(input.Model)if err != nil {return c.Failure(err)}diff --git a/app/handlers/admin_test.go b/app/handlers/admin_test.goindex b277d3cf3..77c54d34a 100644--- a/app/handlers/admin_test.go+++ b/app/handlers/admin_test.go@@ -1,10 +1,13 @@package handlers_testimport (+ "encoding/base64"+ "io/ioutil""net/http""testing". "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/mock""github.com/getfider/fider/app/handlers"@@ -14,6 +17,8 @@ func TestUpdateSettingsHandler(t *testing.T) {RegisterT(t)server, services := mock.NewServer()+ mock.DemoTenant.LogoBlobKey = "logos/hello-world.png"+code, _ := server.OnTenant(mock.DemoTenant).AsUser(mock.JonSnow).@@ -27,6 +32,67 @@ func TestUpdateSettingsHandler(t *testing.T) {Expect(tenant.Name).Equals("GoT")Expect(tenant.Invitation).Equals("Join us!")Expect(tenant.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")+ Expect(tenant.LogoBlobKey).Equals("logos/hello-world.png")+}++func TestUpdateSettingsHandler_NewLogo(t *testing.T) {+ RegisterT(t)++ logoBytes, _ := ioutil.ReadFile(env.Etc("logo.png"))+ logoB64 := base64.StdEncoding.EncodeToString(logoBytes)++ server, services := mock.NewServer()+ code, _ := server.+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ ExecutePost(+ handlers.UpdateSettings(), `{+ "title": "GoT",+ "invitation": "Join us!",+ "welcomeMessage": "Welcome to GoT Feedback Forum",+ "logo": {+ "upload": {+ "fileName": "picture.png",+ "contentType": "image/png",+ "content": "`+logoB64+`"+ }+ }+ }`)++ tenant, _ := services.Tenants.GetByDomain("demo")+ Expect(code).Equals(http.StatusOK)+ Expect(tenant.Name).Equals("GoT")+ Expect(tenant.Invitation).Equals("Join us!")+ Expect(tenant.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")+ Expect(tenant.LogoBlobKey).ContainsSubstring("logos/")+ Expect(tenant.LogoBlobKey).ContainsSubstring("picture.png")+}++func TestUpdateSettingsHandler_RemoveLogo(t *testing.T) {+ RegisterT(t)++ server, services := mock.NewServer()+ mock.DemoTenant.LogoBlobKey = "logos/hello-world.png"++ code, _ := server.+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ ExecutePost(+ handlers.UpdateSettings(), `{+ "title": "GoT",+ "invitation": "Join us!",+ "welcomeMessage": "Welcome to GoT Feedback Forum",+ "logo": {+ "remove": true+ }+ }`)++ tenant, _ := services.Tenants.GetByDomain("demo")+ Expect(code).Equals(http.StatusOK)+ Expect(tenant.Name).Equals("GoT")+ Expect(tenant.Invitation).Equals("Join us!")+ Expect(tenant.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")+ Expect(tenant.LogoBlobKey).Equals("")}func TestUpdatePrivacyHandler(t *testing.T) {diff --git a/app/handlers/common.go b/app/handlers/common.goindex ff472116e..0cb11b646 100644--- a/app/handlers/common.go+++ b/app/handlers/common.go@@ -9,9 +9,11 @@ import ("github.com/getfider/fider/app""github.com/getfider/fider/app/models"+ "github.com/getfider/fider/app/pkg/blob""github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/errors""github.com/getfider/fider/app/pkg/log"+ "github.com/getfider/fider/app/pkg/rand""github.com/getfider/fider/app/pkg/web")@@ -153,3 +155,17 @@ func validateKey(kind models.EmailVerificationKind, c web.Context) (*models.Emaireturn result, nil}++func handleImageUpload(c web.Context, img *models.ImageUpload) error {+ if img.Remove {+ img.BlobKey = ""+ } else if img.Upload != nil && len(img.Upload.Content) > 0 {+ bkey := fmt.Sprintf("logos/%s-%s", rand.String(64), blob.SanitizeFileName(img.Upload.FileName))+ err := c.Services().Blobs.Put(bkey, img.Upload.Content, img.Upload.ContentType)+ if err != nil {+ return err+ }+ img.BlobKey = bkey+ }+ return nil+}diff --git a/app/handlers/images.go b/app/handlers/images.goindex a56af5d58..cae68f2c2 100644--- a/app/handlers/images.go+++ b/app/handlers/images.go@@ -71,16 +71,17 @@ func Favicon() web.HandlerFunc {return func(c web.Context) error {var (bytes []byte+ err errorcontentType string)- id, err := c.ParamAsInt("id")- if err == nil {- logo, err := c.Services().Tenants.GetUpload(id)+ bkey := c.Param("bkey")+ if bkey != "" {+ logo, err := c.Services().Blobs.Get(bkey)if err != nil {return c.Failure(err)}- bytes = logo.Content+ bytes = logo.ObjectcontentType = logo.ContentType} else {bytes, err = ioutil.ReadFile(env.Path("favicon.png"))@@ -114,22 +115,19 @@ func Favicon() web.HandlerFunc {//ViewUploadedImage returns any uploaded image by given ID and sizefunc ViewUploadedImage() web.HandlerFunc {return func(c web.Context) error {- id, err := c.ParamAsInt("id")- if err != nil {- return c.NotFound()- }+ bkey := c.Param("bkey")size, err := c.ParamAsInt("size")if err != nil {return c.NotFound()}- logo, err := c.Services().Tenants.GetUpload(id)+ logo, err := c.Services().Blobs.Get(bkey)if err != nil {return c.Failure(err)}- bytes, err := img.Resize(logo.Content, size, 0)+ bytes, err := img.Resize(logo.Object, size, 0)if err != nil {return c.Failure(err)}diff --git a/app/models/identity.go b/app/models/identity.goindex 0847cc47d..5905a6a97 100644--- a/app/models/identity.go+++ b/app/models/identity.go@@ -18,7 +18,7 @@ type Tenant struct {CNAME string `json:"cname"`Status int `json:"-"`IsPrivate bool `json:"isPrivate"`- LogoID int `json:"logoID"`+ LogoBlobKey string `json:"logoBlobKey"`CustomCSS string `json:"-"`}@@ -230,12 +230,14 @@ type UpdateTenantAdvancedSettings struct {//ImageUpload is the input model used to upload/remove an imagetype ImageUpload struct {- Upload *ImageUploadData `json:"upload"`- Remove bool `json:"remove"`+ BlobKey string+ Upload *ImageUploadData `json:"upload"`+ Remove bool `json:"remove"`}-//UpdateTenantSettingsLogoUpload is the input model used to upload a new logo+//ImageUploadData is the input model used to upload a new logotype ImageUploadData struct {+ FileName string `json:"fileName"`ContentType string `json:"contentType"`Content []byte `json:"content"`}diff --git a/app/models/infra.go b/app/models/infra.goindex e47a4f100..52d8a5e53 100644--- a/app/models/infra.go+++ b/app/models/infra.go@@ -49,7 +49,7 @@ type OAuthConfig struct {ID intProvider stringDisplayName string- LogoID int+ LogoBlobKey stringStatus intClientID stringClientSecret string@@ -72,7 +72,7 @@ func (o OAuthConfig) MarshalJSON() ([]byte, error) {"id": o.ID,"provider": o.Provider,"displayName": o.DisplayName,- "logoID": o.LogoID,+ "logoBlobKey": o.LogoBlobKey,"status": o.Status,"clientID": o.ClientID,"clientSecret": secret,diff --git a/app/pkg/blob/blob.go b/app/pkg/blob/blob.goindex 0cb177007..7532f9705 100644--- a/app/pkg/blob/blob.go+++ b/app/pkg/blob/blob.go@@ -2,9 +2,11 @@ package blobimport ("errors"+ "path/filepath""strings""github.com/getfider/fider/app/models"+ "github.com/gosimple/slug")// Blob is a file persisted somewhere@@ -21,6 +23,16 @@ var ErrNotFound = errors.New("Blob not found")// ErrInvalidKeyFormat is returned when blob key is in invalid formatvar ErrInvalidKeyFormat = errors.New("Blob key is in invalid format")+// SanitizeFileName replaces invalid characters from given filename+func SanitizeFileName(fileName string) string {+ fileName = strings.TrimSpace(fileName)+ ext := filepath.Ext(fileName)+ if ext != "" {+ return slug.Make(fileName[0:len(fileName)-len(ext)]) + ext+ }+ return slug.Make(fileName)+}+// ValidateKey checks if key is is valid formatfunc ValidateKey(key string) error {if len(key) == 0 || len(key) > 512 || strings.Contains(key, " ") {diff --git a/app/pkg/blob/blob_test.go b/app/pkg/blob/blob_test.goindex 412aaab2f..1342d52a9 100644--- a/app/pkg/blob/blob_test.go+++ b/app/pkg/blob/blob_test.go@@ -243,3 +243,14 @@ func KeyFormats(client blob.Storage, t *testing.T) {}}}++func TestSanitizeFileName(t *testing.T) {+ RegisterT(t)++ Expect(blob.SanitizeFileName("João.txt")).Equals("joao.txt")+ Expect(blob.SanitizeFileName(" Jon")).Equals("jon")+ Expect(blob.SanitizeFileName("Jon.png")).Equals("jon.png")+ Expect(blob.SanitizeFileName("Jon Snow.png ")).Equals("jon-snow.png")+ Expect(blob.SanitizeFileName(" ヒキワリ.png")).Equals("hikiwari.png")+ Expect(blob.SanitizeFileName("люди рождаются свободными.png")).Equals("liudi-rozhdaiutsia-svobodnymi.png")+}diff --git a/app/pkg/dbx/dbx_test.go b/app/pkg/dbx/dbx_test.goindex c20938cbb..41af16b91 100644--- a/app/pkg/dbx/dbx_test.go+++ b/app/pkg/dbx/dbx_test.go@@ -295,14 +295,14 @@ func TestByteArray(t *testing.T) {Expect(err).IsNil()_, err = trx.Execute(`- INSERT INTO uploads (tenant_id, size, content_type, file)- VALUES (1, $1, 'text/plain', $2)+ INSERT INTO blobs (key, tenant_id, size, content_type, file)+ VALUES ('favicon.png', 1, $1, 'text/plain', $2)`, len(fileContent), fileContent)Expect(err).IsNil()theFile := file{}- err = trx.Get(&theFile, "SELECT content_type, size, file FROM uploads WHERE id = 1")+ err = trx.Get(&theFile, "SELECT content_type, size, file FROM blobs WHERE key = 'favicon.png' AND tenant_id = 1")Expect(err).IsNil()Expect(theFile.ContentType).Equals("text/plain")Expect(theFile.Content).Equals(fileContent)diff --git a/app/pkg/dbx/setup.sql b/app/pkg/dbx/setup.sqlindex ae7442253..ffa46bc71 100644--- a/app/pkg/dbx/setup.sql+++ b/app/pkg/dbx/setup.sql@@ -2,8 +2,8 @@ TRUNCATE TABLE blobs RESTART IDENTITY CASCADE;TRUNCATE TABLE logs RESTART IDENTITY CASCADE;TRUNCATE TABLE tenants RESTART IDENTITY CASCADE;-INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css)-VALUES ('Demonstration', 'demo', now(), '', '', '', 1, false, '');+INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css, logo_bkey)+VALUES ('Demonstration', 'demo', now(), '', '', '', 1, false, '', '');INSERT INTO users (name, email, tenant_id, created_at, role, status)VALUES ('Jon Snow', '[email protected]', 1, now(), 3, 1);@@ -18,8 +18,8 @@ VALUES (2, 1, 'google', 'GO5678', now());INSERT INTO users (name, email, tenant_id, created_at, role, status)VALUES ('Sansa Stark', '[email protected]', 1, now(), 1, 1);-INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css)-VALUES ('Avengers', 'avengers', now(), 'feedback.avengers.com', '', '', 1, false, '');+INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css, logo_bkey)+VALUES ('Avengers', 'avengers', now(), 'feedback.avengers.com', '', '', 1, false, '', '');INSERT INTO users (name, email, tenant_id, created_at, role, status)VALUES ('Tony Stark', '[email protected]', 2, now(), 3, 1);diff --git a/app/pkg/oauth/oauth.go b/app/pkg/oauth/oauth.goindex 84a430917..bdee4a6e8 100644--- a/app/pkg/oauth/oauth.go+++ b/app/pkg/oauth/oauth.go@@ -30,7 +30,7 @@ type ProviderOption struct {ClientID string `json:"clientID"`URL string `json:"url"`CallbackURL string `json:"callbackURL"`- LogoID int `json:"logoID"`+ LogoBlobKey string `json:"logoBlobKey"`IsCustomProvider bool `json:"isCustomProvider"`IsEnabled bool `json:"isEnabled"`}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex 0753be738..4a0e3fb88 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -14,6 +14,7 @@ import ("github.com/getfider/fider/app""github.com/getfider/fider/app/actions""github.com/getfider/fider/app/models"+ "github.com/getfider/fider/app/pkg/blob""github.com/getfider/fider/app/pkg/dbx""github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/errors"@@ -241,7 +242,8 @@ func (ctx *Context) Gone() error {//Failure returns a 500 pagefunc (ctx *Context) Failure(err error) error {err = errors.StackN(err, 1)- if errors.Cause(err) == app.ErrNotFound {+ cause := errors.Cause(err)+ if cause == app.ErrNotFound || cause == blob.ErrNotFound {return ctx.NotFound()}@@ -445,7 +447,7 @@ func (ctx *Context) Param(name string) string {if ctx.params == nil {return ""}- return ctx.params[name]+ return strings.TrimPrefix(ctx.params[name], "/")}//ParamAsInt returns parameter as int@@ -546,16 +548,16 @@ func (ctx *Context) TenantAssetsURL(path string, a ...interface{}) string {// LogoURL return the full URL to the tenant-specific logo URLfunc (ctx *Context) LogoURL() string {- if ctx.Tenant() != nil && ctx.Tenant().LogoID > 0 {- return ctx.TenantAssetsURL("/images/200/%d", ctx.Tenant().LogoID)+ if ctx.Tenant() != nil && ctx.Tenant().LogoBlobKey != "" {+ return ctx.TenantAssetsURL("/images/200/%s", ctx.Tenant().LogoBlobKey)}return "https://getfider.com/images/logo-100x100.png"}// FaviconURL return the full URL to the tenant-specific favicon URLfunc (ctx *Context) FaviconURL() string {- if ctx.Tenant() != nil && ctx.Tenant().LogoID > 0 {- return ctx.TenantAssetsURL("/images/:size/%d/favicon", ctx.Tenant().LogoID)+ if ctx.Tenant() != nil && ctx.Tenant().LogoBlobKey != "" {+ return ctx.TenantAssetsURL("/favicon/:size/%s", ctx.Tenant().LogoBlobKey)}return ctx.GlobalAssetsURL("/favicon/:size")}diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.goindex a51ca91ec..c3d0b5644 100644--- a/app/pkg/web/engine.go+++ b/app/pkg/web/engine.go@@ -6,6 +6,7 @@ import (stdLog "log""net/http""os"+ "path""runtime""strconv""time"@@ -306,10 +307,10 @@ func (g *Group) Static(prefix, root string) {var h HandlerFuncif fi.IsDir() {h = func(c Context) error {- path := root + c.Param("filepath")- fi, err := os.Stat(path)+ filePath := path.Join(root, c.Param("filepath"))+ fi, err := os.Stat(filePath)if err == nil && !fi.IsDir() {- http.ServeFile(c.Response, c.Request.instance, path)+ http.ServeFile(c.Response, c.Request.instance, filePath)return nil}return c.NotFound()diff --git a/app/pkg/web/oauth.go b/app/pkg/web/oauth.goindex 4c289eb21..bd1666ace 100644--- a/app/pkg/web/oauth.go+++ b/app/pkg/web/oauth.go@@ -245,7 +245,7 @@ func (s *OAuthService) ListAllProviders() ([]*oauth.ProviderOption, error) {URL: fmt.Sprintf("/oauth/%s", p.Provider),CallbackURL: fmt.Sprintf("%s/oauth/%s/callback", s.oauthBaseURL, p.Provider),IsCustomProvider: string(p.Provider[0]) == "_",- LogoID: p.LogoID,+ LogoBlobKey: p.LogoBlobKey,IsEnabled: p.Status == models.OAuthConfigEnabled,})}diff --git a/app/pkg/web/testdata/tenant.html b/app/pkg/web/testdata/tenant.htmlindex 1902cf87c..158913ad9 100755--- a/app/pkg/web/testdata/tenant.html+++ b/app/pkg/web/testdata/tenant.html@@ -40,7 +40,7 @@<script id="server-data" type="application/json">- {"__assets":{"CSS":["/assets/css/file1.css"],"JS":["/assets/js/file1.js","/assets/js/file2.js"]},"__contextID":"CONTEXT_ID","__currentURL":"https://demo.test.fider.io:3000/","__favicon":"https://demo.test.fider.io:3000/favicon/:size","__logo":"https://getfider.com/images/logo-100x100.png","__props":null,"__settings":{"baseURL":"https://demo.test.fider.io:3000","buildTime":"","compiler":"","domain":"","environment":"","globalAssetsURL":"https://demo.test.fider.io:3000","googleAnalytics":"","hasLegal":false,"mode":"","oauth":[],"tenantAssetsURL":"https://demo.test.fider.io:3000","version":""},"__tenant":{"id":0,"name":"Game of Thrones","subdomain":"","invitation":"","welcomeMessage":"","cname":"","isPrivate":false,"logoID":0},"__title":"Game of Thrones"}+ {"__assets":{"CSS":["/assets/css/file1.css"],"JS":["/assets/js/file1.js","/assets/js/file2.js"]},"__contextID":"CONTEXT_ID","__currentURL":"https://demo.test.fider.io:3000/","__favicon":"https://demo.test.fider.io:3000/favicon/:size","__logo":"https://getfider.com/images/logo-100x100.png","__props":null,"__settings":{"baseURL":"https://demo.test.fider.io:3000","buildTime":"","compiler":"","domain":"","environment":"","globalAssetsURL":"https://demo.test.fider.io:3000","googleAnalytics":"","hasLegal":false,"mode":"","oauth":[],"tenantAssetsURL":"https://demo.test.fider.io:3000","version":""},"__tenant":{"id":0,"name":"Game of Thrones","subdomain":"","invitation":"","welcomeMessage":"","cname":"","isPrivate":false,"logoBlobKey":""},"__title":"Game of Thrones"}</script><script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,fetch" crossorigin="anonymous"></script>diff --git a/app/storage/inmemory/tenant.go b/app/storage/inmemory/tenant.goindex 6a52ac727..366c02069 100644--- a/app/storage/inmemory/tenant.go+++ b/app/storage/inmemory/tenant.go@@ -1,7 +1,6 @@package inmemoryimport (- "net/http""time""github.com/getfider/fider/app"@@ -12,12 +11,10 @@ import (// TenantStorage contains read and write operations for tenantstype TenantStorage struct {lastID int- lastLogoID inttenants []*models.Tenantcurrent *models.Tenantuser *models.Userverifications []*models.EmailVerification- tenantLogos map[int]*models.UploadoauthConfigs []*models.OAuthConfig}@@ -26,7 +23,6 @@ func NewTenantStorage() *TenantStorage {return &TenantStorage{tenants: make([]*models.Tenant, 0),verifications: make([]*models.EmailVerification, 0),- tenantLogos: make(map[int]*models.Upload, 0),oauthConfigs: make([]*models.OAuthConfig, 0),}}@@ -41,6 +37,11 @@ func (s *TenantStorage) SetCurrentUser(user *models.User) {s.user = user}+// Current returns current context tenant+func (s *TenantStorage) Current() *models.Tenant {+ return s.current+}+// Add given tenant to tenant listfunc (s *TenantStorage) Add(name string, subdomain string, status int) (*models.Tenant, error) {s.lastID = s.lastID + 1@@ -93,24 +94,11 @@ func (s *TenantStorage) IsCNAMEAvailable(cname string) (bool, error) {func (s *TenantStorage) UpdateSettings(settings *models.UpdateTenantSettings) error {for _, tenant := range s.tenants {if tenant.ID == s.current.ID {-- if settings.Logo != nil && settings.Logo.Upload != nil && len(settings.Logo.Upload.Content) > 0 {- s.lastLogoID = s.lastLogoID + 1- if s.tenantLogos == nil {- s.tenantLogos = make(map[int]*models.Upload, 0)- }- tenant.LogoID = s.lastLogoID- s.tenantLogos[s.lastLogoID] = &models.Upload{- Content: settings.Logo.Upload.Content,- Size: len(settings.Logo.Upload.Content),- ContentType: http.DetectContentType(settings.Logo.Upload.Content),- }- }-tenant.Invitation = settings.Invitationtenant.WelcomeMessage = settings.WelcomeMessagetenant.Name = settings.Titletenant.CNAME = settings.CNAME+ tenant.LogoBlobKey = settings.Logo.BlobKeyreturn nil}}@@ -190,20 +178,12 @@ func (s *TenantStorage) SetKeyAsVerified(key string) error {return nil}-// GetUpload returns upload by id-func (s *TenantStorage) GetUpload(id int) (*models.Upload, error) {- if s.tenantLogos != nil {- logo, ok := s.tenantLogos[id]- if !ok {- return nil, app.ErrNotFound- }- return logo, nil- }- return nil, app.ErrNotFound-}-// SaveOAuthConfig saves given config into databasefunc (s *TenantStorage) SaveOAuthConfig(config *models.CreateEditOAuthConfig) error {+ bkey := ""+ if config.Logo != nil {+ bkey = config.Logo.BlobKey+ }for _, c := range s.oauthConfigs {if c.ID == config.ID {c.Provider = config.Provider@@ -217,6 +197,7 @@ func (s *TenantStorage) SaveOAuthConfig(config *models.CreateEditOAuthConfig) erc.JSONUserIDPath = config.JSONUserIDPathc.JSONUserNamePath = config.JSONUserNamePathc.JSONUserEmailPath = config.JSONUserEmailPath+ c.LogoBlobKey = bkeyreturn nil}}@@ -224,6 +205,7 @@ func (s *TenantStorage) SaveOAuthConfig(config *models.CreateEditOAuthConfig) erID: config.ID,Provider: config.Provider,DisplayName: config.DisplayName,+ LogoBlobKey: bkey,ClientID: config.ClientID,ClientSecret: config.ClientSecret,AuthorizeURL: config.AuthorizeURL,diff --git a/app/storage/postgres/tenant.go b/app/storage/postgres/tenant.goindex 599cfeb35..5cc335b23 100644--- a/app/storage/postgres/tenant.go+++ b/app/storage/postgres/tenant.go@@ -1,8 +1,6 @@package postgresimport (- "database/sql"- "net/http""time""github.com/getfider/fider/app"@@ -13,16 +11,16 @@ import ()type dbTenant struct {- ID int `db:"id"`- Name string `db:"name"`- Subdomain string `db:"subdomain"`- CNAME string `db:"cname"`- Invitation string `db:"invitation"`- WelcomeMessage string `db:"welcome_message"`- Status int `db:"status"`- IsPrivate bool `db:"is_private"`- LogoID dbx.NullInt `db:"logo_id"`- CustomCSS string `db:"custom_css"`+ ID int `db:"id"`+ Name string `db:"name"`+ Subdomain string `db:"subdomain"`+ CNAME string `db:"cname"`+ Invitation string `db:"invitation"`+ WelcomeMessage string `db:"welcome_message"`+ Status int `db:"status"`+ IsPrivate bool `db:"is_private"`+ LogoBlobKey string `db:"logo_bkey"`+ CustomCSS string `db:"custom_css"`}func (t *dbTenant) toModel() *models.Tenant {@@ -30,7 +28,7 @@ func (t *dbTenant) toModel() *models.Tenant {return nil}- tenant := &models.Tenant{+ return &models.Tenant{ID: t.ID,Name: t.Name,Subdomain: t.Subdomain,@@ -39,14 +37,9 @@ func (t *dbTenant) toModel() *models.Tenant {WelcomeMessage: t.WelcomeMessage,Status: t.Status,IsPrivate: t.IsPrivate,+ LogoBlobKey: t.LogoBlobKey,CustomCSS: t.CustomCSS,}-- if t.LogoID.Valid {- tenant.LogoID = int(t.LogoID.Int64)- }-- return tenant}type dbEmailVerification struct {@@ -84,20 +77,20 @@ func (t *dbEmailVerification) toModel() *models.EmailVerification {}type dbOAuthConfig struct {- ID int `db:"id"`- Provider string `db:"provider"`- DisplayName string `db:"display_name"`- LogoID dbx.NullInt `db:"logo_id"`- Status int `db:"status"`- ClientID string `db:"client_id"`- ClientSecret string `db:"client_secret"`- AuthorizeURL string `db:"authorize_url"`- TokenURL string `db:"token_url"`- Scope string `db:"scope"`- ProfileURL string `db:"profile_url"`- JSONUserIDPath string `db:"json_user_id_path"`- JSONUserNamePath string `db:"json_user_name_path"`- JSONUserEmailPath string `db:"json_user_email_path"`+ ID int `db:"id"`+ Provider string `db:"provider"`+ DisplayName string `db:"display_name"`+ LogoBlobKey string `db:"logo_bkey"`+ Status int `db:"status"`+ ClientID string `db:"client_id"`+ ClientSecret string `db:"client_secret"`+ AuthorizeURL string `db:"authorize_url"`+ TokenURL string `db:"token_url"`+ Scope string `db:"scope"`+ ProfileURL string `db:"profile_url"`+ JSONUserIDPath string `db:"json_user_id_path"`+ JSONUserNamePath string `db:"json_user_name_path"`+ JSONUserEmailPath string `db:"json_user_email_path"`}func (m *dbOAuthConfig) toModel() *models.OAuthConfig {@@ -106,7 +99,7 @@ func (m *dbOAuthConfig) toModel() *models.OAuthConfig {Provider: m.Provider,DisplayName: m.DisplayName,Status: m.Status,- LogoID: int(m.LogoID.Int64),+ LogoBlobKey: m.LogoBlobKey,ClientID: m.ClientID,ClientSecret: m.ClientSecret,AuthorizeURL: m.AuthorizeURL,@@ -141,12 +134,17 @@ func (s *TenantStorage) SetCurrentUser(user *models.User) {s.user = user}+// Current returns current context tenant+func (s *TenantStorage) Current() *models.Tenant {+ return s.current+}+// Add given tenant to tenant listfunc (s *TenantStorage) Add(name string, subdomain string, status int) (*models.Tenant, error) {var id interr := s.trx.Get(&id,- `INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css)- VALUES ($1, $2, $3, '', '', '', $4, false, '')+ `INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css, logo_bkey)+ VALUES ($1, $2, $3, '', '', '', $4, false, '', '')RETURNING id`, name, subdomain, time.Now(), status)if err != nil {return nil, err@@ -159,7 +157,7 @@ func (s *TenantStorage) Add(name string, subdomain string, status int) (*models.func (s *TenantStorage) First() (*models.Tenant, error) {tenant := dbTenant{}- err := s.trx.Get(&tenant, "SELECT id, name, subdomain, cname, invitation, welcome_message, status, is_private, logo_id, custom_css FROM tenants ORDER BY id LIMIT 1")+ err := s.trx.Get(&tenant, "SELECT id, name, subdomain, cname, invitation, welcome_message, status, is_private, logo_bkey, custom_css FROM tenants ORDER BY id LIMIT 1")if err != nil {return nil, errors.Wrap(err, "failed to get first tenant")}@@ -171,7 +169,7 @@ func (s *TenantStorage) First() (*models.Tenant, error) {func (s *TenantStorage) GetByDomain(domain string) (*models.Tenant, error) {tenant := dbTenant{}- err := s.trx.Get(&tenant, "SELECT id, name, subdomain, cname, invitation, welcome_message, status, is_private, logo_id, custom_css FROM tenants WHERE subdomain = $1 OR subdomain = $2 OR cname = $3 ORDER BY cname DESC", env.Subdomain(domain), domain, domain)+ err := s.trx.Get(&tenant, "SELECT id, name, subdomain, cname, invitation, welcome_message, status, is_private, logo_bkey, custom_css FROM tenants WHERE subdomain = $1 OR subdomain = $2 OR cname = $3 ORDER BY cname DESC", env.Subdomain(domain), domain, domain)if err != nil {return nil, errors.Wrap(err, "failed to get tenant with domain '%s'", domain)}@@ -181,8 +179,13 @@ func (s *TenantStorage) GetByDomain(domain string) (*models.Tenant, error) {// UpdateSettings of current tenantfunc (s *TenantStorage) UpdateSettings(settings *models.UpdateTenantSettings) error {- query := "UPDATE tenants SET name = $1, invitation = $2, welcome_message = $3, cname = $4 WHERE id = $5"- _, err := s.trx.Execute(query, settings.Title, settings.Invitation, settings.WelcomeMessage, settings.CNAME, s.current.ID)+ bkey := ""+ if settings.Logo != nil {+ bkey = settings.Logo.BlobKey+ }++ query := "UPDATE tenants SET name = $1, invitation = $2, welcome_message = $3, cname = $4, logo_bkey = $5 WHERE id = $6"+ _, err := s.trx.Execute(query, settings.Title, settings.Invitation, settings.WelcomeMessage, settings.CNAME, bkey, s.current.ID)if err != nil {return errors.Wrap(err, "failed update tenant settings")}@@ -192,47 +195,9 @@ func (s *TenantStorage) UpdateSettings(settings *models.UpdateTenantSettings) ers.current.CNAME = settings.CNAMEs.current.WelcomeMessage = settings.WelcomeMessage- // Only update tenant logo if there's a change- if settings.Logo != nil {- var newLogoID sql.NullInt64-- // If there's an upload, save it- if !settings.Logo.Remove && len(settings.Logo.Upload.Content) > 0 {- uploadID, err := s.SaveNewUpload(settings.Logo.Upload.Content)- if err != nil {- return errors.Wrap(err, "failed to upload new tenant logo")- }- newLogoID.Scan(uploadID)- }-- // Update current tenant logo to either new ID or null- query := "UPDATE tenants SET logo_id = $1 WHERE id = $2"- _, err = s.trx.Execute(query, newLogoID, s.current.ID)- if err != nil {- return errors.Wrap(err, "failed update tenant logo")- }-- // Update reference to new logo ID- s.current.LogoID = int(newLogoID.Int64)- }-return nil}-// SaveNewUpload saves given content and return upload id-func (s *TenantStorage) SaveNewUpload(content []byte) (int, error) {- var newID int- err := s.trx.Get(&newID, `- INSERT INTO uploads (tenant_id, size, content_type, file, created_at)- VALUES ($1, $2, $3, $4, $5) RETURNING id- `, s.current.ID, len(content), http.DetectContentType(content), content, time.Now(),- )- if err != nil {- return 0, errors.Wrap(err, "failed to save new upload")- }- return newID, nil-}-// UpdateAdvancedSettings of current tenantfunc (s *TenantStorage) UpdateAdvancedSettings(settings *models.UpdateTenantAdvancedSettings) error {query := "UPDATE tenants SET custom_css = $1 WHERE id = $2"@@ -320,23 +285,6 @@ func (s *TenantStorage) SetKeyAsVerified(key string) error {return nil}-// GetUpload returns upload by id-func (s *TenantStorage) GetUpload(id int) (*models.Upload, error) {- upload := &models.Upload{}- err := s.trx.Get(upload, `- SELECT content_type, size, file- FROM uploads- WHERE tenant_id = $1 AND id = $2- `, s.current.ID, id)- if err == app.ErrNotFound {- return nil, app.ErrNotFound- }- if err != nil {- return nil, errors.Wrap(err, "failed to get upload from tenant")- }- return upload, nil-}-// SaveOAuthConfig saves given config into databasefunc (s *TenantStorage) SaveOAuthConfig(config *models.CreateEditOAuthConfig) error {var err error@@ -346,55 +294,35 @@ func (s *TenantStorage) SaveOAuthConfig(config *models.CreateEditOAuthConfig) ertenant_id, provider, display_name, status,client_id, client_secret, authorize_url,profile_url, token_url, scope, json_user_id_path,- json_user_name_path, json_user_email_path- ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)+ json_user_name_path, json_user_email_path, logo_bkey+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)RETURNING id`err = s.trx.Get(&config.ID, query, s.current.ID, config.Provider,config.DisplayName, config.Status, config.ClientID, config.ClientSecret,config.AuthorizeURL, config.ProfileURL, config.TokenURL,config.Scope, config.JSONUserIDPath, config.JSONUserNamePath,- config.JSONUserEmailPath)+ config.JSONUserEmailPath, config.Logo.BlobKey)} else {query := `UPDATE oauth_providersSET display_name = $3, status = $4, client_id = $5, client_secret = $6,authorize_url = $7, profile_url = $8, token_url = $9, scope = $10,- json_user_id_path = $11, json_user_name_path = $12, json_user_email_path = $13+ json_user_id_path = $11, json_user_name_path = $12, json_user_email_path = $13,+ logo_bkey = $14WHERE tenant_id = $1 AND id = $2`_, err = s.trx.Execute(query, s.current.ID, config.ID,config.DisplayName, config.Status, config.ClientID, config.ClientSecret,config.AuthorizeURL, config.ProfileURL, config.TokenURL,config.Scope, config.JSONUserIDPath, config.JSONUserNamePath,- config.JSONUserEmailPath)+ config.JSONUserEmailPath, config.Logo.BlobKey)}if err != nil {return errors.Wrap(err, "failed to save OAuth Provider")}- // Only update OAuth logo if there's a change- if config.Logo != nil {- var newLogoID sql.NullInt64-- // If there's an upload, save it- if !config.Logo.Remove && len(config.Logo.Upload.Content) > 0 {- uploadID, err := s.SaveNewUpload(config.Logo.Upload.Content)- if err != nil {- return errors.Wrap(err, "failed to upload new OAuth logo")- }- newLogoID.Scan(uploadID)- }-- // Update current OAuth logo to either new ID or null- query := "UPDATE oauth_providers SET logo_id = $1 WHERE tenant_id = $2 and id = $3"- _, err = s.trx.Execute(query, newLogoID, s.current.ID, config.ID)- if err != nil {- return errors.Wrap(err, "failed update OAuth logo")- }- }-return nil}@@ -406,7 +334,7 @@ func (s *TenantStorage) GetOAuthConfigByProvider(provider string) (*models.OAuthconfig := &dbOAuthConfig{}err := s.trx.Get(config, `- SELECT id, provider, display_name, status, logo_id,+ SELECT id, provider, display_name, status, logo_bkey,client_id, client_secret, authorize_url,profile_url, token_url, scope, json_user_id_path,json_user_name_path, json_user_email_path@@ -424,7 +352,7 @@ func (s *TenantStorage) ListOAuthConfig() ([]*models.OAuthConfig, error) {configs := []*dbOAuthConfig{}if s.current != nil {err := s.trx.Select(&configs, `- SELECT id, provider, display_name, status, logo_id,+ SELECT id, provider, display_name, status, logo_bkey,client_id, client_secret, authorize_url,profile_url, token_url, scope, json_user_id_path,json_user_name_path, json_user_email_pathdiff --git a/app/storage/postgres/tenant_test.go b/app/storage/postgres/tenant_test.goindex 39862a102..d9484e1c8 100644--- a/app/storage/postgres/tenant_test.go+++ b/app/storage/postgres/tenant_test.go@@ -1,7 +1,6 @@package postgres_testimport (- "io/ioutil""testing""time"@@ -157,6 +156,9 @@ func TestTenantStorage_UpdateSettings(t *testing.T) {tenants.SetCurrentTenant(tenant)settings := &models.UpdateTenantSettings{+ Logo: &models.ImageUpload{+ BlobKey: "some-logo-key.png",+ },Title: "New Demonstration",Invitation: "Leave us your suggestion",WelcomeMessage: "Welcome!",@@ -172,92 +174,7 @@ func TestTenantStorage_UpdateSettings(t *testing.T) {Expect(tenant.Invitation).Equals("Leave us your suggestion")Expect(tenant.WelcomeMessage).Equals("Welcome!")Expect(tenant.CNAME).Equals("demo.company.com")- Expect(tenant.LogoID).Equals(0)-}--func TestTenantStorage_UpdateSettings_WithLogo(t *testing.T) {- SetupDatabaseTest(t)- defer TeardownDatabaseTest()-- tenant, _ := tenants.GetByDomain("demo")- tenants.SetCurrentTenant(tenant)-- logo, _ := ioutil.ReadFile(env.Path("./favicon.png"))-- settings := &models.UpdateTenantSettings{- Logo: &models.ImageUpload{- Upload: &models.ImageUploadData{- Content: logo,- },- },- Title: "New Demonstration",- Invitation: "Leave us your suggestion",- WelcomeMessage: "Welcome!",- CNAME: "demo.company.com",- }- err := tenants.UpdateSettings(settings)- Expect(err).IsNil()-- upload, err := tenants.GetUpload(tenant.LogoID)- Expect(err).IsNil()- Expect(upload.Content).Equals(logo)- Expect(upload.Size).Equals(len(logo))- Expect(upload.ContentType).Equals("image/png")-- //Remove Logo- settings.Logo.Upload = nil- settings.Logo.Remove = true- err = tenants.UpdateSettings(settings)- Expect(err).IsNil()-- tenant, err = tenants.GetByDomain("demo")- Expect(err).IsNil()- Expect(tenant.LogoID).Equals(0)-}--func TestTenantStorage_UpdateSettings_ReplaceLogo(t *testing.T) {- SetupDatabaseTest(t)- defer TeardownDatabaseTest()-- tenant, _ := tenants.GetByDomain("demo")- tenants.SetCurrentTenant(tenant)-- logo, _ := ioutil.ReadFile(env.Path("./favicon.png"))-- settings := &models.UpdateTenantSettings{- Logo: &models.ImageUpload{- Upload: &models.ImageUploadData{- Content: logo,- },- },- Title: "New Demonstration",- Invitation: "Leave us your suggestion",- WelcomeMessage: "Welcome!",- CNAME: "demo.company.com",- }- err := tenants.UpdateSettings(settings)- Expect(err).IsNil()-- firstLogoID := tenant.LogoID- upload, err := tenants.GetUpload(firstLogoID)- Expect(err).IsNil()- Expect(upload.Content).Equals(logo)- Expect(upload.Size).Equals(len(logo))- Expect(upload.ContentType).Equals("image/png")-- //Replace logo with a new one- newLogo, _ := ioutil.ReadFile(env.Path("./README.md"))- settings.Logo.Upload.Content = newLogo- err = tenants.UpdateSettings(settings)- Expect(err).IsNil()-- Expect(tenant.LogoID).NotEquals(firstLogoID)-- upload, err = tenants.GetUpload(tenant.LogoID)- Expect(err).IsNil()- Expect(upload.Content).Equals(newLogo)- Expect(upload.Size).Equals(len(newLogo))- Expect(upload.ContentType).Equals("text/html; charset=utf-8")+ Expect(tenant.LogoBlobKey).Equals("some-logo-key.png")}func TestTenantStorage_AdvancedSettings(t *testing.T) {@@ -378,15 +295,10 @@ func TestTenantStorage_Save_Get_ListOAuthConfig(t *testing.T) {Expect(config).IsNil()Expect(errors.Cause(err)).Equals(app.ErrNotFound)- logo, _ := ioutil.ReadFile(env.Path("./favicon.png"))-err = tenants.SaveOAuthConfig(&models.CreateEditOAuthConfig{ID: 0,Logo: &models.ImageUpload{- Upload: &models.ImageUploadData{- Content: logo,- ContentType: "image/png",- },+ BlobKey: "uploads/my-logo-key.png",},Provider: "_TEST",DisplayName: "My Provider",@@ -405,6 +317,7 @@ func TestTenantStorage_Save_Get_ListOAuthConfig(t *testing.T) {config, err = tenants.GetOAuthConfigByProvider("_TEST")Expect(err).IsNil()Expect(config.ID).Equals(1)+ Expect(config.LogoBlobKey).Equals("uploads/my-logo-key.png")Expect(config.Provider).Equals("_TEST")Expect(config.DisplayName).Equals("My Provider")Expect(config.ClientID).Equals("823187ahjjfdha8fds7yfdashfjkdsa")@@ -418,15 +331,10 @@ func TestTenantStorage_Save_Get_ListOAuthConfig(t *testing.T) {Expect(config.JSONUserNamePath).Equals("user.name")Expect(config.JSONUserEmailPath).Equals("user.email")- upload, err := tenants.GetUpload(config.LogoID)- Expect(err).IsNil()- Expect(upload.Content).Equals(logo)- Expect(upload.ContentType).Equals("image/png")-err = tenants.SaveOAuthConfig(&models.CreateEditOAuthConfig{ID: config.ID,Logo: &models.ImageUpload{- Remove: true,+ BlobKey: "",},Provider: "_TEST2222", //this has to be ignoredDisplayName: "New My Provider",@@ -446,7 +354,7 @@ func TestTenantStorage_Save_Get_ListOAuthConfig(t *testing.T) {Expect(err).IsNil()Expect(configs).HasLen(1)Expect(configs[0].ID).Equals(1)- Expect(configs[0].LogoID).Equals(0)+ Expect(configs[0].LogoBlobKey).Equals("")Expect(configs[0].Provider).Equals("_TEST")Expect(configs[0].DisplayName).Equals("New My Provider")Expect(configs[0].ClientID).Equals("New 823187ahjjfdha8fds7yfdashfjkdsa")diff --git a/app/storage/postgres/user_test.go b/app/storage/postgres/user_test.goindex f3e8db47a..f0f6abac5 100644--- a/app/storage/postgres/user_test.go+++ b/app/storage/postgres/user_test.go@@ -143,8 +143,8 @@ func TestUserStorage_Register_MultipleProviders(t *testing.T) {var tenantID inttrx.Get(&tenantID, `- INSERT INTO tenants (name, subdomain, created_at, status, is_private, custom_css)- VALUES ('My Domain Inc.','mydomain', now(), 1, false, '')+ INSERT INTO tenants (name, subdomain, created_at, status, is_private, custom_css, logo_bkey)+ VALUES ('My Domain Inc.','mydomain', now(), 1, false, '', '')RETURNING id`)users.SetCurrentTenant(&models.Tenant{diff --git a/app/storage/storage.go b/app/storage/storage.goindex 23165d8b7..748c5943e 100644--- a/app/storage/storage.go+++ b/app/storage/storage.go@@ -65,6 +65,7 @@ type User interface {// Tenant contains read and write operations for tenantstype Tenant interface {Base+ Current() *models.TenantAdd(name string, subdomain string, status int) (*models.Tenant, error)First() (*models.Tenant, error)Activate(id int) error@@ -77,7 +78,6 @@ type Tenant interface {SaveVerificationKey(key string, duration time.Duration, request models.NewEmailVerification) errorFindVerificationByKey(kind models.EmailVerificationKind, key string) (*models.EmailVerification, error)SetKeyAsVerified(key string) error- GetUpload(id int) (*models.Upload, error)SaveOAuthConfig(config *models.CreateEditOAuthConfig) errorGetOAuthConfigByProvider(provider string) (*models.OAuthConfig, error)ListOAuthConfig() ([]*models.OAuthConfig, error)diff --git a/migrations/201812171813_migrate_to_blobs.sql b/migrations/201812171813_migrate_to_blobs.sqlnew file mode 100644index 000000000..ecc31c298--- /dev/null+++ b/migrations/201812171813_migrate_to_blobs.sql@@ -0,0 +1,34 @@+insert into blobs (key, tenant_id, size, content_type, file, created_at, modified_at)+select+ 'logos/'||md5(cast(id as varchar)) || '.' || split_part(content_type, '/', 2) as key,+ tenant_id,+ size,+ content_type,+ file,+ created_at,+ created_at as modified_at+from uploads;++alter table tenants add column logo_bkey varchar(512) null;+alter table oauth_providers add column logo_bkey varchar(512) null;++update tenants+set logo_bkey = 'logos/'||md5(cast(u.id as varchar)) || '.' || split_part(u.content_type, '/', 2)+from uploads u+where u.id = tenants.logo_id;++update oauth_providers+set logo_bkey = 'logos/'||md5(cast(u.id as varchar)) || '.' || split_part(u.content_type, '/', 2)+from uploads u+where u.id = oauth_providers.logo_id;++alter table tenants drop column logo_id;+alter table oauth_providers drop column logo_id;++update tenants set logo_bkey = '' where logo_bkey is null;+update oauth_providers set logo_bkey = '' where logo_bkey is null;++alter table tenants alter column logo_bkey SET NOT NULL;+alter table oauth_providers alter column logo_bkey SET NOT NULL;++drop table uploads;\ No newline at end of filediff --git a/public/components/common/Logo.tsx b/public/components/common/Logo.tsxindex 9f6b217cb..57dba7d7a 100644--- a/public/components/common/Logo.tsx+++ b/public/components/common/Logo.tsx@@ -10,15 +10,15 @@ interface TenantLogoProps {export const TenantLogoURL = (size: Size): string | undefined => {const tenant = Fider.session.tenant;- if (tenant && tenant.logoID > 0) {- return uploadedImageURL(tenant.logoID, size);+ if (tenant && tenant.logoBlobKey) {+ return uploadedImageURL(tenant.logoBlobKey, size);}return undefined;};export const TenantLogo = (props: TenantLogoProps) => {const tenant = Fider.session.tenant;- if (tenant && tenant.logoID > 0) {+ if (tenant && tenant.logoBlobKey) {return <img src={TenantLogoURL(props.size)} alt={tenant.name} />;} else if (props.useFiderIfEmpty) {return <img src="https://getfider.com/images/logo-100x100.png" alt="Fider" />;@@ -34,7 +34,7 @@ interface OAuthProviderLogoProps {option: {provider?: string;displayName: string;- logoID?: number;+ logoBlobKey?: string;};}@@ -45,16 +45,16 @@ const systemProvidersLogo: { [key: string]: string } = {"data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PjwhRE9DVFlQRSBzdmcgIFBVQkxJQyAnLS8vVzNDLy9EVEQgU1ZHIDEuMC8vRU4nICAnaHR0cDovL3d3dy53My5vcmcvVFIvMjAwMS9SRUMtU1ZHLTIwMDEwOTA0L0RURC9zdmcxMC5kdGQnPjxzdmcgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyAwIDAgMzIgMzIiIGhlaWdodD0iMzJweCIgaWQ9IkxheWVyXzEiIHZlcnNpb249IjEuMCIgdmlld0JveD0iMCAwIDMyIDMyIiB3aWR0aD0iMzJweCIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PHBhdGggY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMTYuMDAzLDBDNy4xNywwLDAuMDA4LDcuMTYyLDAuMDA4LDE1Ljk5NyAgYzAsNy4wNjcsNC41ODIsMTMuMDYzLDEwLjk0LDE1LjE3OWMwLjgsMC4xNDYsMS4wNTItMC4zMjgsMS4wNTItMC43NTJjMC0wLjM4LDAuMDA4LTEuNDQyLDAtMi43NzcgIGMtNC40NDksMC45NjctNS4zNzEtMi4xMDctNS4zNzEtMi4xMDdjLTAuNzI3LTEuODQ4LTEuNzc1LTIuMzQtMS43NzUtMi4zNGMtMS40NTItMC45OTIsMC4xMDktMC45NzMsMC4xMDktMC45NzMgIGMxLjYwNSwwLjExMywyLjQ1MSwxLjY0OSwyLjQ1MSwxLjY0OWMxLjQyNywyLjQ0MywzLjc0MywxLjczNyw0LjY1NCwxLjMyOWMwLjE0Ni0xLjAzNCwwLjU2LTEuNzM5LDEuMDE3LTIuMTM5ICBjLTMuNTUyLTAuNDA0LTcuMjg2LTEuNzc2LTcuMjg2LTcuOTA2YzAtMS43NDcsMC42MjMtMy4xNzQsMS42NDYtNC4yOTJDNy4yOCwxMC40NjQsNi43Myw4LjgzNyw3LjYwMiw2LjYzNCAgYzAsMCwxLjM0My0wLjQzLDQuMzk4LDEuNjQxYzEuMjc2LTAuMzU1LDIuNjQ1LTAuNTMyLDQuMDA1LTAuNTM4YzEuMzU5LDAuMDA2LDIuNzI3LDAuMTgzLDQuMDA1LDAuNTM4ICBjMy4wNTUtMi4wNyw0LjM5Ni0xLjY0MSw0LjM5Ni0xLjY0MWMwLjg3MiwyLjIwMywwLjMyMywzLjgzLDAuMTU5LDQuMjM0YzEuMDIzLDEuMTE4LDEuNjQ0LDIuNTQ1LDEuNjQ0LDQuMjkyICBjMCw2LjE0Ni0zLjc0LDcuNDk4LTcuMzA0LDcuODkzQzE5LjQ3OSwyMy41NDgsMjAsMjQuNTA4LDIwLDI2YzAsMiwwLDMuOTAyLDAsNC40MjhjMCwwLjQyOCwwLjI1OCwwLjkwMSwxLjA3LDAuNzQ2ICBDMjcuNDIyLDI5LjA1NSwzMiwyMy4wNjIsMzIsMTUuOTk3QzMyLDcuMTYyLDI0LjgzOCwwLDE2LjAwMywweiIgZmlsbD0iIzE4MTYxNiIgZmlsbC1ydWxlPSJldmVub2RkIi8+PGcvPjxnLz48Zy8+PGcvPjxnLz48Zy8+PC9zdmc+"};-export const OAuthProviderLogoURL = (id?: number): string | undefined => {- if (id && id > 0) {- return uploadedImageURL(id, 100);+export const OAuthProviderLogoURL = (logoBlobKey?: string): string | undefined => {+ if (logoBlobKey) {+ return uploadedImageURL(logoBlobKey, 100);}return undefined;};export const OAuthProviderLogo = (props: OAuthProviderLogoProps) => {- if (props.option.logoID && props.option.logoID > 0) {- return <img src={OAuthProviderLogoURL(props.option.logoID)} alt={props.option.displayName} />;+ if (props.option.logoBlobKey) {+ return <img src={OAuthProviderLogoURL(props.option.logoBlobKey)} alt={props.option.displayName} />;}if (props.option.provider && props.option.provider in systemProvidersLogo) {diff --git a/public/components/common/SocialSignInButton.tsx b/public/components/common/SocialSignInButton.tsxindex f00dcd79b..210dcf98a 100644--- a/public/components/common/SocialSignInButton.tsx+++ b/public/components/common/SocialSignInButton.tsx@@ -7,7 +7,7 @@ interface SocialSignInButtonProps {displayName: string;provider?: string;url?: string;- logoID?: number;+ logoBlobKey?: string;logoURL?: string;};redirectTo?: string;diff --git a/public/components/common/form/ImageUploader.tsx b/public/components/common/form/ImageUploader.tsxindex 1fd2dcb8f..f9f766aaf 100644--- a/public/components/common/form/ImageUploader.tsx+++ b/public/components/common/form/ImageUploader.tsx@@ -21,6 +21,7 @@ interface ImageUploaderState extends ImageUploadState {export interface ImageUploadState {upload?: {+ fileName?: string;content?: string;contentType?: string;};@@ -45,6 +46,7 @@ export class ImageUploader extends React.Component<ImageUploaderProps, ImageUplothis.setState({upload: {+ fileName: file.name,content: base64,contentType: file.type,action: "upload"diff --git a/public/models/identity.ts b/public/models/identity.tsindex 746cbd7da..22eecfb79 100644--- a/public/models/identity.ts+++ b/public/models/identity.ts@@ -6,7 +6,7 @@ export interface Tenant {invitation: string;welcomeMessage: string;isPrivate: boolean;- logoID: number;+ logoBlobKey: string;}export interface User {diff --git a/public/models/settings.ts b/public/models/settings.tsindex 1fde403e0..a111a1459 100644--- a/public/models/settings.ts+++ b/public/models/settings.ts@@ -4,7 +4,7 @@ export interface OAuthProviderOption {clientID: string;url: string;callbackURL: string;- logoID: number;+ logoBlobKey: string;isCustomProvider: boolean;isEnabled: boolean;}@@ -41,7 +41,7 @@ export interface OAuthConfig {authorizeURL: string;tokenURL: string;profileURL: string;- logoID: number;+ logoBlobKey: string;scope: string;jsonUserIDPath: string;jsonUserNamePath: string;diff --git a/public/pages/Administration/components/OAuthForm.tsx b/public/pages/Administration/components/OAuthForm.tsxindex c6f522f7a..dffd85816 100644--- a/public/pages/Administration/components/OAuthForm.tsx+++ b/public/pages/Administration/components/OAuthForm.tsx@@ -55,7 +55,7 @@ export class OAuthForm extends React.Component<OAuthFormProps, OAuthFormState> {jsonUserIDPath: this.props.config ? this.props.config.jsonUserIDPath : "",jsonUserNamePath: this.props.config ? this.props.config.jsonUserNamePath : "",jsonUserEmailPath: this.props.config ? this.props.config.jsonUserEmailPath : "",- logoURL: this.props.config ? OAuthProviderLogoURL(this.props.config.logoID) : ""+ logoURL: this.props.config ? OAuthProviderLogoURL(this.props.config.logoBlobKey) : ""};}diff --git a/public/services/actions/tenant.ts b/public/services/actions/tenant.tsindex 0d65adace..32aa860c3 100644--- a/public/services/actions/tenant.ts+++ b/public/services/actions/tenant.ts@@ -26,6 +26,7 @@ export const createTenant = async (request: CreateTenantRequest): Promise<Resultexport interface UpdateTenantSettingsRequest {logo?: {upload?: {+ fileName?: string;content?: string;contentType?: string;};diff --git a/public/services/utils.ts b/public/services/utils.tsindex 8a69208c7..9aac465f8 100644--- a/public/services/utils.ts+++ b/public/services/utils.ts@@ -123,9 +123,9 @@ export const isCookieEnabled = (): boolean => {}};-export const uploadedImageURL = (id: number, size: number): string | undefined => {- if (id > 0) {- return `${Fider.settings.tenantAssetsURL}/images/${size}/${id}`;+export const uploadedImageURL = (bkey: string, size: number): string | undefined => {+ if (bkey) {+ return `${Fider.settings.tenantAssetsURL}/images/${size}/${bkey}`;}return undefined;};