spacedrive/interface/util/pdfViewer.tsx
Vítor Vasconcellos 30e7c9d709
[ENG-528] QuickPreview isn't correctly handling errors for video/audio playback (#815)
* Centralize the file preview logic in `Thumb.tsx`

* Fix useEffect

* Fix Inspector thumb keeping internal state from previous selected files
 - Change video border to follow video aspect-ratio, just like Finder

* Restore memo to Thumb
 - Only add borders to video thumb, not the video itself

* Simplify and improve Thumb logic
 - Add A internal Thumbnail component
 - Rename main component to FileThumb to match mobile naming
 - Move getIcon utility function to assets/icons

* Add new `useCallbackToWatchResize` hook
 - Replace `ResizeObserver` with new resize hook in `Thumb`
 - Simplify and improve `useIsDark` hook by replacing `react-responsive` with direct usage of WebAPI `matchMedia`
 - Fix `Thumb` src not updating correctly
 - Fix video extension incorrectly showing when size <= 80 in `Thumb`

* Fix `Inspector` not updating thumb type
 - Remove superfluous `newThumb` from `getExplorerItemData`

* Remove superfluous `ThumSize`

* Forgot a `?`

* Fix incorrect className check in `Thumb`
 - Updated changed files to use the hooks root import

* Format

* Fix PDF preview compleatly breaking the application
 - Allow Linux to access both the spacedrive:// custom protocol and the workaround webserver
 - On Linux only use the webserver for audio and video, which don't work when requested through a custom protocol
 - Configure tauri IPC to allow API access to the spacedrive://localhost domain, to avoid PDF previews from breaking the security scope and rendering the application unusable
 - Configure CSP to allow the pdf plugin custom protocol used by webkit
 - Fix race condition between Thumb error handler and thumbType useEffect, by using replacing it with a useLayoutEffect
 - Improve Thumb's error handling
2023-05-18 03:31:15 +00:00

25 lines
1 KiB
TypeScript

/**
* Check if webview can display PDFs
* https://developer.mozilla.org/en-US/docs/Web/API/Navigator/pdfViewerEnabled
* https://developer.mozilla.org/en-US/docs/Web/API/Navigator/mimeTypes
* https://developer.mozilla.org/en-US/docs/Web/API/Navigator/plugins
*/
export const pdfViewerEnabled = () => {
// pdfViewerEnabled is quite new, Safari only started supporting it in march 2023
// https://caniuse.com/?search=pdfViewerEnabled
if ('pdfViewerEnabled' in navigator && navigator.pdfViewerEnabled) return true;
// This is deprecated, but should be supported on all browsers/webviews
// https://caniuse.com/mdn-api_navigator_mimetypes
if (navigator.mimeTypes) {
if ('application/pdf' in navigator.mimeTypes)
return !!(navigator.mimeTypes['application/pdf'] as null | MimeType)?.enabledPlugin;
if ('text/pdf' in navigator.mimeTypes)
return !!(navigator.mimeTypes['text/pdf'] as null | MimeType)?.enabledPlugin;
}
// Last ditch effort
// https://caniuse.com/mdn-api_navigator_plugins
return 'PDF Viewer' in navigator.plugins;
};