Ver Fonte

Expose runner from pipeline context

DarthSim há 2 semanas atrás
pai
commit
7ebf913f98
3 ficheiros alterados com 32 adições e 19 exclusões
  1. 9 0
      processing/pipeline.go
  2. 8 8
      processing/processing.go
  3. 15 11
      processing/watermark.go

+ 9 - 0
processing/pipeline.go

@@ -13,6 +13,9 @@ import (
 
 // NOTE: this will be called pipeline.Context in the separate package
 type Context struct {
+	// The runner that runs this pipeline
+	runner *Runner
+
 	Ctx context.Context
 
 	// Global processing configuration which could be used by individual steps
@@ -127,6 +130,8 @@ func (r *Runner) newContext(
 	imgdata imagedata.ImageData,
 ) Context {
 	pctx := Context{
+		runner: r,
+
 		Ctx:     ctx,
 		Config:  r.config,
 		Img:     img,
@@ -149,3 +154,7 @@ func (r *Runner) newContext(
 
 	return pctx
 }
+
+func (c *Context) Runner() *Runner {
+	return c.runner
+}

+ 8 - 8
processing/processing.go

@@ -417,6 +417,13 @@ func transformAnimated(
 	po *options.ProcessingOptions,
 	watermark auximageprovider.Provider,
 ) error {
+	// NOTE: THIS IS TEMPORARY
+	runner, rerr := tmpNewRunner(watermark)
+	if rerr != nil {
+		return rerr
+	}
+	// NOTE: END TEMPORARY BLOCK
+
 	if po.Trim.Enabled {
 		log.Warning("Trim is not supported for animated images")
 		po.Trim.Enabled = false
@@ -467,13 +474,6 @@ func transformAnimated(
 
 		frames = append(frames, frame)
 
-		// NOTE: THIS IS TEMPORARY
-		runner, rerr := tmpNewRunner(watermark)
-		if rerr != nil {
-			return rerr
-		}
-		// NOTE: END TEMPORARY BLOCK
-
 		// Transform the frame using the main pipeline.
 		// We don't provide imgdata here to prevent scale-on-load.
 		// Watermarking is disabled for individual frames (see above)
@@ -509,7 +509,7 @@ func transformAnimated(
 			dprScale = 1.0
 		}
 
-		if err = applyWatermark(ctx, img, watermark, po, dprScale, framesCount); err != nil {
+		if err = applyWatermark(ctx, runner, img, watermark, po, dprScale, framesCount); err != nil {
 			return err
 		}
 	}

+ 15 - 11
processing/watermark.go

@@ -22,7 +22,15 @@ var watermarkPipeline = Pipeline{
 	padding,
 }
 
-func prepareWatermark(wm *vips.Image, wmData imagedata.ImageData, po *options.ProcessingOptions, imgWidth, imgHeight int, offsetScale float64, framesCount int) error {
+func prepareWatermark(
+	ctx context.Context,
+	runner *Runner,
+	wm *vips.Image,
+	wmData imagedata.ImageData,
+	po *options.ProcessingOptions,
+	imgWidth, imgHeight int,
+	offsetScale float64,
+) error {
 	if err := wm.Load(wmData, 1, 1.0, 1); err != nil {
 		return err
 	}
@@ -62,14 +70,7 @@ func prepareWatermark(wm *vips.Image, wmData imagedata.ImageData, po *options.Pr
 		wmPo.Padding.Bottom = offY - wmPo.Padding.Top
 	}
 
-	// NOTE: THIS IS TEMPORARY
-	runner, err := tmpNewRunner(nil) // watermark will present in runner
-	if err != nil {
-		return err
-	}
-	// NOTE: END TEMPORARY BLOCK
-
-	if err := runner.Run(watermarkPipeline, context.Background(), wm, wmPo, wmData); err != nil {
+	if err := runner.Run(watermarkPipeline, ctx, wm, wmPo, wmData); err != nil {
 		return err
 	}
 
@@ -91,6 +92,7 @@ func prepareWatermark(wm *vips.Image, wmData imagedata.ImageData, po *options.Pr
 
 func applyWatermark(
 	ctx context.Context,
+	runner *Runner,
 	img *vips.Image,
 	watermark auximageprovider.Provider,
 	po *options.ProcessingOptions,
@@ -119,7 +121,9 @@ func applyWatermark(
 	height := img.Height()
 	frameHeight := height / framesCount
 
-	if err := prepareWatermark(wm, wmData, po, width, frameHeight, offsetScale, framesCount); err != nil {
+	if err := prepareWatermark(
+		ctx, runner, wm, wmData, po, width, frameHeight, offsetScale,
+	); err != nil {
 		return err
 	}
 
@@ -198,5 +202,5 @@ func watermark(c *Context) error {
 		return nil
 	}
 
-	return applyWatermark(c.Ctx, c.Img, c.WatermarkProvider, c.PO, c.DprScale, 1)
+	return applyWatermark(c.Ctx, c.Runner(), c.Img, c.WatermarkProvider, c.PO, c.DprScale, 1)
 }