Files
sm-journal-plugin/console/image_variants.go
Jakub Zych fc3c25530c fix: run journal:image-variants from the application CLI
- the generated CLI publishes no database or uploads bucket for plugin
  commands, so the command stopped with database is not configured
- without a published database it opens database.dsn and publishes it, and
  without a published bucket it opens storage.uploads.bucket_url, closing
  what it opened
2026-11-04 19:27:00 +01:00

72 lines
2.7 KiB
Go

package console
import (
"context"
"fmt"
"git.golem15.com/golem15/sm-journal-plugin/classes"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
)
// ImageVariants is journal:image-variants: it writes the thumbnails and
// WebP copies of the posts' featured images at golem15.journal.image_widths
// in this process (the backfill of the golem15.journal.image_variants job).
// Without --all it takes the images that lack a variant of some width.
func ImageVariants(app *backpack.App) bonfire.Command {
return bonfire.Command{
Name: "journal:image-variants",
Description: "Write the WebP and resized variants of featured images",
Long: `Writes, for every post's featured image, a thumbnail at each width of
golem15.journal.image_widths (480, 800 and 1200 by default) that is not
wider than the original, and a WebP copy of each next to it, and records
them in golem15_journal_image_variants. The Journal API lists them as
featured_images[].variants and the journal layer serves them through
<picture> with the original format as fallback.
New uploads get their variants from the golem15.journal.image_variants job,
queued after the upload commits. This command is the backfill: without
--all it takes the images that lack a variant of some width (for example
after adding a width), with --all it checks every featured image. A
variant that exists is never written again, so the command can run any
number of times.
It runs from the application's CLI as well as next to serve: without a
running server it opens database.dsn and storage.uploads.bucket_url
itself.`,
Flags: []bonfire.Flag{
{Name: "all", Description: "Check every featured image (default: only those missing a width)", Bare: true},
},
Examples: []bonfire.Example{
{Command: "journal:image-variants", Description: "Write the variants images are missing"},
{Command: "journal:image-variants --all", Description: "Check every featured image"},
},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
db, done, err := lookupDB(ctx, app)
if err != nil {
return err
}
defer done()
bucket, closeBucket, err := lookupBucket(ctx, app)
if err != nil {
return err
}
defer closeBucket()
ids, err := classes.FeaturedImageIDs(db.WithContext(ctx), appConfig(app), flagOn(in, "all"))
if err != nil {
return err
}
if len(ids) == 0 {
out.Info("Every featured image has its variants.")
return nil
}
res, err := classes.GenerateImageVariants(ctx, db, bucket, classes.ImageWidths(appConfig(app)), ids)
if err != nil {
return err
}
out.Success(fmt.Sprintf("Checked %d featured images, wrote %d variants, skipped %d.", res.Files, res.Written, res.Skipped))
return nil
},
}
}