Skip to content

Commit

Permalink
fix: resize type on signed url (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos authored Mar 6, 2023
1 parent 6e4bfc1 commit 028d770
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/http/routes/bucket/createBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const createBucketBodySchema = {
name: { type: 'string', examples: ['avatars'] },
id: { type: 'string', examples: ['avatars'] },
public: { type: 'boolean', examples: [false] },
file_size_limit: { anyOf: [{ type: 'string' }, { type: 'integer' }] },
file_size_limit: { anyOf: [{ type: 'integer' }, { type: 'string' }] },
allowed_mime_types: { type: 'array', items: { type: 'string' } },
},
required: ['name'],
Expand Down
2 changes: 1 addition & 1 deletion src/http/routes/bucket/updateBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const updateBucketBodySchema = {
type: 'object',
properties: {
public: { type: 'boolean', examples: [false] },
file_size_limit: { anyOf: [{ type: 'string' }, { type: 'integer' }] },
file_size_limit: { anyOf: [{ type: 'integer' }, { type: 'string' }] },
allowed_mime_types: { type: 'array', items: { type: 'string' } },
},
} as const
Expand Down
7 changes: 4 additions & 3 deletions src/http/routes/object/getSignedURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ export default async function routes(fastify: FastifyInstance) {

const transformationOptions = imageTransformationEnabled
? {
transformations: ImageRenderer.applyTransformation(request.body.transform || {}).join(
','
),
transformations: ImageRenderer.applyTransformation(
request.body.transform || {},
true
).join(','),
format: request.body.transform?.format || '',
}
: undefined
Expand Down
95 changes: 50 additions & 45 deletions src/storage/renderer/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,56 @@ export class ImageRenderer extends Renderer {
this.client = client
}

/**
* Applies whitelisted transformations with specific limits applied
* @param options
* @param keepOriginal
*/
static applyTransformation(options: TransformOptions, keepOriginal?: boolean): string[] {
const segments = []

if (options.height) {
segments.push(`height:${clamp(options.height, LIMITS.height.min, LIMITS.height.max)}`)
}

if (options.width) {
segments.push(`width:${clamp(options.width, LIMITS.width.min, LIMITS.width.max)}`)
}

if (options.width || options.height) {
if (keepOriginal) {
segments.push(`resize:${options.resize}`)
} else {
segments.push(`resizing_type:${this.formatResizeType(options.resize)}`)
}
}

if (options.quality) {
segments.push(`quality:${options.quality}`)
}

if (options.format && options.format !== 'origin') {
segments.push(`format:${options.format}`)
}

return segments
}

protected static formatResizeType(resize: TransformOptions['resize']) {
const defaultResize = 'fill'

switch (resize) {
case 'cover':
return defaultResize
case 'contain':
return 'fit'
case 'fill':
return 'force'
default:
return defaultResize
}
}

/**
* Get the base http client
*/
Expand Down Expand Up @@ -188,51 +238,6 @@ export class ImageRenderer extends Renderer {
}
}

/**
* Applies whitelisted transformations with specific limits applied
* @param options
*/
static applyTransformation(options: TransformOptions): string[] {
const segments = []

if (options.height) {
segments.push(`height:${clamp(options.height, LIMITS.height.min, LIMITS.height.max)}`)
}

if (options.width) {
segments.push(`width:${clamp(options.width, LIMITS.width.min, LIMITS.width.max)}`)
}

if (options.width || options.height) {
segments.push(`resizing_type:${this.formatResizeType(options.resize)}`)
}

if (options.quality) {
segments.push(`quality:${options.quality}`)
}

if (options.format && options.format !== 'origin') {
segments.push(`format:${options.format}`)
}

return segments
}

protected static formatResizeType(resize: TransformOptions['resize']) {
const defaultResize = 'fill'

switch (resize) {
case 'cover':
return defaultResize
case 'contain':
return 'fit'
case 'fill':
return 'force'
default:
return defaultResize
}
}

protected async handleRequestError(error: AxiosError) {
const stream = error.response?.data as Stream
if (!stream) {
Expand Down

0 comments on commit 028d770

Please sign in to comment.