mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 22:46:20 -05:00
Creating FilePreview for FileBrowser.
This commit is contained in:
parent
69e673e0b8
commit
a2e1cfb0eb
4 changed files with 130 additions and 56 deletions
|
|
@ -20,6 +20,7 @@ import _ from 'lodash';
|
|||
import {SortableTable} from "./SortableTable";
|
||||
import {useBrowseFiles} from "../hooks/customHooks";
|
||||
import {DirectoryRefreshButton, FilesRefreshButton} from "./Files";
|
||||
import {useFilePreview} from "./FilePreview";
|
||||
|
||||
function depthIndentation(path) {
|
||||
// Repeated spaces for every folder a path is in.
|
||||
|
|
@ -36,13 +37,13 @@ function Folder({folder, onFolderClick, sortData, selectedPath, onFileClick, onS
|
|||
<TableCell collapsing>
|
||||
<Checkbox checked={selectedPath === folder['path']} onChange={() => onSelect(folder['path'])}/>
|
||||
</TableCell>
|
||||
<TableCell onClick={() => onFolderClick(path)} className='file-path' colSpan={2}>
|
||||
<TableCell onClick={() => onFolderClick(path)} className='file-path' colSpan={2} disabled={is_empty}>
|
||||
{depthIndentation(pathWithNoTrailingSlash)}
|
||||
{is_empty ? <Icon name='folder outline'/> : <Icon name='folder'/>}
|
||||
{name}
|
||||
</TableCell>
|
||||
</TableRow>;
|
||||
if (children && !_.isEmpty(children)) {
|
||||
if (children) {
|
||||
// Folder has children, recursively display them.
|
||||
children = sortData(children);
|
||||
let childPaths = [];
|
||||
|
|
@ -112,51 +113,7 @@ export function FileBrowser() {
|
|||
key: 'path', text: 'Path', sortBy: i => i['path'].toLowerCase()
|
||||
}, {key: 'size', text: 'Size', sortBy: 'size'},];
|
||||
|
||||
const [modal, setModal] = React.useState(null);
|
||||
const onFileClick = (path) => {
|
||||
const {mimetype} = path;
|
||||
const url = `/media/${path['path']}`;
|
||||
let modalBody;
|
||||
if (mimetype === 'text/plain' || mimetype === 'text/html') {
|
||||
modalBody = <ModalContent>
|
||||
<div className='full-height'>
|
||||
<iframe title='textModal' src={url}
|
||||
style={{
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
border: 'none',
|
||||
position: 'absolute',
|
||||
top: 0,
|
||||
}}/>
|
||||
</div>
|
||||
</ModalContent>;
|
||||
} else if (mimetype.startsWith('image/')) {
|
||||
modalBody = <ModalContent>
|
||||
<a href={url}>
|
||||
<Image src={url}/>
|
||||
</a>
|
||||
</ModalContent>;
|
||||
}
|
||||
|
||||
if (modalBody) {
|
||||
// Create modal that will instantly display the file.
|
||||
setModal(<Modal closeIcon
|
||||
size='fullscreen'
|
||||
open={true}
|
||||
onClose={() => setModal(null)}
|
||||
>
|
||||
{modalBody}
|
||||
<ModalActions>
|
||||
<SButton color='blue' onClick={() => window.open(url)}>Open</SButton>
|
||||
<SButton onClick={() => setModal(null)}>Close</SButton>
|
||||
</ModalActions>
|
||||
</Modal>)
|
||||
} else {
|
||||
// No special handler for this file type, just open it.
|
||||
setModal(null);
|
||||
window.open(url);
|
||||
}
|
||||
}
|
||||
const {modal, setPath} = useFilePreview();
|
||||
|
||||
const onSelect = (path) => {
|
||||
if (path.endsWith('/') && selectedPath === path) {
|
||||
|
|
@ -241,7 +198,7 @@ export function FileBrowser() {
|
|||
key={i['key']}
|
||||
path={i}
|
||||
onFolderClick={onFolderClick}
|
||||
onFileClick={onFileClick}
|
||||
onFileClick={(i) => setPath(i)}
|
||||
sortData={sortData}
|
||||
selectedPath={selectedPath}
|
||||
onSelect={onSelect}
|
||||
|
|
|
|||
112
app/src/components/FilePreview.js
Normal file
112
app/src/components/FilePreview.js
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
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 <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>
|
||||
}
|
||||
|
||||
export function useFilePreview() {
|
||||
const [path, setPath] = React.useState(null);
|
||||
const [modal, setModal] = React.useState(null);
|
||||
|
||||
function setModalContent(content, url, downloadURL) {
|
||||
setModal(<Modal closeIcon
|
||||
size='fullscreen'
|
||||
open={true}
|
||||
onClose={() => setModal(null)}
|
||||
>
|
||||
{content}
|
||||
<ModalActions>
|
||||
{downloadURL &&
|
||||
<SButton color='yellow' onClick={() => window.open(downloadURL)} floated='left'>Download</SButton>}
|
||||
<SButton color='blue' onClick={() => window.open(url)}>Open</SButton>
|
||||
<SButton onClick={() => setPath(null)}>Close</SButton>
|
||||
</ModalActions>
|
||||
</Modal>);
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue