Skip to content

Commit

Permalink
feat: return Content-Type header with the mimeType of the file (#1809)
Browse files Browse the repository at this point in the history
* feat: return Content-Type header with the mimeType of the file 
* chore: specify file-type version on package.json

---------

Co-authored-by: Alejo Thomas Ortega <[email protected]>
  • Loading branch information
pentreathm and aleortega authored Sep 27, 2024
1 parent a78332b commit ca76508
Show file tree
Hide file tree
Showing 7 changed files with 993 additions and 940 deletions.
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

0 comments on commit ca76508

Please sign in to comment.