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 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 }, } }