Skip to content

Commit d2bc3c1

Browse files
authored
fix: mod creation with logo (#96)
1 parent 4363f9b commit d2bc3c1

11 files changed

+98
-27
lines changed

db/schema/mod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (Mod) Fields() []ent.Field {
2626
field.String("name").MaxLen(32),
2727
field.String("short_description").MaxLen(128),
2828
field.String("full_description"),
29-
field.String("logo"),
29+
field.String("logo").Optional(),
3030
field.String("logo_thumbhash").Optional(),
3131
field.String("source_url").Optional(),
3232
field.String("creator_id"),

generated/ent/internal/schema.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/ent/migrate/schema.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/ent/mod/where.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/ent/mod_create.go

+28-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/ent/mod_update.go

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/ent/mutation.go

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gql/resolver_mods.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,6 @@ func (r *mutationResolver) CreateMod(ctx context.Context, newMod generated.NewMo
8080

8181
dbMod.SetCreatorID(user.ID)
8282

83-
var logoData []byte
84-
var thumbHash string
85-
86-
if newMod.Logo != nil {
87-
file, err := io.ReadAll(newMod.Logo.File)
88-
if err != nil {
89-
return nil, fmt.Errorf("failed to read logo file: %w", err)
90-
}
91-
92-
logoData, thumbHash, err = converter.ConvertAnyImageToWebp(ctx, file)
93-
if err != nil {
94-
return nil, fmt.Errorf("failed to convert logo file: %w", err)
95-
}
96-
} else {
97-
dbMod.SetLogo("")
98-
}
99-
10083
// Allow only new 4 mods per 24h
10184
existingMods, err := db.From(ctx).Mod.Query().
10285
Order(mod.ByCreatedAt(sql.OrderAsc())).
@@ -136,12 +119,22 @@ func (r *mutationResolver) CreateMod(ctx context.Context, newMod generated.NewMo
136119
return nil, err
137120
}
138121

139-
if logoData != nil {
122+
if newMod.Logo != nil {
123+
file, err := io.ReadAll(newMod.Logo.File)
124+
if err != nil {
125+
return nil, fmt.Errorf("failed to read logo file: %w", err)
126+
}
127+
128+
logoData, thumbHash, err := converter.ConvertAnyImageToWebp(ctx, file)
129+
if err != nil {
130+
return nil, fmt.Errorf("failed to convert logo file: %w", err)
131+
}
132+
140133
logoKey, err := storage.UploadModLogo(ctx, resultMod.ID, bytes.NewReader(logoData))
141134
if err == nil {
142135
resultMod, err = resultMod.Update().
143136
SetLogo(storage.GenerateDownloadLink(ctx, logoKey)).
144-
SetNillableLogoThumbhash(util.ContainsOrNil(thumbHash)).
137+
SetLogoThumbhash(thumbHash).
145138
Save(ctx)
146139
if err != nil {
147140
return nil, err
@@ -208,7 +201,7 @@ func (r *mutationResolver) UpdateMod(ctx context.Context, modID string, updateMo
208201
dbUpdate.SetLogo(storage.GenerateDownloadLink(ctx, logoKey))
209202
dbUpdate.SetLogoThumbhash(thumbHash)
210203
} else {
211-
dbUpdate.SetLogo("")
204+
dbUpdate.ClearLogo()
212205
dbUpdate.ClearLogoThumbhash()
213206
}
214207
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- reverse: modify "mods" table
2+
ALTER TABLE "mods" ALTER COLUMN "logo" SET NOT NULL;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- modify "mods" table
2+
ALTER TABLE "mods" ALTER COLUMN "logo" DROP NOT NULL;

migrations/sql/atlas.sum

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:EOAzeUdvTGahAD3VGUxcEW7xEMJthrP36wbaIBYE6Fs=
1+
h1:NVXwUmNMPEOBFBSqgQGuw/OMarJOpSsxkE/8o1qQH3k=
22
000001_create_initial_tables.down.sql h1:QcXXwE8ir3PM3vZjQfBihY55WiuLYjN8pbbk/L6U+bs=
33
000001_create_initial_tables.up.sql h1:EfAqETefs7TW9BDVS4ukiB7wUFs+HQzVdc3qHhdAFd4=
44
000002_create_user_group_table.down.sql h1:uJjRbQ3H7yvPGNXLD5iSPmderEoWhCNGdsqN8yqBIRY=
@@ -77,3 +77,5 @@ h1:EOAzeUdvTGahAD3VGUxcEW7xEMJthrP36wbaIBYE6Fs=
7777
20241002191358_state_fix.up.sql h1:4bVw9fxEP9MrHBSYuRF3vFCgaWF79EUeacD3e+vSQR4=
7878
20241003161005_create_virustotal_results_table.down.sql h1:BPUQ6UnWq6Inwz2GhZLoy4/QghPhClf1PlfZHis92P4=
7979
20241003161005_create_virustotal_results_table.up.sql h1:mXMWj998hZ59bbkZYIbbyxU9foptzWuXHnlS2UuY4bY=
80+
20241008092121_nullable_mod_logo.down.sql h1:vKZrz/FqaaQctvzrTR0EvQcyQ6ENWgTbcud7q2/NvnY=
81+
20241008092121_nullable_mod_logo.up.sql h1:7ePponYitScS4NnMP+hRoEAKnvPPpbCHXMxilYzfmD8=

0 commit comments

Comments
 (0)