import React from "react";
import _ from "lodash";
import {
Button as SButton,
Header as SHeader,
Image,
Modal,
ModalActions,
ModalContent,
ModalHeader
} from "semantic-ui-react";
function getMediaPathURL(path) {
return `/media/${encodeURIComponent(path['path'])}`;
}
function getDownloadPathURL(path) {
return `/download/${encodeURIComponent(path['path'])}`;
}
function getEpubViewerURL(path) {
return `/epub.html?url=download/${encodeURIComponent(path['path'])}`;
}
function getIframeModal(path) {
const url = getMediaPathURL(path);
return
}
function getImageModal(path) {
const url = getMediaPathURL(path);
const name = path['path'].replace(/^.*[\\\/]/, '');
return
{name}
}
function getEpubModal(path) {
const downloadURL = getDownloadPathURL(path);
const viewerUrl = `/epub.html?url=${downloadURL}`;
return
}
function getVideoModal(path) {
const url = getMediaPathURL(path);
const name = path['path'].replace(/^.*[\\\/]/, '');
const type = path['mimetype'] ? path['mimetype'] : 'video/mp4';
return
{name}
{path['path']}
}
function getAudioModal(path) {
const url = getMediaPathURL(path);
const name = path['path'].replace(/^.*[\\\/]/, '');
// Some audio files may not have the correct mimetype. Trust the suffix identifies an audio file.
const type = path['mimetype'] !== 'application/octet-stream' ? path['mimetype'] : 'audio/mpeg';
return
{name}
}
export const FilePreviewContext = React.createContext({
previewFile: null, setPreviewFile: null, previewModal: null, setPreviewModal: null,
});
const MAXIMUM_TEXT_SIZE = 5_000_000;
export function FilePreviewWrapper({children}) {
const [previewFile, setPreviewFile] = React.useState(null);
const [previewModal, setPreviewModal] = React.useState(null);
const value = {
previewFile, setPreviewFile, previewModal, setPreviewModal,
};
function setModalContent(content, url, downloadURL) {
setPreviewModal( setPreviewModal(null)}
>
{content}
{downloadURL &&
window.open(downloadURL)} floated='left'>Download}
window.open(url)}>Open
setPreviewFile(null)}>Close
);
}
React.useEffect(() => {
setPreviewModal(null);
if (previewFile && !_.isObject(previewFile)) {
console.error(`Unknown previewFile type: ${typeof previewFile}`);
return;
}
if (previewFile && !_.isEmpty(previewFile)) {
const {mimetype, size, path} = previewFile;
const lowerPath = path.toLowerCase();
console.debug(`useFilePreview path=${previewFile['path']} mimetype=${mimetype}`);
const url = getMediaPathURL(previewFile);
const downloadURL = getDownloadPathURL(previewFile);
if (mimetype.startsWith('text/') && size > MAXIMUM_TEXT_SIZE) {
// Large text files should be downloaded.
window.open(downloadURL);
} else if (mimetype.startsWith('text/') || mimetype.startsWith('application/json')) {
setModalContent(getIframeModal(previewFile), url, downloadURL);
} else if (mimetype.startsWith('video/')) {
setModalContent(getVideoModal(previewFile), url, downloadURL);
} else if (mimetype.startsWith('audio/')) {
setModalContent(getAudioModal(previewFile), url, downloadURL);
} else if (mimetype.startsWith('application/epub')) {
const viewerURL = getEpubViewerURL(previewFile);
setModalContent(getEpubModal(previewFile), viewerURL, downloadURL);
} else if (mimetype.startsWith('image/')) {
setModalContent(getImageModal(previewFile), url);
} else if (mimetype.startsWith('application/octet-stream') && lowerPath.endsWith('.mp3')) {
setModalContent(getAudioModal(previewFile), url, downloadURL);
} else {
// No special handler for this file type, just open it.
window.open(downloadURL);
}
}
}, [previewFile]);
return
{children}
{previewModal}
}