Skip to content

Commit e29c5d1

Browse files
authored
fix: image resizing improvements (#204)
1 parent 11f9493 commit e29c5d1

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/backend/s3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,6 @@ export class S3Backend implements GenericStorageBackend {
152152
}
153153

154154
const command = new GetObjectCommand(input)
155-
return getSignedUrl(this.client, command, { expiresIn: 3600 })
155+
return getSignedUrl(this.client, command, { expiresIn: 300 })
156156
}
157157
}

src/renderer/imgproxy.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,21 @@ export class Imgproxy {
4646
const privateURL = await this.backend.privateAssetUrl(bucket, key)
4747
const urlTransformation = this.applyURLTransformation(options)
4848

49-
const url = ['/public', ...urlTransformation, 'plain', privateURL]
50-
51-
return this.getClient().get(url.join('/'), {
49+
const url = [
50+
'/public',
51+
...urlTransformation,
52+
'plain',
53+
privateURL.startsWith('local://') ? privateURL : encodeURIComponent(privateURL),
54+
]
55+
56+
const response = await this.getClient().get(url.join('/'), {
5257
responseType: 'stream',
5358
})
59+
60+
return {
61+
response,
62+
urlTransformation,
63+
}
5464
}
5565

5666
protected applyURLTransformation(options: TransformOptions) {

src/routes/render/renderAuthenticatedImage.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ export default async function routes(fastify: FastifyInstance) {
8080
const s3Key = `${request.tenantId}/${bucketName}/${objectName}`
8181

8282
try {
83-
const imageResponse = await imageRenderer.transform(globalS3Bucket, s3Key, request.query)
83+
const { response: imageResponse, urlTransformation } = await imageRenderer.transform(
84+
globalS3Bucket,
85+
s3Key,
86+
request.query
87+
)
8488

8589
response
8690
.status(imageResponse.status)
@@ -89,6 +93,7 @@ export default async function routes(fastify: FastifyInstance) {
8993
.header('Cache-Control', imageResponse.headers['cache-control'])
9094
.header('Content-Length', imageResponse.headers['content-length'])
9195
.header('ETag', imageResponse.headers['etag'])
96+
.header('X-Transformation', urlTransformation.concat(','))
9297

9398
return response.send(imageResponse.data)
9499
} catch (err: any) {

src/routes/render/renderPublicImage.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { FastifyInstance } from 'fastify'
77
import { Bucket } from '../../types/types'
88
import { normalizeContentType, transformPostgrestError } from '../../utils'
99
import { Imgproxy } from '../../renderer/imgproxy'
10-
import { AxiosError } from 'axios'
1110

1211
const { region, globalS3Bucket, globalS3Endpoint, storageBackendType, imgProxyURL } = getConfig()
1312

@@ -77,7 +76,11 @@ export default async function routes(fastify: FastifyInstance) {
7776
const s3Key = `${request.tenantId}/${bucketName}/${objectName}`
7877

7978
try {
80-
const imageResponse = await imageRenderer.transform(globalS3Bucket, s3Key, request.query)
79+
const { response: imageResponse, urlTransformation } = await imageRenderer.transform(
80+
globalS3Bucket,
81+
s3Key,
82+
request.query
83+
)
8184

8285
response
8386
.status(imageResponse.status)
@@ -86,6 +89,7 @@ export default async function routes(fastify: FastifyInstance) {
8689
.header('Cache-Control', imageResponse.headers['cache-control'])
8790
.header('Content-Length', imageResponse.headers['content-length'])
8891
.header('ETag', imageResponse.headers['etag'])
92+
.header('X-Transformations', urlTransformation.concat(','))
8993

9094
return response.send(imageResponse.data)
9195
} catch (err: any) {

0 commit comments

Comments
 (0)