mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 03:16:22 -05:00
Adding preview URL query which allows sharing of a specific file.
This commit is contained in:
parent
6409e0ffde
commit
1bb333e43b
7 changed files with 89 additions and 4 deletions
|
|
@ -26,6 +26,7 @@ import {SearchView, useSearch, useSearchSuggestions} from "./components/Search";
|
|||
import {KiwixRestartMessage, OutdatedZimsMessage} from "./components/Zim";
|
||||
import {useSettings, useWROLMode} from "./hooks/customHooks";
|
||||
import {FileSearchFilterButton} from "./components/Files";
|
||||
import {FilePreviewContext} from "./components/FilePreview";
|
||||
|
||||
function FlagsMessages({flags}) {
|
||||
const {settings, fetchSettings} = useSettings();
|
||||
|
|
@ -172,6 +173,8 @@ export function DashboardPage() {
|
|||
} = useSearchSuggestions(searchStr, activeTags);
|
||||
const {status} = useContext(StatusContext);
|
||||
|
||||
const {setPreviewFile} = React.useContext(FilePreviewContext);
|
||||
|
||||
React.useEffect(() => {
|
||||
setSuggestionSearchStr(localSearchStr);
|
||||
}, [localSearchStr]);
|
||||
|
|
|
|||
|
|
@ -593,6 +593,17 @@ export async function getFiles(directories) {
|
|||
return files;
|
||||
}
|
||||
|
||||
export async function getFile(path) {
|
||||
let body = {file: path};
|
||||
let response = await apiPost(`${API_URI}/files/file`, body);
|
||||
if (response.ok) {
|
||||
const {file} = await response.json();
|
||||
return file;
|
||||
} else {
|
||||
throw new Error('Failed to get file data');
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteFile(paths) {
|
||||
let body = {paths: paths};
|
||||
const response = await apiPost(`${API_URI}/files/delete`, body);
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ import {Grid, Header as SHeader, Image,} from "semantic-ui-react";
|
|||
import {TagsSelector} from "../Tags";
|
||||
import {Media} from "../contexts/contexts";
|
||||
import {encodeMediaPath} from "./Common";
|
||||
import {fetchFile, tagFileGroup, untagFileGroup} from "../api";
|
||||
import {fetchFile, getFile, tagFileGroup, untagFileGroup} from "../api";
|
||||
import {StlViewer} from "react-stl-viewer";
|
||||
import {Button, Modal, ModalActions, ModalContent, ModalHeader} from "./Theme";
|
||||
import {toast} from "react-semantic-toasts-2";
|
||||
import {useOneQuery} from "../hooks/customHooks";
|
||||
import {ShareButton} from "./Share";
|
||||
|
||||
function getMediaPathURL(previewFile) {
|
||||
if (previewFile['primary_path']) {
|
||||
|
|
@ -143,9 +145,56 @@ export const FilePreviewContext = React.createContext({
|
|||
const MAXIMUM_TEXT_SIZE = 5_000_000;
|
||||
|
||||
export function FilePreviewProvider({children}) {
|
||||
// Data about the file that is being previewed.
|
||||
const [previewFile, setPreviewFile] = React.useState(null);
|
||||
// A modal is only displayed once we have data about the file.
|
||||
const [previewModal, setPreviewModal] = React.useState(null);
|
||||
const [callbacks, setCallbacks] = React.useState(null);
|
||||
// The file path from the `preview` URL query.
|
||||
const [previewQuery, setPreviewQuery] = useOneQuery('preview');
|
||||
const [errorModalOpen, setErrorModalOpen] = React.useState(false);
|
||||
|
||||
const handleErrorModalClose = () => {
|
||||
setPreviewQuery(null);
|
||||
setErrorModalOpen(false);
|
||||
}
|
||||
|
||||
const errorModal = <Modal closeIcon
|
||||
open={errorModalOpen}
|
||||
onClose={handleErrorModalClose}
|
||||
>
|
||||
<ModalHeader>Unknown File</ModalHeader>
|
||||
<ModalContent>Cannot display preview, no such file exists: {previewQuery}</ModalContent>
|
||||
<ModalActions>
|
||||
<Button onClick={handleErrorModalClose}>Close</Button>
|
||||
</ModalActions>
|
||||
</Modal>
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!_.isEmpty(previewFile)) {
|
||||
// Change URL to match the file that is being previewed.
|
||||
setPreviewQuery(previewFile['primary_path'] || previewFile['path']);
|
||||
}
|
||||
}, [previewFile]);
|
||||
|
||||
const initPreviewFile = async () => {
|
||||
// Get simple information about the file, preview the file.
|
||||
try {
|
||||
const file = await getFile(previewQuery);
|
||||
setPreviewFile(file);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
setPreviewModal(errorModal);
|
||||
setErrorModalOpen(true);
|
||||
}
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
// Navigated to a page which has a file preview active, fetch the information about the file, then display it.
|
||||
if (previewQuery && !previewFile) {
|
||||
initPreviewFile();
|
||||
}
|
||||
}, []);
|
||||
|
||||
const localFetchFile = async () => {
|
||||
// Get the file again with its Tags.
|
||||
|
|
@ -164,6 +213,7 @@ export function FilePreviewProvider({children}) {
|
|||
}
|
||||
setPreviewModal(null);
|
||||
setPreviewFile(null);
|
||||
setPreviewQuery(null);
|
||||
}
|
||||
|
||||
const handleCallbacks = async () => {
|
||||
|
|
@ -220,7 +270,8 @@ export function FilePreviewProvider({children}) {
|
|||
<Grid.Column width={5}>{closeButton}</Grid.Column>
|
||||
</Grid.Row>
|
||||
<Grid.Row>
|
||||
<Grid.Column width={16}>{tagsDisplay}</Grid.Column>
|
||||
<Grid.Column width={2} style={{paddingTop: '0.5em'}}><ShareButton/></Grid.Column>
|
||||
<Grid.Column width={14}>{tagsDisplay}</Grid.Column>
|
||||
</Grid.Row>
|
||||
</Grid>
|
||||
</Media>
|
||||
|
|
@ -228,7 +279,8 @@ export function FilePreviewProvider({children}) {
|
|||
<Grid>
|
||||
<Grid.Row>
|
||||
<Grid.Column width={2}>{downloadButton}</Grid.Column>
|
||||
<Grid.Column width={10}>{tagsDisplay}</Grid.Column>
|
||||
<Grid.Column width={1} style={{paddingTop: '0.5em'}}><ShareButton/></Grid.Column>
|
||||
<Grid.Column width={9}>{tagsDisplay}</Grid.Column>
|
||||
<Grid.Column width={2}>{openButton}</Grid.Column>
|
||||
<Grid.Column width={2}>{closeButton}</Grid.Column>
|
||||
</Grid.Row>
|
||||
|
|
@ -292,5 +344,6 @@ export function FilePreviewProvider({children}) {
|
|||
return <FilePreviewContext.Provider value={value}>
|
||||
{children}
|
||||
{previewModal}
|
||||
{errorModal}
|
||||
</FilePreviewContext.Provider>
|
||||
}
|
||||
|
|
|
|||
|
|
@ -249,6 +249,7 @@ export function useSearchSuggestions(defaultSearchStr, defaultTagNames) {
|
|||
// User clicked on a result in the dropdown.
|
||||
const handleResultSelect = ({result}) => {
|
||||
if (result.location) {
|
||||
console.info(`useSearchSuggestions Navigating: ${result.location}`)
|
||||
navigate(result.location);
|
||||
} else {
|
||||
console.error('No location to navigate');
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import {
|
|||
getChannels,
|
||||
getDirectories,
|
||||
getDownloads,
|
||||
getFile,
|
||||
getFiles,
|
||||
getInventory,
|
||||
getOutdatedZims,
|
||||
|
|
@ -109,6 +110,7 @@ export const useQuery = () => {
|
|||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
|
||||
const setQuery = (obj, replace = true) => {
|
||||
console.debug(`setQuery`, obj);
|
||||
setSearchParams(createSearchParams(obj), {replace: replace});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,7 +41,10 @@ async def get_files(_: Request, body: schema.FilesRequest):
|
|||
)
|
||||
@validate(schema.FileRequest)
|
||||
async def get_file(_: Request, body: schema.FileRequest):
|
||||
file = lib.get_file_dict(body.file)
|
||||
try:
|
||||
file = lib.get_file_dict(body.file)
|
||||
except FileNotFoundError:
|
||||
raise InvalidFile()
|
||||
return json_response({'file': file})
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -587,3 +587,15 @@ async def test_delete_directory_recursive(test_session, test_directory, make_fil
|
|||
empty = test_session.query(FileGroup).one()
|
||||
assert empty.primary_path.name == 'empty'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_file(test_session, test_async_client, test_directory, make_files_structure):
|
||||
"""Can get info about a single file."""
|
||||
make_files_structure({'foo/bar.txt': 'foo contents'})
|
||||
await lib.refresh_files()
|
||||
|
||||
content = dict(file='foo/bar.txt')
|
||||
request, response = await test_async_client.post('/api/files/file', content=json.dumps(content))
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert response.json['file']
|
||||
assert response.json['file']['path'] == 'foo/bar.txt'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue