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

feat: return Content-Type header with the mimeType of the file #1809

Merged
merged 6 commits into from
Sep 27, 2024
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
1 change: 1 addition & 0 deletions content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"bloom-filters": "^3.0.0",
"dcl-catalyst-client": "^21.7.0",
"eth-connect": "^6.2.4",
"file-type": "^15.0.1",
"form-data": "^4.0.0",
"fp-future": "^1.0.1",
"joi": "^17.9.2",
Expand Down
2 changes: 1 addition & 1 deletion content/src/controller/handlers/get-content-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getContentHandler(context: HandlerContextWithPath<'storage

return {
status: 200,
headers: createContentFileHeaders(content, hash),
headers: await createContentFileHeaders(content, hash),
body: context.request.method.toUpperCase() === 'GET' ? await content.asRawStream() : undefined
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function getEntityImageHandler(

return {
status: 200,
headers: createContentFileHeaders(content, hash),
headers: await createContentFileHeaders(content, hash),
body: context.request.method.toUpperCase() === 'GET' ? await content.asRawStream() : undefined
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function getEntityThumbnailHandler(

return {
status: 200,
headers: createContentFileHeaders(content, hash),
headers: await createContentFileHeaders(content, hash),
body: context.request.method.toUpperCase() === 'GET' ? await content.asRawStream() : undefined
}
}
10 changes: 8 additions & 2 deletions content/src/controller/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ContentItem } from '@dcl/catalyst-storage'
import { InvalidRequestError, Pagination } from '../types'
import { fromStream } from 'file-type'
import { Readable } from 'stream'

export function paginationObject(url: URL, maxPageSize: number = 1000): Pagination {
const pageSize = url.searchParams.has('pageSize') ? parseInt(url.searchParams.get('pageSize')!, 10) : 100
Expand Down Expand Up @@ -37,9 +39,13 @@ export function asEnumValue<T extends { [key: number]: string }>(
}
}

export function createContentFileHeaders(content: ContentItem, hashId: string): Record<string, string> {
export async function createContentFileHeaders(content: ContentItem, hashId: string): Promise<Record<string, string>> {
const stream: Readable = await content.asRawStream()
const mime = await fromStream(stream)
const mimeType = mime?.mime || 'application/octet-stream'

const headers: Record<string, string> = {
'Content-Type': 'application/octet-stream',
'Content-Type': mimeType,
ETag: JSON.stringify(hashId), // by spec, the ETag must be a double-quoted string
'Access-Control-Expose-Headers': 'ETag',
'Cache-Control': 'public,max-age=31536000,s-maxage=31536000,immutable'
Expand Down
4 changes: 2 additions & 2 deletions content/test/integration/controller/entity-metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('Integration - Get wearable image and thumbnail', () => {

for (const response of responses) {
expect(response.status).toEqual(200)
expect(response.headers.get('content-type')).toEqual('application/octet-stream')
expect(response.headers.get('content-type')).toEqual('image/png')
expect(response.headers.get('ETag')).toBeTruthy()
expect(response.headers.get('Cache-Control')).toBeTruthy()
}
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('Integration - Get wearable image and thumbnail', () => {

for (const response of responses) {
expect(response.status).toEqual(200)
expect(response.headers.get('content-type')).toEqual('application/octet-stream')
expect(response.headers.get('content-type')).toEqual('image/png')
expect(response.headers.get('ETag')).toBeTruthy()
expect(response.headers.get('Cache-Control')).toBeTruthy()
}
Expand Down
Loading
Loading