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

fix: Update code to account for params on end of asset url in s3 #1217

Merged
merged 1 commit into from
Feb 23, 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
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
Loading