-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support additional file types for opening docs in browser (#1214)
* support additional mime types * update tests for additional mime types * update open doc code for landing page
- Loading branch information
Showing
6 changed files
with
62 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,43 @@ | ||
import axios from 'axios' | ||
|
||
export const isPdf = (url: string) => { | ||
return url.toString().includes('.pdf') | ||
// Helper function to determine MIME type from the Keystone file URL | ||
export const mimeTypes: Record<string, string> = { | ||
pdf: 'application/pdf', | ||
jpg: 'image/jpeg', | ||
jpeg: 'image/jpeg', | ||
png: 'image/png', | ||
webp: 'image/webp', | ||
gif: 'image/gif', | ||
svg: 'image/svg+xml', | ||
webm: 'video/webm', | ||
mp4: 'video/mp4', | ||
mov: 'video/quicktime', | ||
mp3: 'audio/mpeg', | ||
wav: 'audio/wav', | ||
txt: 'text/plain', | ||
csv: 'text/csv', | ||
xls: 'application/vnd.ms-excel', | ||
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
// Add more mappings as needed | ||
} | ||
export const getMimeType = (url: string): string => { | ||
const extension = url.split('.').pop()?.toLowerCase() | ||
return mimeTypes[extension || ''] || 'application/octet-stream' // Default MIME type | ||
} | ||
// Function to open files in a new tab with TypeScript annotations | ||
export const openFileInNewTab = async (fileUrl: string): Promise<void> => { | ||
try { | ||
const response = await axios.get<Blob>(fileUrl, { responseType: 'blob' }) | ||
const mimeType = getMimeType(fileUrl) | ||
|
||
export const handleOpenPdfLink = async (pdfString: string) => { | ||
// Fetch the file from Keystone / S3 | ||
const res = await axios.get(pdfString, { responseType: 'blob' }) | ||
const file = new Blob([response.data], { type: mimeType }) | ||
const fileURL = URL.createObjectURL(file) | ||
|
||
// Create a blob from the file and open it in a new tab | ||
const blobData = await res.data | ||
const file = new Blob([blobData], { type: 'application/pdf' }) | ||
const fileUrl = URL.createObjectURL(file) | ||
// If the browser cannot open the file, it will download it automatically | ||
window.open(fileURL, '_blank') | ||
|
||
window.open(fileUrl) | ||
// Let the browser know not to keep the reference to the file any longer. | ||
URL.revokeObjectURL(fileUrl) | ||
URL.revokeObjectURL(fileURL) | ||
} catch (error) { | ||
console.error('Error opening file:', error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters