Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resize type on signed url #285

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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