@@ -31,40 +31,40 @@ class ImageRequest {
31
31
32
32
this . sharpObject = sharp ( this . originalImageBody )
33
33
this . originalMetadata = await this . sharpObject . metadata ( )
34
-
35
- const qp = this . _parseQueryParams ( )
36
-
37
34
this . headers = this . event . headers
38
35
39
- if ( qp . auto !== undefined ) {
40
- qp . fm = this . inferAutoFormat ( )
41
- }
42
- this . schema = schemaParser . getSchemaForQueryParams ( qp )
43
- this . edits = schemaParser . normalizeAndValidateSchema ( this . schema , qp )
36
+ const queryParams = this . normalizeQueryParams ( this . event . queryStringParameters )
37
+
38
+ this . schema = schemaParser . getSchemaForQueryParams ( queryParams )
39
+ this . edits = schemaParser . normalizeAndValidateSchema ( this . schema , queryParams )
44
40
}
45
41
46
- inferAutoFormat ( ) {
47
- const specialOutputFormats = eventParser . getAcceptedImageFormatsFromHeaders ( this . headers )
42
+ getAutoFormat ( ) {
48
43
const coercibleFormats = [ 'jpg' , 'png' , 'webp' , 'avif' , 'jpeg' , 'tiff' ]
44
+ const autoParam = this . event . multiValueQueryStringParameters . auto
45
+ const specialOutputFormats = eventParser . getAcceptedImageFormatsFromHeaders ( this . headers )
49
46
50
- if ( ! coercibleFormats . includes ( this . originalMetadata . format ) ) {
51
- return this . originalMetadata . format
47
+ if (
48
+ ! autoParam ||
49
+ ! autoParam . includes ( 'format' ) ||
50
+ ! coercibleFormats . includes ( this . originalMetadata . format )
51
+ ) {
52
+ return null
52
53
}
53
54
54
- // TODO: Ensure image is at least 16x16px for avif compatibility - this needs to happen down the line after schema parsing and validation
55
55
if ( specialOutputFormats . includes ( 'avif' ) ) {
56
56
return 'avif'
57
57
}
58
58
// If avif isn't available, try to use webp
59
- if ( specialOutputFormats . includes ( 'webp' ) ) {
59
+ else if ( specialOutputFormats . includes ( 'webp' ) ) {
60
60
return 'webp'
61
61
}
62
62
// Coerce pngs and tiffs without alpha channels to jpg
63
- if ( ! this . originalMetadata . hasAlpha && ( [ 'png' , 'tiff' ] . contains ( this . originalMetadata . format ) ) ) {
63
+ else if ( ! this . originalMetadata . hasAlpha && ( [ 'png' , 'tiff' ] . includes ( this . originalMetadata . format ) ) ) {
64
64
return 'jpeg'
65
65
}
66
- // There's no other coercion worth doing, use original format
67
- return this . originalMetadata . format
66
+
67
+ return null
68
68
}
69
69
70
70
/**
@@ -107,17 +107,13 @@ class ImageRequest {
107
107
return true
108
108
}
109
109
110
- /**
111
- * Decodes the image request path associated with default
112
- * image requests. Provides error handling for invalid or undefined path values.
113
- * @param {Object } event - The proxied request object.
114
- */
115
- _parseQueryParams ( ) {
116
- let qp = this . event . queryStringParameters
117
- if ( ! qp ) {
118
- qp = { }
119
- }
120
- return schemaParser . replaceAliases ( qp )
110
+ normalizeQueryParams ( params = { } ) {
111
+ let normalizedParams = schemaParser . replaceAliases ( params )
112
+
113
+ normalizedParams . fm = this . getAutoFormat ( ) || normalizedParams . fm || this . originalMetadata . format ;
114
+
115
+ console . log ( normalizedParams . fm ) ;
116
+ return normalizedParams
121
117
}
122
118
}
123
119
0 commit comments