Skip to content

Commit

Permalink
fix: Update code to account for params on end of asset url in s3 (#1217)
Browse files Browse the repository at this point in the history
fix code to account for query params on end of asset url in s3
  • Loading branch information
abbyoung authored Feb 23, 2024
1 parent ceb2cf1 commit abb88ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/helpers/openDocumentLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ URL.revokeObjectURL = mockRevokeObjectURL
const mockWindowOpen = jest.fn()
window.open = mockWindowOpen

describe('isPdf', () => {
describe('openDocumentLink', () => {
Object.entries(mimeTypes).forEach(([extension, type]) => {
test('returns correct type if url ends with extension', () => {
const fileName = `https://www.google.com/test.${extension}`
test('returns correct type if url is hosted on s3', () => {
const fileName = `https://www.google.com/test.${extension}?queryparamsands3things`
expect(getMimeType(fileName)).toBe(type)
})
})
Expand All @@ -31,11 +31,17 @@ describe('isPdf', () => {
'application/octet-stream'
)
})

test('returns correct type if url ends with extension', () => {
expect(getMimeType('https://www.google.com/test.pdf')).toBe(
'application/pdf'
)
})
})

describe('handleOpenPdfLink', () => {
test('opens a new window if the url is a pdf', async () => {
const pdfString = 'https://www.google.com/test.pdf'
const pdfString = 'https://www.google.com/test.pdf?queryparamsands3things'
await openFileInNewTab(pdfString)
expect(axios.get).toHaveBeenCalled()
expect(mockCreateObjectURL).toHaveBeenCalled()
Expand Down
16 changes: 15 additions & 1 deletion src/helpers/openDocumentLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@ export const mimeTypes: Record<string, string> = {
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
// Add more mappings as needed
}

export const extractExtensionFromUrl = (url: string): string | null => {
// Remove query parameters if there are any
const baseUrl = url.split('?')[0].toLowerCase()
const extensions = Object.keys(mimeTypes)

for (const extension of extensions) {
if (baseUrl.endsWith(`.${extension}`)) {
return extension
}
}
return null
}

export const getMimeType = (url: string): string => {
const extension = url.split('.').pop()?.toLowerCase()
const extension = extractExtensionFromUrl(url)
return mimeTypes[extension || ''] || 'application/octet-stream' // Default MIME type
}
// Function to open files in a new tab with TypeScript annotations
Expand Down

0 comments on commit abb88ac

Please sign in to comment.