Creating new /api/archive/upload which accepts a file from the SingleFile browser extension and converts it to an Archive.

This commit is contained in:
Roland 2025-01-26 12:44:16 -07:00
parent 01338e4d89
commit 531ec3d65e
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
62 changed files with 1000 additions and 562 deletions

View file

@ -1,4 +1,11 @@
import {ErrorMessage, LoadStatistic, PageContainer, SearchResultsInput, useTitle} from "./components/Common";
import {
ErrorMessage,
LoadStatistic,
PageContainer,
RefreshHeader,
SearchResultsInput,
useTitle
} from "./components/Common";
import React, {useContext, useState} from "react";
import {Media, SettingsContext, StatusContext} from "./contexts/contexts";
import {DownloadMenu} from "./components/Download";
@ -25,8 +32,8 @@ import {TagsDashboard} from "./Tags";
import {Upload} from "./components/Upload";
import {SearchView, useSearch, useSearchSuggestions} from "./components/Search";
import {KiwixRestartMessage, OutdatedZimsMessage} from "./components/Zim";
import {useSearchFilter, useWROLMode} from "./hooks/customHooks";
import {FileSearchFilterButton} from "./components/Files";
import {useSearchFilter, useSearchRecentFiles, useWROLMode} from "./hooks/customHooks";
import {FileCards, FileSearchFilterButton} from "./components/Files";
import {DateSelectorButton} from "./components/DatesSelector";
import {useCalculators} from "./components/Calculators";
import {evaluate} from "mathjs";
@ -234,6 +241,72 @@ export function Getters() {
</>
}
function DashboardStatus() {
const {status} = useContext(StatusContext);
let percent = 0;
let load = {};
let cores = 0;
let pending_downloads = '?';
if (status && status['cpu_stats']) {
percent = status['cpu_stats']['percent'];
load = status['load_stats'];
cores = status['cpu_stats']['cores'];
}
const {downloads} = status;
if (!_.isEmpty(downloads)) {
pending_downloads = downloads && downloads['disabled'] ? 'x' : downloads['pending'];
}
let bandwidths = <ProgressPlaceholder/>;
if (status && status['nic_bandwidth_stats']) {
bandwidths = Object.entries(status['nic_bandwidth_stats'])
.map(([name, bandwidth]) => <BandwidthProgressCombined key={name} bandwidth={bandwidth}/>);
} else if (window.apiDown) { // apiDown is set in useStatus
bandwidths = null;
}
return <Segment>
<Link to='/admin/status'>
<Header as='h2'>Status</Header>
<CPUUsageProgress value={percent} label='CPU Usage'/>
<StatisticGroup size='mini'>
<LoadStatistic label='1 Min. Load' value={load['minute_1']} cores={cores}/>
<LoadStatistic label='5 Min. Load' value={load['minute_5']} cores={cores}/>
<LoadStatistic label='15 Min. Load' value={load['minute_15']} cores={cores}/>
</StatisticGroup>
<Header as='h3'>Bandwidth</Header>
{bandwidths}
</Link>
<Divider style={{marginTop: '3em'}}/>
<Link to='/admin'>
<StatisticGroup size='mini'>
<Statistic label='Downloading' value={pending_downloads}/>
</StatisticGroup>
</Link>
</Segment>;
}
function DashboardRecentFiles() {
const {searchFiles, loading, fetchFiles} = useSearchRecentFiles();
return <Segment>
<RefreshHeader
header='Recently Viewed Files'
popupContents='Fetch the most recent files again'
onRefresh={fetchFiles}
/>
<FileCards files={searchFiles} loading={loading}/>
</Segment>
}
export function DashboardPage() {
const navigate = useNavigate();
@ -284,6 +357,7 @@ export function DashboardPage() {
<TagsDashboard/>
<DashboardStatus/>
<DashboardCalculators/>
<DashboardRecentFiles/>
</React.Fragment>;
if (searchStr || (activeTags && activeTags.length > 0)) {
// User has submitted and wants full search.
@ -370,56 +444,4 @@ export function DashboardPage() {
{!searchStr && <FlagsMessages/>}
{body}
</PageContainer>
}
function DashboardStatus() {
const {status} = useContext(StatusContext);
let percent = 0;
let load = {};
let cores = 0;
let pending_downloads = '?';
if (status && status['cpu_stats']) {
percent = status['cpu_stats']['percent'];
load = status['load_stats'];
cores = status['cpu_stats']['cores'];
}
const {downloads} = status;
if (!_.isEmpty(downloads)) {
pending_downloads = downloads && downloads['disabled'] ? 'x' : downloads['pending'];
}
let bandwidths = <ProgressPlaceholder/>;
if (status && status['nic_bandwidth_stats']) {
bandwidths = Object.entries(status['nic_bandwidth_stats'])
.map(([name, bandwidth]) => <BandwidthProgressCombined key={name} bandwidth={bandwidth}/>);
} else if (window.apiDown) { // apiDown is set in useStatus
bandwidths = null;
}
return <Segment>
<Link to='/admin/status'>
<Header as='h2'>Status</Header>
<CPUUsageProgress value={percent} label='CPU Usage'/>
<StatisticGroup size='mini'>
<LoadStatistic label='1 Min. Load' value={load['minute_1']} cores={cores}/>
<LoadStatistic label='5 Min. Load' value={load['minute_5']} cores={cores}/>
<LoadStatistic label='15 Min. Load' value={load['minute_15']} cores={cores}/>
</StatisticGroup>
<Header as='h3'>Bandwidth</Header>
{bandwidths}
</Link>
<Divider style={{marginTop: '3em'}}/>
<Link to='/admin'>
<StatisticGroup size='mini'>
<Statistic label='Downloading' value={pending_downloads}/>
</StatisticGroup>
</Link>
</Segment>;
}
}

View file

@ -92,6 +92,15 @@ function handleEvents(events) {
eventToast('Config Save Failed', message, 'error');
}
if (event === 'upload_archive') {
eventToast(
'Archive Uploaded',
message,
'success',
5000,
() => window.open(url, '_self'));
}
if (subject) {
newestEvents[subject] = dt;
}

View file

@ -222,6 +222,22 @@ export async function getVideo(video_id) {
return [data['file_group'], data['prev'], data['next']];
}
export async function getVideoComments(videoId) {
const response = await apiGet(`${VIDEOS_API}/video/${videoId}/comments`);
let data = await response.json();
return {
comments: data.comments,
}
}
export async function getVideoCaptions(videoId) {
const response = await apiGet(`${VIDEOS_API}/video/${videoId}/captions`);
let data = await response.json();
return {
captions: data.captions,
}
}
export async function deleteVideos(videoIds) {
if (!videoIds || videoIds.length === 0) {
toast({
@ -827,7 +843,7 @@ export async function deleteDownload(downloadId) {
}
}
export async function filesSearch(offset, limit, searchStr, mimetypes, model, tagNames, headline, months, fromYear, toYear, anyTag) {
export async function filesSearch(offset, limit, searchStr, mimetypes, model, tagNames, headline, months, fromYear, toYear, anyTag, order) {
const body = {search_str: searchStr, offset: parseInt(offset), limit: parseInt(limit), any_tag: anyTag};
if (mimetypes) {
body['mimetypes'] = mimetypes;
@ -850,6 +866,9 @@ export async function filesSearch(offset, limit, searchStr, mimetypes, model, ta
if (toYear) {
body['to_year'] = toYear;
}
if (order) {
body['order'] = order;
}
console.info('searching files', body);
const response = await apiPost(`${API_URI}/files/search`, body);
@ -1171,20 +1190,6 @@ export async function deleteTag(id, name) {
}
}
export async function fetchFile(path) {
const uri = `${API_URI}/files/file`;
const body = {file: path};
const response = await apiPost(uri, body);
if (response.ok) {
const content = await response.json();
return content['file'];
} else {
console.error('Unable to fetch file dict! See client logs.');
const message = await getErrorMessage(response, 'Unable to get File');
toast({type: 'error', title: 'File Error', description: message, time: 5000});
}
}
export async function sendNotification(message, url) {
const body = {message, url};
const response = await apiPost(`${API_URI}/notify`, body);

View file

@ -5,7 +5,6 @@ import {
CardHeader,
CardMeta,
Container,
Dropdown,
GridColumn,
GridRow,
Image,
@ -24,8 +23,8 @@ import {
ExternalCardLink,
FileIcon,
findPosterPath,
InfoHeader,
humanFileSize,
InfoHeader,
isoDatetimeToAgoPopup,
mimetypeColor,
PageContainer,
@ -39,7 +38,7 @@ import {
import {deleteArchives, postDownload, tagFileGroup, untagFileGroup} from "../api";
import {Link, Route, Routes, useNavigate, useParams} from "react-router-dom";
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
import {useArchive, useDomains, useSearchArchives, useSearchDomain, useSearchOrder} from "../hooks/customHooks";
import {useArchive, useDomains, useSearchArchives, useSearchOrder} from "../hooks/customHooks";
import {FileCards, FileRowTagIcon, FilesView} from "./Files";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
import _ from "lodash";
@ -48,7 +47,7 @@ import {Button, Card, CardIcon, darkTheme, Header, Loader, Placeholder, Popup, S
import {SortableTable} from "./SortableTable";
import {taggedImageLabel, TagsSelector} from "../Tags";
import {toast} from "react-semantic-toasts-2";
import {Downloaders} from "./Vars";
import {API_ARCHIVE_UPLOAD_URI, Downloaders} from "./Vars";
function archiveFileLink(path, directory = false) {
if (path) {
@ -394,29 +393,47 @@ export function DomainsPage() {
</>;
}
export function SearchDomain() {
// A Dropdown which allows the user to filter by Archive domains.
const {domain, domains, setDomain} = useSearchDomain();
function ArchiveSettingsPage() {
const {t} = React.useContext(ThemeContext);
const handleChange = (e, {value}) => {
setDomain(value);
}
const urlClipboardButton = <APIButton
icon='copy'
onClick={() => navigator.clipboard.writeText(API_ARCHIVE_UPLOAD_URI)}
/>;
const dataFieldNameClipboardButton = <APIButton
icon='copy'
onClick={() => navigator.clipboard.writeText('singlefile_contents')}
/>;
const urlFieldNameClipboardButton = <APIButton
icon='copy'
onClick={() => navigator.clipboard.writeText('url')}
/>;
let domainOptions = [];
return <PageContainer>
<Header as='h1'>SingleFile Browser Extension</Header>
if (domains && domains.length > 0) {
domainOptions = domains.map(i => {
return {key: i['domain'], value: i['domain'], text: i['domain']}
});
}
return <>
<Dropdown selection search clearable fluid
placeholder='Domains'
options={domainOptions}
onChange={handleChange}
value={domain}
<p {...t}>
These are the settings necessary to configure the <a
href="https://github.com/gildas-lormeau/SingleFile?tab=readme-ov-file#install">SingleFile Browser
Extension</a> to automatically upload to your WROLPi.
</p>
<label {...t}>Upload URL</label>
<Input fluid
value={API_ARCHIVE_UPLOAD_URI}
label={urlClipboardButton}
/>
</>
<label {...t}>Data Field Name</label>
<Input fluid
value='singlefile_contents'
label={dataFieldNameClipboardButton}
/>
<label {...t}>URL Field Name</label>
<Input fluid
value='url'
label={urlFieldNameClipboardButton}
/>
</PageContainer>
}
function ArchivesPage() {
@ -599,12 +616,14 @@ export function ArchiveRoute() {
const links = [
{text: 'Archives', to: '/archive', end: true},
{text: 'Domains', to: '/archive/domains'},
{text: 'Settings', to: '/archive/settings'},
];
return <PageContainer>
<TabLinks links={links}/>
<Routes>
<Route path='/' element={<ArchivesPage/>}/>
<Route path='domains' element={<DomainsPage/>}/>
<Route path='settings' element={<ArchiveSettingsPage/>}/>
<Route path=':archiveId' element={<ArchivePage/>}/>
</Routes>
</PageContainer>

View file

@ -10,6 +10,8 @@ import {
Container,
Dimmer,
DimmerDimmable,
GridColumn,
GridRow,
Icon as SIcon,
IconGroup,
Input,
@ -48,6 +50,7 @@ import {FilePreviewContext} from "./FilePreview";
import _ from "lodash";
import {killDownloads, startDownloads} from "../api";
import {allFrequencyOptions, NAME, semanticUIColorMap, validUrlRegex} from "./Vars";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
export function Paginator({activePage, onPageChange, totalPages, showFirstAndLast, size = 'mini'}) {
const handlePageChange = (e, {activePage}) => {
@ -1960,14 +1963,22 @@ export function getDistinctColor(hexColors) {
return hslToHex(newColor.h, newColor.s, newColor.l);
}
export const DismissibleMessage = ({storageName, children}) => {
const [dismissed, setDismissed] = useLocalStorage(storageName, false);
if (dismissed) {
return <React.Fragment/>
export const RefreshHeader = ({header, headerSize = 'h2', onRefresh, popupContents}) => {
const refreshButton = <APIButton icon='refresh' onClick={onRefresh}/>;
let popup;
if (popupContents) {
popup = <Popup
content={popupContents}
on='hover'
trigger={refreshButton}
/>;
}
return <Message onDismiss={() => setDismissed(true)}>
{children}
</Message>
return <Grid columns={2}>
<GridRow>
<GridColumn>
<Header as={headerSize}>{header}</Header>
</GridColumn>
<GridColumn textAlign='right'>{popup || refreshButton}</GridColumn>
</GridRow>
</Grid>
}

View file

@ -4,7 +4,7 @@ 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, getFile, tagFileGroup, untagFileGroup} from "../api";
import {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";
@ -200,7 +200,7 @@ export function FilePreviewProvider({children}) {
// Get the file again with its Tags.
const {path, primary_path} = previewFile;
try {
const file = await fetchFile(primary_path ?? path);
const file = await getFile(primary_path ?? path);
setPreviewFile(file);
} finally {
await handleCallbacks();
@ -310,6 +310,7 @@ export function FilePreviewProvider({children}) {
const lowerPath = path.toLowerCase();
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);
@ -336,6 +337,14 @@ export function FilePreviewProvider({children}) {
// No special handler for this file type, just open it.
setModalContent(getGenericPreviewModal(previewFile), url, downloadURL, path, taggable);
}
// Fetch the file so `FileGroup.viewed` is set.
try {
getFile(path);
} catch (e) {
console.error(e);
console.error('Failed to get file to set FileGroup.viewed');
}
}
}, [previewFile]);

View file

@ -12,6 +12,7 @@ export const FILES_MEDIA_URI = `https://${window.location.hostname}:${port}/medi
export const MAP_VIEWER_URI = `https://${window.location.hostname}:8084`;
export const ZIM_VIEWER_URI = `https://${window.location.hostname}:8085`;
export const HELP_VIEWER_URI = `https://${window.location.hostname}:8086`;
export const API_ARCHIVE_UPLOAD_URI = `http://${window.location.hostname}:8081/api/archive/upload`;
export let defaultFileOrder = '-published_datetime'; // Most recently published files first.
export let defaultSearchOrder = 'rank'; // Most relevant files first.

View file

@ -18,7 +18,7 @@ import {
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
import Container from "semantic-ui-react/dist/commonjs/elements/Container";
import {VideoPlaceholder} from "./Placeholder";
import {useChannel} from "../hooks/customHooks";
import {useChannel, useVideoCaptions, useVideoExtras} from "../hooks/customHooks";
import {ThemeContext} from "../contexts/contexts";
import {Button, darkTheme, Header, Icon, Segment, Tab, TabPane} from "./Theme";
import {VideoCard} from "./Videos";
@ -192,6 +192,7 @@ function VideoPage({videoFile, prevFile, nextFile, fetchVideo, ...props}) {
// Get the Video's channel, fallback to the URL's channel id.
const {channelId} = useParams();
const {channel} = useChannel(video && video.channel_id ? video.channel_id : channelId);
const {comments, captions} = useVideoExtras(videoFile?.video?.id);
if (videoFile === null) {
return <VideoPlaceholder/>;
@ -236,11 +237,11 @@ function VideoPage({videoFile, prevFile, nextFile, fetchVideo, ...props}) {
const captionsPane = {
menuItem: 'Captions', render: () => <TabPane>
<pre>{video.caption || 'No captions available.'}</pre>
<pre>{captions || 'No captions available.'}</pre>
</TabPane>
};
const {poster_file, info_json_file, comments} = video;
const {poster_file, info_json_file} = video;
const filesPane = {
menuItem: 'Files', render: () => <TabPane>
<h3>Video File</h3>

View file

@ -12,15 +12,15 @@ import {
Modal,
ModalContent,
ModalHeader,
Popup,
Segment
} from "../Theme";
import {ButtonGroup, Container, Dimmer, Dropdown, GridColumn, GridRow, Icon, Input} from "semantic-ui-react";
import {
APIButton,
ErrorMessage,
InfoPopup,
HotspotToggle,
InfoPopup,
RefreshHeader,
ThrottleToggle,
Toggle,
useMessageDismissal,
@ -489,16 +489,6 @@ export function SettingsPage() {
body = <ErrorMessage>Unable to fetch settings</ErrorMessage>
}
const refreshButton = <APIButton
icon='refresh'
onClick={fetchConfigs}
/>;
const refreshPopupButton = <Popup
content='Check if configs are valid'
on='hover'
trigger={refreshButton}
/>;
const controlSegment = <Segment>
<Header as='h3'>Control WROLPi</Header>
<HotspotToggle/>
@ -509,14 +499,11 @@ export function SettingsPage() {
</Segment>;
const configsSegment = <Segment>
<Grid columns={2}>
<GridRow>
<GridColumn>
<Header as='h2'>Configs</Header>
</GridColumn>
<GridColumn textAlign='right'>{refreshPopupButton}</GridColumn>
</GridRow>
</Grid>
<RefreshHeader
header='Refresh'
popupContents='Check if configs are valid'
onRefresh={fetchConfigs}
/>
<p>These configs control this WROLPi; they are the source of truth. Any changes to configs will be applied to
the database when imported (typically at startup).</p>

View file

@ -18,6 +18,8 @@ import {
getStatistics,
getStatus,
getVideo,
getVideoCaptions,
getVideoComments,
getVideosStatistics,
postDumpConfig,
postImportConfig,
@ -423,6 +425,42 @@ export const useVideo = (videoId) => {
return {videoFile, prevFile, nextFile, fetchVideo};
}
export const useVideoExtras = (videoId) => {
// Fetches extra data to display on a Video's page.
const [comments, setComments] = useState(null);
const [captions, setCaptions] = useState(null);
const fetchComments = async () => {
try {
const result = await getVideoComments(videoId);
setComments(result.comments);
} catch (e) {
console.error(e);
}
}
const fetchCaptions = async () => {
try {
const result = await getVideoCaptions(videoId);
setCaptions(result.captions);
} catch (e) {
console.error(e);
}
}
useEffect(() => {
if (videoId) {
fetchComments();
fetchCaptions();
} else {
setComments(null);
setCaptions(null);
}
}, [videoId]);
return {comments, captions}
}
export const useChannel = (channel_id) => {
const emptyChannel = {
name: '',
@ -500,6 +538,37 @@ export const useChannels = () => {
return {channels, fetchChannels}
}
export const useSearchRecentFiles = () => {
const [searchFiles, setSearchFiles] = useState(null);
const [loading, setLoading] = useState(false);
const localSearchFiles = async () => {
setLoading(true);
try {
let [file_groups, total] = await filesSearch(
null, 12, null, null, null, [], false,
null, null, null, false, 'viewed');
setSearchFiles(file_groups);
} catch (e) {
console.error(e);
toast({
type: 'error',
title: 'Unexpected server response',
description: 'Could not get recent files',
time: 5000,
});
} finally {
setLoading(false);
}
}
React.useEffect(() => {
localSearchFiles();
}, []);
return {searchFiles, loading, fetchFiles: localSearchFiles};
}
export const useSearchFiles = (defaultLimit = 48, emptySearch = false, model) => {
const {
activeTags, anyTag,