import React, {useEffect} from "react";
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) {
return `/epub.html?url=${getDownloadPathURL(path)}`;
}
function getIframeModal(path) {
const url = getMediaPathURL(path);
return
}
function getImageModal(path) {
const url = getMediaPathURL(path);
return
}
function getEpubModal(path) {
const downloadURL = getDownloadPathURL(path);
const viewerUrl = `/epub.html?url=${downloadURL}`;
return
}
function getVideoModal(path) {
const url = getMediaPathURL(path);
return
}
export function useFilePreview() {
const [path, setPath] = React.useState(null);
const [modal, setModal] = React.useState(null);
function setModalContent(content, url, downloadURL) {
setModal( setModal(null)}
>
{content}
{downloadURL &&
window.open(downloadURL)} floated='left'>Download}
window.open(url)}>Open
setPath(null)}>Close
);
}
useEffect(() => {
setModal(null);
if (path && !_.isEmpty(path)) {
const {mimetype, size} = path;
console.debug(`useFilePreview path=${path['path']} mimetype=${mimetype}`);
const url = getMediaPathURL(path);
const downloadURL = getDownloadPathURL(path);
if (mimetype.startsWith('text/') && size >= 10000) {
// Large text files should be downloaded.
window.open(downloadURL);
} else if (mimetype.startsWith('text/') || mimetype.startsWith('application/json')) {
setModalContent(getIframeModal(path), url, downloadURL);
} else if (mimetype.startsWith('video/')) {
setModalContent(getVideoModal(path), url, downloadURL);
} else if (mimetype.startsWith('application/epub')) {
const viewerURL = getEpubViewerURL(path);
setModalContent(getEpubModal(path), viewerURL, downloadURL);
} else if (mimetype.startsWith('image/')) {
setModalContent(getImageModal(path), url);
} else {
// No special handler for this file type, just open it.
window.open(downloadURL);
}
}
}, [path]);
return {modal, setPath};
}