2023-02-15 20:58:07 -07:00
|
|
|
import React from "react";
|
2023-02-15 17:40:57 -07:00
|
|
|
import _ from "lodash";
|
|
|
|
|
import {Button as SButton, Image, Modal, ModalActions, ModalContent} from "semantic-ui-react";
|
|
|
|
|
|
|
|
|
|
function getMediaPathURL(path) {
|
|
|
|
|
return `/media/${encodeURIComponent(path['path'])}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDownloadPathURL(path) {
|
|
|
|
|
return `/download/${encodeURIComponent(path['path'])}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getEpubViewerURL(path) {
|
2023-02-15 20:58:07 -07:00
|
|
|
return `/epub.html?url=download/${encodeURIComponent(path['path'])}`;
|
2023-02-15 17:40:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getIframeModal(path) {
|
|
|
|
|
const url = getMediaPathURL(path);
|
|
|
|
|
return <ModalContent>
|
|
|
|
|
<div className='full-height'>
|
|
|
|
|
<iframe title='textModal' src={url}
|
|
|
|
|
style={{
|
|
|
|
|
height: '100%', width: '100%', border: 'none', position: 'absolute', top: 0,
|
|
|
|
|
}}/>
|
|
|
|
|
</div>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getImageModal(path) {
|
|
|
|
|
const url = getMediaPathURL(path);
|
|
|
|
|
return <ModalContent>
|
|
|
|
|
<a href={url}>
|
|
|
|
|
<Image src={url}/>
|
|
|
|
|
</a>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getEpubModal(path) {
|
|
|
|
|
const downloadURL = getDownloadPathURL(path);
|
|
|
|
|
const viewerUrl = `/epub.html?url=${downloadURL}`;
|
|
|
|
|
return <ModalContent>
|
|
|
|
|
<div className='full-height'>
|
|
|
|
|
<iframe title='textModal' src={viewerUrl}
|
|
|
|
|
style={{
|
|
|
|
|
height: '100%', width: '100%', border: 'none', position: 'absolute', top: 0,
|
|
|
|
|
}}/>
|
|
|
|
|
</div>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getVideoModal(path) {
|
|
|
|
|
const url = getMediaPathURL(path);
|
|
|
|
|
return <ModalContent>
|
|
|
|
|
<video controls
|
|
|
|
|
autoPlay={true}
|
|
|
|
|
id="player"
|
|
|
|
|
playsInline={true}
|
|
|
|
|
style={{maxWidth: '100%'}}
|
|
|
|
|
>
|
|
|
|
|
<source src={url} type="video/mp4"/>
|
|
|
|
|
</video>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 20:58:07 -07:00
|
|
|
export const FilePreviewContext = React.createContext({
|
|
|
|
|
previewPath: null, setPreviewPath: null, previewModal: null, setPreviewModal: null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function FilePreviewWrapper({children}) {
|
|
|
|
|
const [previewPath, setPreviewPath] = React.useState(null);
|
|
|
|
|
const [previewModal, setPreviewModal] = React.useState(null);
|
|
|
|
|
|
|
|
|
|
const value = {
|
|
|
|
|
previewPath, setPreviewPath, previewModal, setPreviewModal,
|
|
|
|
|
};
|
2023-02-15 17:40:57 -07:00
|
|
|
|
|
|
|
|
function setModalContent(content, url, downloadURL) {
|
2023-02-15 20:58:07 -07:00
|
|
|
setPreviewModal(<Modal closeIcon
|
|
|
|
|
size='fullscreen'
|
|
|
|
|
open={true}
|
|
|
|
|
onClose={() => setPreviewModal(null)}
|
2023-02-15 17:40:57 -07:00
|
|
|
>
|
|
|
|
|
{content}
|
|
|
|
|
<ModalActions>
|
|
|
|
|
{downloadURL &&
|
|
|
|
|
<SButton color='yellow' onClick={() => window.open(downloadURL)} floated='left'>Download</SButton>}
|
|
|
|
|
<SButton color='blue' onClick={() => window.open(url)}>Open</SButton>
|
2023-02-15 20:58:07 -07:00
|
|
|
<SButton onClick={() => setPreviewPath(null)}>Close</SButton>
|
2023-02-15 17:40:57 -07:00
|
|
|
</ModalActions>
|
|
|
|
|
</Modal>);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 20:58:07 -07:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
setPreviewModal(null);
|
|
|
|
|
if (previewPath && !_.isEmpty(previewPath)) {
|
|
|
|
|
const {mimetype, size} = previewPath;
|
|
|
|
|
console.debug(`useFilePreview path=${previewPath['path']} mimetype=${mimetype}`);
|
|
|
|
|
const url = getMediaPathURL(previewPath);
|
|
|
|
|
const downloadURL = getDownloadPathURL(previewPath);
|
2023-02-15 17:40:57 -07:00
|
|
|
if (mimetype.startsWith('text/') && size >= 10000) {
|
|
|
|
|
// Large text files should be downloaded.
|
|
|
|
|
window.open(downloadURL);
|
|
|
|
|
} else if (mimetype.startsWith('text/') || mimetype.startsWith('application/json')) {
|
2023-02-15 20:58:07 -07:00
|
|
|
setModalContent(getIframeModal(previewPath), url, downloadURL);
|
2023-02-15 17:40:57 -07:00
|
|
|
} else if (mimetype.startsWith('video/')) {
|
2023-02-15 20:58:07 -07:00
|
|
|
setModalContent(getVideoModal(previewPath), url, downloadURL);
|
2023-02-15 17:40:57 -07:00
|
|
|
} else if (mimetype.startsWith('application/epub')) {
|
2023-02-15 20:58:07 -07:00
|
|
|
const viewerURL = getEpubViewerURL(previewPath);
|
|
|
|
|
console.dir(previewPath);
|
|
|
|
|
console.log(viewerURL);
|
|
|
|
|
setModalContent(getEpubModal(previewPath), viewerURL, downloadURL);
|
2023-02-15 17:40:57 -07:00
|
|
|
} else if (mimetype.startsWith('image/')) {
|
2023-02-15 20:58:07 -07:00
|
|
|
setModalContent(getImageModal(previewPath), url);
|
2023-02-15 17:40:57 -07:00
|
|
|
} else {
|
|
|
|
|
// No special handler for this file type, just open it.
|
|
|
|
|
window.open(downloadURL);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-15 20:58:07 -07:00
|
|
|
}, [previewPath]);
|
2023-02-15 17:40:57 -07:00
|
|
|
|
2023-02-15 20:58:07 -07:00
|
|
|
return <FilePreviewContext.Provider value={value}>
|
|
|
|
|
{children}
|
|
|
|
|
{previewModal}
|
|
|
|
|
</FilePreviewContext.Provider>
|
2023-02-15 17:40:57 -07:00
|
|
|
}
|