Skip to content

Commit 95039ea

Browse files
author
Tim Kelty
committed
Fix auto format
1 parent 393832b commit 95039ea

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/ImageRequest.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,40 @@ class ImageRequest {
3131

3232
this.sharpObject = sharp(this.originalImageBody)
3333
this.originalMetadata = await this.sharpObject.metadata()
34-
35-
const qp = this._parseQueryParams()
36-
3734
this.headers = this.event.headers
3835

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)
4440
}
4541

46-
inferAutoFormat() {
47-
const specialOutputFormats = eventParser.getAcceptedImageFormatsFromHeaders(this.headers)
42+
getAutoFormat() {
4843
const coercibleFormats = ['jpg', 'png', 'webp', 'avif', 'jpeg', 'tiff']
44+
const autoParam = this.event.multiValueQueryStringParameters.auto
45+
const specialOutputFormats = eventParser.getAcceptedImageFormatsFromHeaders(this.headers)
4946

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
5253
}
5354

54-
// TODO: Ensure image is at least 16x16px for avif compatibility - this needs to happen down the line after schema parsing and validation
5555
if (specialOutputFormats.includes('avif')) {
5656
return 'avif'
5757
}
5858
// If avif isn't available, try to use webp
59-
if (specialOutputFormats.includes('webp')) {
59+
else if (specialOutputFormats.includes('webp')) {
6060
return 'webp'
6161
}
6262
// 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))) {
6464
return 'jpeg'
6565
}
66-
// There's no other coercion worth doing, use original format
67-
return this.originalMetadata.format
66+
67+
return null
6868
}
6969

7070
/**
@@ -107,17 +107,13 @@ class ImageRequest {
107107
return true
108108
}
109109

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
121117
}
122118
}
123119

0 commit comments

Comments
 (0)