From 8e7c20fffa38e4e0ce9d85fb2abb88c8db2fbf85 Mon Sep 17 00:00:00 2001 From: Asaf Gartner Date: Sun, 13 Feb 2022 22:07:09 +0200 Subject: [PATCH] Removed old project logo url fields --- src/admintools/admintools.go | 142 ------------------ ...022-02-13T200155Z_RemoveProjectLogoUrls.go | 49 ++++++ src/models/project.go | 3 - src/templates/mapping.go | 5 +- src/website/base_data.go | 6 +- src/website/requesthandling.go | 13 +- src/website/routes.go | 2 + 7 files changed, 62 insertions(+), 158 deletions(-) create mode 100644 src/migration/migrations/2022-02-13T200155Z_RemoveProjectLogoUrls.go diff --git a/src/admintools/admintools.go b/src/admintools/admintools.go index accba5c6..d88d1e48 100644 --- a/src/admintools/admintools.go +++ b/src/admintools/admintools.go @@ -1,23 +1,16 @@ package admintools import ( - "bytes" "context" "errors" "fmt" - "image" - "net/http" "os" - "path" "strconv" - "strings" "time" - "git.handmade.network/hmn/hmn/src/assets" "git.handmade.network/hmn/hmn/src/auth" "git.handmade.network/hmn/hmn/src/db" "git.handmade.network/hmn/hmn/src/email" - "git.handmade.network/hmn/hmn/src/hmndata" "git.handmade.network/hmn/hmn/src/logging" "git.handmade.network/hmn/hmn/src/models" "git.handmade.network/hmn/hmn/src/oops" @@ -360,140 +353,5 @@ func init() { moveThreadsToSubforumCommand.MarkFlagRequired("subforum_slug") adminCommand.AddCommand(moveThreadsToSubforumCommand) - uploadProjectLogos := &cobra.Command{ - Use: "uploadprojectlogos", - Short: "Uploads project imagefiles to S3 and replaces them with assets", - Run: func(cmd *cobra.Command, args []string) { - ctx := context.Background() - conn := db.NewConnPool(1, 1) - defer conn.Close() - - allProjects, err := db.Query(ctx, conn, models.Project{}, `SELECT $columns FROM handmade_project`) - if err != nil { - panic(oops.New(err, "Failed to fetch projects from db")) - } - - var fixupProjects []*models.Project - numImages := 0 - for _, project := range allProjects { - p := project.(*models.Project) - if p.LogoLight != "" || p.LogoDark != "" { - fixupProjects = append(fixupProjects, p) - } - - if p.LogoLight != "" { - numImages += 1 - } - if p.LogoDark != "" { - numImages += 1 - } - } - - fmt.Printf("%d images to upload\n", numImages) - - uploadImage := func(ctx context.Context, conn db.ConnOrTx, filepath string, owner *models.User) *models.Asset { - filepath = "./public/media/" + filepath - contents, err := os.ReadFile(filepath) - if err != nil { - panic(oops.New(err, fmt.Sprintf("Failed to read file: %s", filepath))) - } - width := 0 - height := 0 - mime := "" - fileExtensionOverrides := []string{".svg"} - fileExt := strings.ToLower(path.Ext(filepath)) - tryDecode := true - for _, ext := range fileExtensionOverrides { - if fileExt == ext { - tryDecode = false - } - } - if tryDecode { - config, _, err := image.DecodeConfig(bytes.NewReader(contents)) - if err != nil { - panic(oops.New(err, fmt.Sprintf("Failed to decode file: %s", filepath))) - } - width = config.Width - height = config.Height - mime = http.DetectContentType(contents) - } else { - if fileExt == ".svg" { - mime = "image/svg+xml" - } - } - - filename := path.Base(filepath) - - asset, err := assets.Create(ctx, conn, assets.CreateInput{ - Content: contents, - Filename: filename, - ContentType: mime, - UploaderID: &owner.ID, - Width: width, - Height: height, - }) - if err != nil { - panic(oops.New(err, "Failed to create asset")) - } - - return asset - } - - for _, p := range fixupProjects { - owners, err := hmndata.FetchProjectOwners(ctx, conn, p.ID) - if err != nil { - panic(oops.New(err, "Failed to fetch project owners")) - } - if len(owners) == 0 { - fmt.Printf("PROBLEM!! Project %d (%s) doesn't have owners!!\n", p.ID, p.Name) - continue - } - if p.LogoLight != "" { - lightAsset := uploadImage(ctx, conn, p.LogoLight, owners[0]) - _, err := conn.Exec(ctx, - ` - UPDATE handmade_project - SET - logolight_asset_id = $2, - logolight = NULL - WHERE - id = $1 - `, - p.ID, - lightAsset.ID, - ) - if err != nil { - panic(oops.New(err, "Failed to update project")) - } - numImages -= 1 - fmt.Printf(".") - } - if p.LogoDark != "" { - darkAsset := uploadImage(ctx, conn, p.LogoDark, owners[0]) - _, err := conn.Exec(ctx, - ` - UPDATE handmade_project - SET - logodark_asset_id = $2, - logodark = NULL - WHERE - id = $1 - `, - p.ID, - darkAsset.ID, - ) - if err != nil { - panic(oops.New(err, "Failed to update project")) - } - numImages -= 1 - fmt.Printf(".") - } - } - - fmt.Printf("\nDone! %d images not patched for some reason.\n\n", numImages) - }, - } - adminCommand.AddCommand(uploadProjectLogos) - addProjectCommands(adminCommand) } diff --git a/src/migration/migrations/2022-02-13T200155Z_RemoveProjectLogoUrls.go b/src/migration/migrations/2022-02-13T200155Z_RemoveProjectLogoUrls.go new file mode 100644 index 00000000..4e330e7c --- /dev/null +++ b/src/migration/migrations/2022-02-13T200155Z_RemoveProjectLogoUrls.go @@ -0,0 +1,49 @@ +package migrations + +import ( + "context" + "time" + + "git.handmade.network/hmn/hmn/src/migration/types" + "github.com/jackc/pgx/v4" +) + +func init() { + registerMigration(RemoveProjectLogoUrls{}) +} + +type RemoveProjectLogoUrls struct{} + +func (m RemoveProjectLogoUrls) Version() types.MigrationVersion { + return types.MigrationVersion(time.Date(2022, 2, 13, 20, 1, 55, 0, time.UTC)) +} + +func (m RemoveProjectLogoUrls) Name() string { + return "RemoveProjectLogoUrls" +} + +func (m RemoveProjectLogoUrls) Description() string { + return "Remove project logo url fields as we're now using assets" +} + +func (m RemoveProjectLogoUrls) Up(ctx context.Context, tx pgx.Tx) error { + _, err := tx.Exec(ctx, + ` + ALTER TABLE handmade_project + DROP COLUMN logolight, + DROP COLUMN logodark; + `, + ) + return err +} + +func (m RemoveProjectLogoUrls) Down(ctx context.Context, tx pgx.Tx) error { + _, err := tx.Exec(ctx, + ` + ALTER TABLE handmade_project + ADD COLUMN logolight character varying(100), + ADD COLUMN logodark character varying(100); + `, + ) + return err +} diff --git a/src/models/project.go b/src/models/project.go index e089b702..27c07630 100644 --- a/src/models/project.go +++ b/src/models/project.go @@ -72,9 +72,6 @@ type Project struct { Color1 string `db:"color_1"` Color2 string `db:"color_2"` - LogoLight string `db:"logolight"` - LogoDark string `db:"logodark"` - Personal bool `db:"personal"` Hidden bool `db:"hidden"` Featured bool `db:"featured"` diff --git a/src/templates/mapping.go b/src/templates/mapping.go index 3186aa23..3df02dde 100644 --- a/src/templates/mapping.go +++ b/src/templates/mapping.go @@ -64,16 +64,13 @@ func ProjectLogoUrl(p *models.Project, lightAsset *models.Asset, darkAsset *mode if theme == "dark" { if darkAsset != nil { return hmnurl.BuildS3Asset(darkAsset.S3Key) - } else { - return hmnurl.BuildUserFile(p.LogoDark) } } else { if lightAsset != nil { return hmnurl.BuildS3Asset(lightAsset.S3Key) - } else { - return hmnurl.BuildUserFile(p.LogoLight) } } + return "" } func ProjectToTemplate( diff --git a/src/website/base_data.go b/src/website/base_data.go index 29602539..7436a05e 100644 --- a/src/website/base_data.go +++ b/src/website/base_data.go @@ -58,7 +58,7 @@ func getBaseData(c *RequestContext, title string, breadcrumbs []templates.Breadc ReportIssueMailto: "team@handmade.network", - OpenGraphItems: buildDefaultOpenGraphItems(&project, title), + OpenGraphItems: buildDefaultOpenGraphItems(&project, c.CurrentProjectLogoUrl, title), IsProjectPage: !project.IsHMN(), Header: templates.Header{ @@ -114,14 +114,14 @@ func getBaseData(c *RequestContext, title string, breadcrumbs []templates.Breadc return baseData } -func buildDefaultOpenGraphItems(project *models.Project, title string) []templates.OpenGraphItem { +func buildDefaultOpenGraphItems(project *models.Project, projectLogoUrl string, title string) []templates.OpenGraphItem { if title == "" { title = "Handmade Network" } image := hmnurl.BuildPublic("logo.png", false) if !project.IsHMN() { - image = hmnurl.BuildUserFile(project.LogoLight) + image = projectLogoUrl } return []templates.OpenGraphItem{ diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index 1a4e689a..b6536066 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -159,12 +159,13 @@ type RequestContext struct { // We sometimes need the original response object so that some functions of the http package can set connection-management flags on it. Res http.ResponseWriter - Conn *pgxpool.Pool - CurrentProject *models.Project - CurrentUser *models.User - CurrentSession *models.Session - Theme string - UrlContext *hmnurl.UrlContext + Conn *pgxpool.Pool + CurrentProject *models.Project + CurrentProjectLogoUrl string + CurrentUser *models.User + CurrentSession *models.Session + Theme string + UrlContext *hmnurl.UrlContext CurrentUserCanEditCurrentProject bool diff --git a/src/website/routes.go b/src/website/routes.go index c5fc792a..de55ed68 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -476,6 +476,7 @@ func LoadCommonWebsiteData(c *RequestContext) (bool, ResponseData) { }) if err == nil { c.CurrentProject = &dbProject.Project + c.CurrentProjectLogoUrl = templates.ProjectLogoUrl(&dbProject.Project, dbProject.LogoLightAsset, dbProject.LogoDarkAsset, c.Theme) owners = dbProject.Owners } else { if errors.Is(err, db.NotFound) { @@ -495,6 +496,7 @@ func LoadCommonWebsiteData(c *RequestContext) (bool, ResponseData) { panic(oops.New(err, "failed to fetch HMN project")) } c.CurrentProject = &dbProject.Project + c.CurrentProjectLogoUrl = templates.ProjectLogoUrl(&dbProject.Project, dbProject.LogoLightAsset, dbProject.LogoDarkAsset, c.Theme) } if c.CurrentProject == nil {