Files
sm-journal-plugin/jobs.go
Jakub Zych 4f5bd190e5 feat: image variants job and variants in the post API
- golem15_journal_image_variants (migration 202610280002): a thumbnail
  per width of golem15.journal.image_widths (480, 800, 1200 by default,
  never wider than the original) and its WebP copy, with the original's
  size
- conga job golem15.journal.image_variants (three attempts), queued after
  a featured image upload and after an admin save of a post whose images
  lack a width; the upload request never encodes
- journal:image-variants [--all] backfills
- featured_images[].variants {width, height, url, webp_url, source_width,
  source_height} on the post answers (additive key)
2026-11-04 17:05:16 +01:00

43 lines
1.3 KiB
Go

package journal
import (
"context"
"errors"
"git.golem15.com/golem15/sm-journal-plugin/classes"
"git.golem15.com/golem15/summercms/modules/conga"
"git.golem15.com/golem15/summercms/modules/pact"
"gocloud.dev/blob"
"gorm.io/gorm"
)
var _ pact.HasJobs = (*Plugin)(nil)
// Jobs registers the image variants job (golem15.journal.image_variants,
// D-85): thumbnails and WebP copies of featured images, outside the upload
// request, three attempts.
func (p *Plugin) Jobs() []pact.Job {
return []pact.Job{conga.Job(p.imageVariants, conga.MaxAttempts(3))}
}
// imageVariants runs one image variants job.
func (p *Plugin) imageVariants(ctx context.Context, args classes.ImageVariantsArgs) error {
if p.app == nil {
return errors.New("journal: app is not booted")
}
db, ok := p.app.Lookup[*gorm.DB]()
if !ok || db == nil {
return errors.New("journal: database is not configured")
}
bucket, ok := p.app.Lookup[*blob.Bucket]()
if !ok || bucket == nil {
return errors.New("journal: the uploads bucket is not configured")
}
res, err := classes.GenerateImageVariants(ctx, db, bucket, classes.ImageWidths(p.app.Config), args.FileIDs)
if err != nil {
return err
}
pluginLog.Info("journal: image variants", "files", res.Files, "written", res.Written, "skipped", res.Skipped)
return nil
}