Skip to content

Commit a6333ab

Browse files
authored
fix: use body instead of query params (#230)
1 parent 05824a7 commit a6333ab

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

src/http/routes/object/getSignedURL.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { FromSchema } from 'json-schema-to-ts'
33
import { createDefaultSchema } from '../../generic-routes'
44
import { AuthenticatedRequest } from '../../request'
55
import { ImageRenderer } from '../../../storage/renderer'
6-
import { transformationQueryString } from '../../schemas/transformations'
6+
import { getConfig } from '../../../config'
7+
import { transformationOptionsSchema } from '../../schemas/transformations'
78

89
const getSignedURLParamsSchema = {
910
type: 'object',
@@ -17,15 +18,14 @@ const getSignedURLBodySchema = {
1718
type: 'object',
1819
properties: {
1920
expiresIn: { type: 'integer', minimum: 1, examples: [60000] },
21+
transform: {
22+
type: 'object',
23+
properties: transformationOptionsSchema,
24+
},
2025
},
2126
required: ['expiresIn'],
2227
} as const
23-
const renderImageQuerySchema = {
24-
type: 'object',
25-
properties: {
26-
...transformationQueryString,
27-
},
28-
} as const
28+
2929
const successResponseSchema = {
3030
type: 'object',
3131
properties: {
@@ -41,16 +41,16 @@ const successResponseSchema = {
4141
interface getSignedURLRequestInterface extends AuthenticatedRequest {
4242
Params: FromSchema<typeof getSignedURLParamsSchema>
4343
Body: FromSchema<typeof getSignedURLBodySchema>
44-
Querystring: FromSchema<typeof renderImageQuerySchema>
4544
}
4645

46+
const { enableImageTransformation } = getConfig()
47+
4748
export default async function routes(fastify: FastifyInstance) {
4849
const summary = 'Generate a presigned url to retrieve an object'
4950

5051
const schema = createDefaultSchema(successResponseSchema, {
5152
body: getSignedURLBodySchema,
5253
params: getSignedURLParamsSchema,
53-
querystring: renderImageQuerySchema,
5454
summary,
5555
tags: ['object'],
5656
})
@@ -70,7 +70,9 @@ export default async function routes(fastify: FastifyInstance) {
7070
const signedURL = await request.storage
7171
.from(bucketName)
7272
.signObjectUrl(objectName, urlPath as string, expiresIn, {
73-
transformations: ImageRenderer.applyTransformation(request.query || {}).join(','),
73+
transformations: enableImageTransformation
74+
? ImageRenderer.applyTransformation(request.body.transform || {}).join(',')
75+
: '',
7476
})
7577

7678
return response.status(200).send({ signedURL })

src/http/routes/render/renderAuthenticatedImage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getConfig } from '../../../config'
22
import { FromSchema } from 'json-schema-to-ts'
33
import { FastifyInstance } from 'fastify'
44
import { ImageRenderer } from '../../../storage/renderer'
5-
import { transformationQueryString } from '../../schemas/transformations'
5+
import { transformationOptionsSchema } from '../../schemas/transformations'
66

77
const { globalS3Bucket } = getConfig()
88

@@ -18,7 +18,7 @@ const renderAuthenticatedImageParamsSchema = {
1818
const renderImageQuerySchema = {
1919
type: 'object',
2020
properties: {
21-
...transformationQueryString,
21+
...transformationOptionsSchema,
2222
download: { type: 'string' },
2323
},
2424
} as const

src/http/routes/render/renderPublicImage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getConfig } from '../../../config'
22
import { FromSchema } from 'json-schema-to-ts'
33
import { FastifyInstance } from 'fastify'
44
import { ImageRenderer } from '../../../storage/renderer'
5-
import { transformationQueryString } from '../../schemas/transformations'
5+
import { transformationOptionsSchema } from '../../schemas/transformations'
66

77
const { globalS3Bucket } = getConfig()
88

@@ -19,7 +19,7 @@ const renderPublicImageParamsSchema = {
1919
const renderImageQuerySchema = {
2020
type: 'object',
2121
properties: {
22-
...transformationQueryString,
22+
...transformationOptionsSchema,
2323
download: { type: 'string' },
2424
},
2525
} as const

src/http/schemas/transformations.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const transformationQueryString = {
1+
export const transformationOptionsSchema = {
22
height: { type: 'integer', examples: [100], minimum: 0 },
33
width: { type: 'integer', examples: [100], minimum: 0 },
44
resize: { type: 'string', enum: ['fill', 'fit', 'fill-down', 'force', 'auto'] },

src/test/render-routes.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ describe('image rendering routes', () => {
6969
it('will render a transformed image providing a signed url', async () => {
7070
const signURLResponse = await app().inject({
7171
method: 'POST',
72-
url: '/object/sign/bucket2/authenticated/casestudy.png?width=100&height=100',
72+
url: '/object/sign/bucket2/authenticated/casestudy.png',
7373
payload: {
7474
expiresIn: 60000,
75+
transform: {
76+
width: 100,
77+
height: 100,
78+
},
7579
},
7680
headers: {
7781
authorization: `Bearer ${process.env.SERVICE_KEY}`,

0 commit comments

Comments
 (0)