Skip to content

Commit

Permalink
fix: mod creation with logo (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata authored Oct 8, 2024
1 parent 4363f9b commit d2bc3c1
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 27 deletions.
2 changes: 1 addition & 1 deletion db/schema/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (Mod) Fields() []ent.Field {
field.String("name").MaxLen(32),
field.String("short_description").MaxLen(128),
field.String("full_description"),
field.String("logo"),
field.String("logo").Optional(),
field.String("logo_thumbhash").Optional(),
field.String("source_url").Optional(),
field.String("creator_id"),
Expand Down
2 changes: 1 addition & 1 deletion generated/ent/internal/schema.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion generated/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions generated/ent/mod/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 28 additions & 3 deletions generated/ent/mod_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions generated/ent/mod_update.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions generated/ent/mutation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 13 additions & 20 deletions gql/resolver_mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,6 @@ func (r *mutationResolver) CreateMod(ctx context.Context, newMod generated.NewMo

dbMod.SetCreatorID(user.ID)

var logoData []byte
var thumbHash string

if newMod.Logo != nil {
file, err := io.ReadAll(newMod.Logo.File)
if err != nil {
return nil, fmt.Errorf("failed to read logo file: %w", err)
}

logoData, thumbHash, err = converter.ConvertAnyImageToWebp(ctx, file)
if err != nil {
return nil, fmt.Errorf("failed to convert logo file: %w", err)
}
} else {
dbMod.SetLogo("")
}

// Allow only new 4 mods per 24h
existingMods, err := db.From(ctx).Mod.Query().
Order(mod.ByCreatedAt(sql.OrderAsc())).
Expand Down Expand Up @@ -136,12 +119,22 @@ func (r *mutationResolver) CreateMod(ctx context.Context, newMod generated.NewMo
return nil, err
}

if logoData != nil {
if newMod.Logo != nil {
file, err := io.ReadAll(newMod.Logo.File)
if err != nil {
return nil, fmt.Errorf("failed to read logo file: %w", err)
}

logoData, thumbHash, err := converter.ConvertAnyImageToWebp(ctx, file)
if err != nil {
return nil, fmt.Errorf("failed to convert logo file: %w", err)
}

logoKey, err := storage.UploadModLogo(ctx, resultMod.ID, bytes.NewReader(logoData))
if err == nil {
resultMod, err = resultMod.Update().
SetLogo(storage.GenerateDownloadLink(ctx, logoKey)).
SetNillableLogoThumbhash(util.ContainsOrNil(thumbHash)).
SetLogoThumbhash(thumbHash).
Save(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -208,7 +201,7 @@ func (r *mutationResolver) UpdateMod(ctx context.Context, modID string, updateMo
dbUpdate.SetLogo(storage.GenerateDownloadLink(ctx, logoKey))
dbUpdate.SetLogoThumbhash(thumbHash)
} else {
dbUpdate.SetLogo("")
dbUpdate.ClearLogo()
dbUpdate.ClearLogoThumbhash()
}
}
Expand Down
2 changes: 2 additions & 0 deletions migrations/sql/20241008092121_nullable_mod_logo.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- reverse: modify "mods" table
ALTER TABLE "mods" ALTER COLUMN "logo" SET NOT NULL;
2 changes: 2 additions & 0 deletions migrations/sql/20241008092121_nullable_mod_logo.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- modify "mods" table
ALTER TABLE "mods" ALTER COLUMN "logo" DROP NOT NULL;
4 changes: 3 additions & 1 deletion migrations/sql/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
h1:EOAzeUdvTGahAD3VGUxcEW7xEMJthrP36wbaIBYE6Fs=
h1:NVXwUmNMPEOBFBSqgQGuw/OMarJOpSsxkE/8o1qQH3k=
000001_create_initial_tables.down.sql h1:QcXXwE8ir3PM3vZjQfBihY55WiuLYjN8pbbk/L6U+bs=
000001_create_initial_tables.up.sql h1:EfAqETefs7TW9BDVS4ukiB7wUFs+HQzVdc3qHhdAFd4=
000002_create_user_group_table.down.sql h1:uJjRbQ3H7yvPGNXLD5iSPmderEoWhCNGdsqN8yqBIRY=
Expand Down Expand Up @@ -77,3 +77,5 @@ h1:EOAzeUdvTGahAD3VGUxcEW7xEMJthrP36wbaIBYE6Fs=
20241002191358_state_fix.up.sql h1:4bVw9fxEP9MrHBSYuRF3vFCgaWF79EUeacD3e+vSQR4=
20241003161005_create_virustotal_results_table.down.sql h1:BPUQ6UnWq6Inwz2GhZLoy4/QghPhClf1PlfZHis92P4=
20241003161005_create_virustotal_results_table.up.sql h1:mXMWj998hZ59bbkZYIbbyxU9foptzWuXHnlS2UuY4bY=
20241008092121_nullable_mod_logo.down.sql h1:vKZrz/FqaaQctvzrTR0EvQcyQ6ENWgTbcud7q2/NvnY=
20241008092121_nullable_mod_logo.up.sql h1:7ePponYitScS4NnMP+hRoEAKnvPPpbCHXMxilYzfmD8=

0 comments on commit d2bc3c1

Please sign in to comment.