1
1
import * as sharp from 'sharp' ;
2
2
import { Features , IAction , InvalidArgument , IProcessContext , IProcessor , IProcessResponse } from '../../processor' ;
3
3
import { IBufferStore } from '../../store' ;
4
+ import { ActionMask } from './_base' ;
4
5
import { AutoOrientAction } from './auto-orient' ;
5
6
import { BlurAction } from './blur' ;
6
7
import { BrightAction } from './bright' ;
@@ -46,14 +47,16 @@ export class ImageProcessor implements IProcessor {
46
47
const ctx : IProcessContext = {
47
48
uri,
48
49
actions,
50
+ mask : new ActionMask ( actions ) ,
49
51
bufferStore,
50
52
features : {
51
53
[ Features . AutoOrient ] : true ,
52
54
[ Features . ReadAllAnimatedFrames ] : true ,
53
55
} ,
54
56
headers : { } ,
55
57
} ;
56
- for ( const action of actions ) {
58
+ for ( let i = 0 ; i < actions . length ; i ++ ) {
59
+ const action = actions [ i ] ;
57
60
if ( ( this . name === action ) || ( ! action ) ) {
58
61
continue ;
59
62
}
@@ -64,7 +67,7 @@ export class ImageProcessor implements IProcessor {
64
67
if ( ! act ) {
65
68
throw new InvalidArgument ( `Unkown action: "${ name } "` ) ;
66
69
}
67
- act . beforeNewContext . bind ( act ) ( ctx , params ) ;
70
+ act . beforeNewContext . bind ( act ) ( ctx , params , i ) ;
68
71
}
69
72
const { buffer, headers } = await bufferStore . get ( uri ) ;
70
73
const image = sharp ( buffer , { failOnError : false , animated : ctx . features [ Features . ReadAllAnimatedFrames ] } ) ;
@@ -77,7 +80,7 @@ export class ImageProcessor implements IProcessor {
77
80
return {
78
81
uri : ctx . uri ,
79
82
actions : ctx . actions ,
80
- effectiveActions : ctx . effectiveActions ,
83
+ mask : ctx . mask ,
81
84
bufferStore : ctx . bufferStore ,
82
85
features : ctx . features ,
83
86
headers : Object . assign ( ctx . headers , headers ) ,
@@ -96,8 +99,28 @@ export class ImageProcessor implements IProcessor {
96
99
97
100
if ( ctx . features [ Features . AutoOrient ] ) { ctx . image . rotate ( ) ; }
98
101
99
- const actions = ( ctx . effectiveActions && ctx . effectiveActions . length ) ? ctx . effectiveActions : ctx . actions ;
100
- for ( const action of actions ) {
102
+ ctx . mask . forEachAction ( ( action , _ , index ) => {
103
+ if ( ( this . name === action ) || ( ! action ) ) {
104
+ return ;
105
+ }
106
+ // "<action-name>,<param-1>,<param-2>,..."
107
+ const params = action . split ( ',' ) ;
108
+ const name = params [ 0 ] ;
109
+ const act = this . action ( name ) ;
110
+ if ( ! act ) {
111
+ throw new InvalidArgument ( `Unkown action: "${ name } "` ) ;
112
+ }
113
+ act . beforeProcess . bind ( act ) ( ctx , params , index ) ;
114
+ } ) ;
115
+ const enabledActions = ctx . mask . filterEnabledActions ( ) ;
116
+ const nothing2do = ( enabledActions . length === 1 ) && ( this . name === enabledActions [ 0 ] ) ;
117
+
118
+ if ( nothing2do && ( ! ctx . features [ Features . AutoWebp ] ) ) {
119
+ const { buffer } = await ctx . bufferStore . get ( ctx . uri ) ;
120
+ return { data : buffer , type : ctx . metadata . format ! } ;
121
+ }
122
+
123
+ for ( const action of enabledActions ) {
101
124
if ( ( this . name === action ) || ( ! action ) ) {
102
125
continue ;
103
126
}
0 commit comments