import React from "react";
import _ from "lodash";
import {Grid, Header as SHeader, Image,} from "semantic-ui-react";
import {TagsProvider, TagsSelector} from "../Tags";
import {Media} from "../contexts/contexts";
import {encodeMediaPath} from "./Common";
import {addTag, fetchFile, removeTag} from "../api";
import {StlViewer} from "react-stl-viewer";
import {Button, Modal, ModalActions, ModalContent, ModalHeader} from "./Theme";
function getMediaPathURL(previewFile) {
if (previewFile['primary_path']) {
return `/media/${encodeMediaPath(previewFile['primary_path'])}`;
} else {
return `/media/${encodeMediaPath(previewFile['path'])}`;
}
}
function getDownloadPathURL(previewFile) {
if (previewFile['primary_path']) {
return `/download/${encodeMediaPath(previewFile['primary_path'])}`;
} else {
return `/download/${encodeMediaPath(previewFile['path'])}`;
}
}
function getEpubViewerURL(previewFile) {
if (previewFile['primary_path']) {
return `/epub.html?url=download/${encodeMediaPath(previewFile['primary_path'])}`;
} else {
return `/epub.html?url=download/${encodeMediaPath(previewFile['path'])}`;
}
}
function getIframeModal(previewFile) {
const url = getMediaPathURL(previewFile);
return
}
function getImageModal(previewFile) {
const path = previewFile.primary_path ?? previewFile.path;
const url = getMediaPathURL(previewFile);
// Get the file name from the absolute path.
const name = path.replace(/^.*[\\\/]/, '');
return
{name}
}
function getEpubModal(previewFile) {
const downloadURL = getDownloadPathURL(previewFile);
const viewerUrl = `/epub.html?url=${downloadURL}`;
return
}
function getVideoModal(previewFile) {
const url = getMediaPathURL(previewFile);
const path = previewFile.primary_path ?? previewFile.path;
const name = path.replace(/^.*[\\\/]/, '');
const type = previewFile.mimetype ? previewFile.mimetype : 'video/mp4';
return
{name}
{path['path']}
}
function getAudioModal(previewFile) {
const url = getMediaPathURL(previewFile);
const path = previewFile.primary_path ?? previewFile.path;
const name = path.replace(/^.*[\\\/]/, '');
// Some audio files may not have the correct mimetype. Trust the suffix identifies an audio file.
const type = previewFile['mimetype'] !== 'application/octet-stream' ? previewFile['mimetype'] : 'audio/mpeg';
return
{name}
}
function getSTLModal(previewFile) {
const url = getMediaPathURL(previewFile);
const path = previewFile.primary_path ?? previewFile.path;
const name = path.replace(/^.*[\\\/]/, '');
return
{name}
}
export const FilePreviewContext = React.createContext({
previewFile: null,
setPreviewFile: null,
previewModal: null,
setPreviewModal: null,
setCallbacks: null,
});
const MAXIMUM_TEXT_SIZE = 5_000_000;
export function FilePreviewProvider({children}) {
const [previewFile, setPreviewFile] = React.useState(null);
const [previewModal, setPreviewModal] = React.useState(null);
const [callbacks, setCallbacks] = React.useState(null);
const localFetchFile = async () => {
// Get the file again with its Tags.
const {path, primary_path} = previewFile;
try {
const file = await fetchFile(primary_path ?? path);
setPreviewFile(file);
} finally {
await handleCallbacks();
}
};
const handleClose = (e) => {
if (e) {
e.preventDefault();
}
setPreviewModal(null);
setPreviewFile(null);
}
const handleCallbacks = async () => {
for (const callbacksKey in callbacks) {
try {
await callbacks[callbacksKey]();
} catch (e) {
console.error(e);
}
}
}
const localAddTag = async (name) => {
await addTag(previewFile, name);
await localFetchFile();
}
const localRemoveTag = async (name) => {
await removeTag(previewFile, name);
await localFetchFile();
}
function setModalContent(content, url, downloadURL) {
let openButton;
if (url) {
openButton = ;
}
const closeButton = ;
const tagsDisplay = ;
let downloadButton;
if (downloadURL) {
downloadButton = ;
}
setPreviewModal( handleClose(e)}
>
{content}
{downloadButton}
{openButton}
{closeButton}
{tagsDisplay}
{downloadButton}
{tagsDisplay}
{openButton}
{closeButton}
);
}
React.useEffect(() => {
setPreviewModal(null);
if (previewFile && !_.isObject(previewFile)) {
console.error(`Unknown previewFile type: ${typeof previewFile}`);
return;
}
if (previewFile && !_.isEmpty(previewFile)) {
const {mimetype, size} = previewFile;
const path = previewFile['primary_path'] || previewFile['path'];
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('application/pdf')) {
setModalContent(getIframeModal(previewFile), url, downloadURL);
} else if (mimetype.startsWith('image/')) {
setModalContent(getImageModal(previewFile), url);
} else if (mimetype.startsWith('model/stl')) {
setModalContent(getSTLModal(previewFile), null, downloadURL);
} else if (mimetype.startsWith('application/octet-stream') && lowerPath.endsWith('.mp3')) {
setModalContent(getAudioModal(previewFile), url, downloadURL);
} else if (mimetype.startsWith('application/octet-stream') && lowerPath.endsWith('.stl')) {
setModalContent(getSTLModal(previewFile), null, downloadURL);
} else {
// No special handler for this file type, just open it.
window.open(downloadURL);
}
}
}, [previewFile]);
const value = {previewFile, setPreviewFile, previewModal, setPreviewModal, setCallbacks};
return
{children}
{previewModal}
}