2022-05-01 21:18:49 -06:00
|
|
|
import {API_URI, ARCHIVES_API, DEFAULT_LIMIT, emptyToNull, OTP_API, VIDEOS_API} from "./components/Common";
|
2023-01-31 18:08:07 -07:00
|
|
|
import {toast} from "react-semantic-toasts-2";
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
function timeoutPromise(ms, promise) {
|
|
|
|
|
// Create a timeout wrapper around a promise. If the timeout is reached, throw an error. Otherwise, return
|
|
|
|
|
// the results of the promise.
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
|
reject(new Error("promise timeout"))
|
|
|
|
|
}, ms);
|
2022-11-12 14:17:21 -07:00
|
|
|
promise.then((res) => {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
resolve(res);
|
|
|
|
|
}, (err) => {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
reject(err);
|
|
|
|
|
});
|
2021-11-01 16:42:52 -06:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
async function apiCall(url, method, body, ms = 20000) {
|
2021-11-01 16:42:52 -06:00
|
|
|
let init = {method};
|
|
|
|
|
if (body !== undefined) {
|
|
|
|
|
init.body = JSON.stringify(body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a fetch promise, this will be awaited by the timeout.
|
|
|
|
|
let promise = fetch(url, init);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// await the response or error.
|
2021-12-20 16:08:13 -07:00
|
|
|
let response = await timeoutPromise(ms, promise);
|
2022-08-06 05:22:07 +00:00
|
|
|
if (200 <= response.status && response.status < 300) {
|
2022-02-04 13:55:13 -07:00
|
|
|
// Request was successful.
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
// Request encountered an error.
|
2022-01-15 18:15:23 -07:00
|
|
|
let copy = response.clone();
|
2022-02-04 09:41:08 -07:00
|
|
|
let code = (await copy.json())['code'];
|
|
|
|
|
if (response.status === 403 && code === 17) {
|
2021-12-20 16:08:13 -07:00
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
2021-12-30 20:45:11 -07:00
|
|
|
title: 'WROL Mode is enabled!',
|
2022-08-06 05:22:07 +00:00
|
|
|
description: 'This functionality is disabled while WROL Mode is enabled!',
|
2021-12-20 16:08:13 -07:00
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return response;
|
2021-11-01 16:42:52 -06:00
|
|
|
} catch (e) {
|
|
|
|
|
// Timeout, or some other exception.
|
2023-01-31 17:11:31 -07:00
|
|
|
console.error(e);
|
2021-11-01 16:42:52 -06:00
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convenience functions for API calls.
|
|
|
|
|
let apiGet = async (url) => {
|
|
|
|
|
return await apiCall(url, 'GET')
|
|
|
|
|
};
|
|
|
|
|
let apiDelete = async (url) => {
|
|
|
|
|
return await apiCall(url, 'DELETE')
|
|
|
|
|
};
|
|
|
|
|
let apiPost = async (url, body) => {
|
|
|
|
|
return await apiCall(url, 'POST', body)
|
|
|
|
|
};
|
|
|
|
|
let apiPut = async (url, body) => {
|
|
|
|
|
return await apiCall(url, 'PUT', body)
|
|
|
|
|
};
|
|
|
|
|
let apiPatch = async (url, body) => {
|
|
|
|
|
return await apiCall(url, 'PATCH', body)
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-08 09:57:04 -06:00
|
|
|
export async function updateChannel(id, channel) {
|
2022-02-16 21:57:27 -07:00
|
|
|
channel = emptyToNull(channel);
|
2022-04-08 09:57:04 -06:00
|
|
|
return await apiPut(`${VIDEOS_API}/channels/${id}`, channel);
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createChannel(channel) {
|
2022-02-16 21:57:27 -07:00
|
|
|
channel = emptyToNull(channel);
|
2021-11-01 16:42:52 -06:00
|
|
|
return await apiPost(`${VIDEOS_API}/channels`, channel);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export async function deleteChannel(channelId) {
|
|
|
|
|
return apiDelete(`${VIDEOS_API}/channels/${channelId}`);
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getChannels() {
|
|
|
|
|
let response = await apiGet(`${VIDEOS_API}/channels`);
|
2022-05-01 21:12:39 -06:00
|
|
|
if (response.status === 200) {
|
2022-05-01 21:03:30 -06:00
|
|
|
return (await response.json())['channels'];
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Unexpected server response',
|
|
|
|
|
description: 'Failed to get channels. See server logs.',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-04-08 09:57:04 -06:00
|
|
|
export async function getChannel(id) {
|
|
|
|
|
let response = await apiGet(`${VIDEOS_API}/channels/${id}`);
|
2021-11-01 16:42:52 -06:00
|
|
|
return (await response.json())['channel'];
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export async function searchVideos(offset, limit, channelId, searchStr, order_by, filters = []) {
|
2021-11-01 16:42:52 -06:00
|
|
|
// Build a search query to retrieve a list of videos from the API
|
2022-08-06 05:22:07 +00:00
|
|
|
offset = parseInt(offset || 0);
|
|
|
|
|
limit = parseInt(limit || DEFAULT_LIMIT);
|
|
|
|
|
let body = {offset, limit, filters, order_by: order_by || 'rank'};
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
if (searchStr) {
|
2022-08-06 05:22:07 +00:00
|
|
|
body['search_str'] = searchStr;
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
2022-08-06 05:22:07 +00:00
|
|
|
if (channelId) {
|
|
|
|
|
body['channel_id'] = parseInt(channelId);
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
2022-08-06 05:22:07 +00:00
|
|
|
|
|
|
|
|
console.debug('searching videos', body);
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
let response = await apiPost(`${VIDEOS_API}/search`, body);
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
let data = await response.json();
|
2022-08-06 05:22:07 +00:00
|
|
|
return [data['files'], data['totals']['files']];
|
2021-11-01 16:42:52 -06:00
|
|
|
} else {
|
2022-05-01 21:03:30 -06:00
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Unexpected server response',
|
|
|
|
|
description: 'Failed to search videos. See server logs.',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
2022-08-06 05:22:07 +00:00
|
|
|
return [[], 0];
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getVideo(video_id) {
|
|
|
|
|
let response = await apiGet(`${VIDEOS_API}/video/${video_id}`);
|
|
|
|
|
let data = await response.json();
|
2022-08-06 05:22:07 +00:00
|
|
|
return [data['file'], data['prev'], data['next']];
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-09-23 16:43:45 -06:00
|
|
|
export async function deleteVideos(videoIds) {
|
|
|
|
|
const i = videoIds.join(',');
|
|
|
|
|
let response = await apiDelete(`${VIDEOS_API}/video/${i}`);
|
2021-11-01 16:42:52 -06:00
|
|
|
if (response.status !== 204) {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
2021-12-30 20:45:11 -07:00
|
|
|
title: 'Unexpected server response',
|
2022-09-23 16:43:45 -06:00
|
|
|
description: 'Failed to delete videos. See server logs.',
|
2021-11-01 16:42:52 -06:00
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getDirectories(search_str) {
|
|
|
|
|
let form_data = {'search_str': search_str || null};
|
2022-11-09 08:33:55 -07:00
|
|
|
let response = await apiPost(`${API_URI}/files/directories`, form_data);
|
2021-11-01 16:42:52 -06:00
|
|
|
if (response.status === 200) {
|
2022-11-09 08:33:55 -07:00
|
|
|
return await response.json();
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 20:07:40 -06:00
|
|
|
export async function getStatus() {
|
|
|
|
|
let response = await apiGet(`${API_URI}/status`);
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get server status', time: 5000,
|
2022-06-29 20:07:40 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 21:57:27 -07:00
|
|
|
export async function getSettings() {
|
2021-11-01 16:42:52 -06:00
|
|
|
let response = await apiGet(`${API_URI}/settings`);
|
2022-05-01 21:12:39 -06:00
|
|
|
if (response.status === 200) {
|
2022-05-01 21:03:30 -06:00
|
|
|
return await response.json();
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get settings', time: 5000,
|
2022-05-01 21:03:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-02-16 21:57:27 -07:00
|
|
|
export async function saveSettings(settings) {
|
|
|
|
|
return await apiPatch(`${API_URI}/settings`, settings);
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2021-12-20 16:08:13 -07:00
|
|
|
export async function getDownloads() {
|
|
|
|
|
let response = await apiGet(`${API_URI}/download`);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get downloads', time: 5000,
|
2022-05-01 21:03:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2021-12-20 16:08:13 -07:00
|
|
|
}
|
|
|
|
|
|
2021-11-01 16:42:52 -06:00
|
|
|
export async function validateRegex(regex) {
|
|
|
|
|
let response = await apiPost(`${API_URI}/valid_regex`, {regex});
|
|
|
|
|
return (await response.json())['valid'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function favoriteVideo(video_id, favorite) {
|
|
|
|
|
let body = {favorite: favorite, video_id};
|
2021-12-20 16:08:13 -07:00
|
|
|
let response = await apiPost(`${VIDEOS_API}/favorite`, body);
|
2021-11-01 16:42:52 -06:00
|
|
|
return (await response.json())['favorite'];
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 11:10:01 -07:00
|
|
|
export async function getVideosStatistics() {
|
2021-11-01 16:42:52 -06:00
|
|
|
let response = await apiGet(`${VIDEOS_API}/statistics`);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return (await response.json())['statistics'];
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get statistics', time: 5000,
|
2022-05-01 21:03:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export async function downloadChannel(channelId) {
|
|
|
|
|
let url = `${VIDEOS_API}/channels/download/${channelId}`;
|
2021-12-30 20:45:11 -07:00
|
|
|
let response = await apiPost(url);
|
|
|
|
|
let json = await response.json();
|
|
|
|
|
if (response.status === 400 && json['code'] === 30) {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Cannot Download!',
|
2022-05-10 22:44:59 -06:00
|
|
|
description: 'This channel does not have a download record. Modify the frequency.',
|
2021-12-30 20:45:11 -07:00
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export async function refreshChannel(channelId) {
|
|
|
|
|
let url = `${VIDEOS_API}/channels/refresh/${channelId}`;
|
2021-11-01 16:42:52 -06:00
|
|
|
await fetch(url, {method: 'POST'});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function encryptOTP(otp, plaintext) {
|
|
|
|
|
let body = {otp, plaintext};
|
2021-12-30 20:45:11 -07:00
|
|
|
let response = await apiPost(`${OTP_API}/encrypt_otp`, body);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
} else {
|
2021-11-01 16:42:52 -06:00
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Error!', description: 'Failed to encrypt OTP', time: 5000,
|
2021-11-01 16:42:52 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function decryptOTP(otp, ciphertext) {
|
|
|
|
|
let body = {otp, ciphertext};
|
2021-12-30 20:45:11 -07:00
|
|
|
let response = await apiPost(`${OTP_API}/decrypt_otp`, body);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
} else {
|
2021-11-01 16:42:52 -06:00
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Error!', description: 'Failed to decrypt OTP', time: 5000,
|
2021-11-01 16:42:52 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getCategories() {
|
|
|
|
|
let response = await apiGet(`${API_URI}/inventory/categories`);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return (await response.json())['categories'];
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get categories', time: 5000,
|
2022-05-01 21:03:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getBrands() {
|
|
|
|
|
let response = await apiGet(`${API_URI}/inventory/brands`);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return (await response.json())['brands'];
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get brands', time: 5000,
|
2022-05-01 21:03:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getInventories() {
|
|
|
|
|
let response = await apiGet(`${API_URI}/inventory`);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return (await response.json())['inventories'];
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get inventories', time: 5000,
|
2022-05-01 21:03:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getInventory(inventoryId) {
|
|
|
|
|
let response = await apiGet(`${API_URI}/inventory/${inventoryId}`);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get inventory', time: 5000,
|
2022-05-01 21:03:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function saveInventory(inventory) {
|
|
|
|
|
return await apiPost(`${API_URI}/inventory`, inventory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateInventory(inventoryId, inventory) {
|
|
|
|
|
delete inventory['id'];
|
|
|
|
|
return await apiPut(`${API_URI}/inventory/${inventoryId}`, inventory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteInventory(inventoryId) {
|
|
|
|
|
return await apiDelete(`${API_URI}/inventory/${inventoryId}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getItems(inventoryId) {
|
|
|
|
|
let response = await apiGet(`${API_URI}/inventory/${inventoryId}/item`);
|
|
|
|
|
return await response.json();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function saveItem(inventoryId, item) {
|
|
|
|
|
return await apiPost(`${API_URI}/inventory/${inventoryId}/item`, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateItem(itemId, item) {
|
2022-02-16 21:57:27 -07:00
|
|
|
item = emptyToNull(item);
|
2021-11-01 16:42:52 -06:00
|
|
|
return await apiPut(`${API_URI}/inventory/item/${itemId}`, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteItems(itemIds) {
|
|
|
|
|
let i = itemIds.join(',');
|
|
|
|
|
await apiDelete(`${API_URI}/inventory/item/${i}`);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 16:22:57 -06:00
|
|
|
export async function deleteArchives(archiveIds) {
|
|
|
|
|
let i = archiveIds.join(',');
|
|
|
|
|
try {
|
|
|
|
|
return await apiDelete(`${API_URI}/archive/${i}`);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Unable to delete archives', time: 5000,
|
2022-09-23 16:22:57 -06:00
|
|
|
});
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export async function searchArchives(offset, limit, domain, searchStr, order) {
|
2021-11-01 16:42:52 -06:00
|
|
|
// Build a search query to retrieve a list of videos from the API
|
2022-02-02 14:02:51 -07:00
|
|
|
offset = parseInt(offset || 0);
|
|
|
|
|
limit = parseInt(limit || DEFAULT_LIMIT);
|
2021-11-01 16:42:52 -06:00
|
|
|
let body = {offset, limit};
|
|
|
|
|
if (domain) {
|
|
|
|
|
body['domain'] = domain;
|
|
|
|
|
}
|
2022-01-03 15:13:02 -07:00
|
|
|
if (searchStr) {
|
|
|
|
|
body['search_str'] = searchStr;
|
|
|
|
|
}
|
2022-08-06 05:22:07 +00:00
|
|
|
if (order) {
|
|
|
|
|
body['order_by'] = order;
|
|
|
|
|
}
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
console.debug('searching archives', body);
|
2021-11-01 16:42:52 -06:00
|
|
|
let response = await apiPost(`${ARCHIVES_API}/search`, body);
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
let data = await response.json();
|
2021-12-31 22:35:23 -07:00
|
|
|
return [data['archives'], data['totals']['archives']];
|
2021-11-01 16:42:52 -06:00
|
|
|
} else {
|
2022-01-15 18:15:23 -07:00
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Unable to search archives',
|
|
|
|
|
description: 'Cannot search archives. See server logs.',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchDomains() {
|
|
|
|
|
let response = await apiGet(`${ARCHIVES_API}/domains`);
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
let data = await response.json();
|
|
|
|
|
return [data['domains'], data['totals']['domains']];
|
|
|
|
|
} else {
|
2022-01-15 18:15:23 -07:00
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Domains Error',
|
|
|
|
|
description: 'Unable to fetch Domains. See server logs.',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-20 16:08:13 -07:00
|
|
|
|
2022-05-16 17:56:55 -06:00
|
|
|
export async function getArchive(archiveId) {
|
|
|
|
|
const response = await apiGet(`${ARCHIVES_API}/${archiveId}`);
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
const data = await response.json();
|
2022-11-14 15:42:57 -07:00
|
|
|
return [data['file'], data['history']];
|
2022-05-16 17:56:55 -06:00
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Archive Error', description: 'Unable to get Archive. See server logs.', time: 5000,
|
2022-05-16 17:56:55 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 08:33:55 -07:00
|
|
|
export async function postDownload(urls, downloader, frequency, sub_downloader, excludedURLs, destination) {
|
|
|
|
|
let body = {
|
|
|
|
|
urls: urls,
|
|
|
|
|
downloader: downloader,
|
|
|
|
|
frequency: frequency || null,
|
|
|
|
|
excluded_urls: excludedURLs,
|
|
|
|
|
destination: destination || null,
|
|
|
|
|
};
|
2022-05-20 15:15:31 -06:00
|
|
|
if (sub_downloader) {
|
|
|
|
|
body['sub_downloader'] = sub_downloader;
|
|
|
|
|
}
|
2021-12-20 16:08:13 -07:00
|
|
|
let response = await apiPost(`${API_URI}/download`, body);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function killDownload(download_id) {
|
|
|
|
|
let response = await apiPost(`${API_URI}/download/${download_id}/kill`);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
2021-12-30 20:45:11 -07:00
|
|
|
|
2022-01-11 10:08:47 -07:00
|
|
|
export async function killDownloads() {
|
|
|
|
|
let response = await apiPost(`${API_URI}/download/kill`);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function startDownloads() {
|
|
|
|
|
let response = await apiPost(`${API_URI}/download/enable`);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 20:45:11 -07:00
|
|
|
export async function getDownloaders() {
|
2022-02-04 13:55:13 -07:00
|
|
|
try {
|
|
|
|
|
let response = await apiGet(`${API_URI}/downloaders`);
|
|
|
|
|
return await response.json();
|
|
|
|
|
} catch (e) {
|
2022-05-01 21:03:30 -06:00
|
|
|
return {downloaders: []};
|
2022-02-04 13:55:13 -07:00
|
|
|
}
|
2021-12-30 20:45:11 -07:00
|
|
|
}
|
2022-01-29 16:50:22 -07:00
|
|
|
|
2022-11-12 14:17:21 -07:00
|
|
|
export async function deleteDownload(downloadId) {
|
|
|
|
|
try {
|
|
|
|
|
let response = await apiDelete(`${API_URI}/download/${downloadId}`);
|
|
|
|
|
if (response.status !== 204) {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Unable to delete',
|
|
|
|
|
description: 'Unable to delete the download. See server logs.',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 14:02:51 -07:00
|
|
|
const replaceFileDatetimes = (files) => {
|
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
2022-02-24 12:20:20 -07:00
|
|
|
let file = files[i];
|
2022-02-02 14:02:51 -07:00
|
|
|
if (file['modified']) {
|
2022-05-01 21:18:49 -06:00
|
|
|
files[i]['modified'] = file['modified'] * 1000;
|
2022-02-02 14:02:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 11:22:20 -07:00
|
|
|
export async function filesSearch(offset, limit, searchStr, mimetypes, model) {
|
2022-02-01 14:11:14 -07:00
|
|
|
const body = {search_str: searchStr, offset: parseInt(offset), limit: parseInt(limit)};
|
2022-12-09 11:22:20 -07:00
|
|
|
if (mimetypes) {
|
|
|
|
|
body['mimetypes'] = mimetypes;
|
2022-08-06 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
if (model) {
|
|
|
|
|
body['model'] = model;
|
|
|
|
|
}
|
|
|
|
|
console.debug('searching files', body);
|
2022-02-01 14:11:14 -07:00
|
|
|
const response = await apiPost(`${API_URI}/files/search`, body);
|
2022-02-02 14:02:51 -07:00
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
let data = await response.json();
|
|
|
|
|
let [files, total] = [data['files'], data['totals']['files']];
|
|
|
|
|
files = replaceFileDatetimes(files);
|
|
|
|
|
return [files, total];
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Unable to search files',
|
|
|
|
|
description: 'Cannot search files. See server logs.',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
2022-08-06 05:22:07 +00:00
|
|
|
return [null, null];
|
2022-02-02 14:02:51 -07:00
|
|
|
}
|
2022-02-01 14:11:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function refreshFiles() {
|
|
|
|
|
return await apiPost(`${API_URI}/files/refresh`);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export async function refreshDirectoryFiles(directory) {
|
|
|
|
|
let body = {directory};
|
|
|
|
|
try {
|
|
|
|
|
return await apiPost(`${API_URI}/files/refresh/directory`, body);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Unable to refresh directory',
|
|
|
|
|
description: 'Cannot refresh the files in the directory. See server logs.',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-29 16:50:22 -07:00
|
|
|
export async function getFiles(directories) {
|
2023-02-15 17:40:57 -07:00
|
|
|
console.debug(`getFiles ${directories}`);
|
2022-01-29 16:50:22 -07:00
|
|
|
let body = {directories: directories || []};
|
|
|
|
|
let response = await apiPost(`${API_URI}/files`, body);
|
2022-02-02 14:02:51 -07:00
|
|
|
let {files} = await response.json();
|
|
|
|
|
files = replaceFileDatetimes(files);
|
|
|
|
|
return files;
|
2022-01-29 16:50:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteFile(file) {
|
|
|
|
|
let body = {file: file};
|
|
|
|
|
await apiPost(`${API_URI}/files/delete`, body);
|
|
|
|
|
}
|
2022-01-30 16:59:26 -07:00
|
|
|
|
2022-02-04 09:41:08 -07:00
|
|
|
|
2022-02-04 16:05:19 -07:00
|
|
|
export async function getHotspotStatus() {
|
2022-02-16 21:57:27 -07:00
|
|
|
let response = await getSettings();
|
|
|
|
|
return response['hotspot_status'];
|
2022-02-04 09:41:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function setHotspot(on) {
|
|
|
|
|
let response;
|
|
|
|
|
if (on) {
|
|
|
|
|
response = await apiPost(`${API_URI}/hotspot/on`);
|
|
|
|
|
} else {
|
|
|
|
|
response = await apiPost(`${API_URI}/hotspot/off`);
|
|
|
|
|
}
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
let code = (await response.json())['code'];
|
|
|
|
|
if (code === 34) {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Unsupported!',
|
|
|
|
|
description: 'Hotspot is only supported on a Raspberry Pi!',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
2022-02-04 16:05:19 -07:00
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Error!', description: 'Could not modify hotspot. See server logs.', time: 5000,
|
2022-02-04 16:05:19 -07:00
|
|
|
});
|
2022-02-04 09:41:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-07 13:37:58 -07:00
|
|
|
|
|
|
|
|
export async function getThrottleStatus() {
|
2022-02-16 21:57:27 -07:00
|
|
|
let response = await getSettings();
|
|
|
|
|
return response['throttle_status'];
|
2022-02-07 13:37:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function setThrottle(on) {
|
|
|
|
|
let response;
|
|
|
|
|
if (on) {
|
|
|
|
|
response = await apiPost(`${API_URI}/throttle/on`);
|
|
|
|
|
} else {
|
|
|
|
|
response = await apiPost(`${API_URI}/throttle/off`);
|
|
|
|
|
}
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
let code = (await response.json())['code'];
|
|
|
|
|
if (code === 34) {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Unsupported!',
|
|
|
|
|
description: 'Throttle is only supported on a Raspberry Pi!',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Error!', description: 'Could not modify throttle. See server logs.', time: 5000,
|
2022-02-07 13:37:58 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
export async function getMapImportStatus() {
|
2022-03-31 07:31:15 -06:00
|
|
|
let response = await apiGet(`${API_URI}/map/files`);
|
2022-05-01 21:03:30 -06:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Unexpected server response', description: 'Could not get import status', time: 5000,
|
2022-05-01 21:03:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2022-03-08 13:26:03 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-31 07:31:15 -06:00
|
|
|
export async function importMapFiles(paths) {
|
|
|
|
|
let body = {'files': paths};
|
2022-03-08 13:26:03 -07:00
|
|
|
let response = await apiPost(`${API_URI}/map/import`, body);
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2022-11-12 14:17:21 -07:00
|
|
|
type: 'error', title: 'Error!', description: 'Could not start import! See server logs.', time: 5000,
|
2022-03-08 13:26:03 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-01 16:31:49 -06:00
|
|
|
|
2022-04-01 16:45:59 -06:00
|
|
|
export async function clearCompletedDownloads() {
|
2022-04-01 16:31:49 -06:00
|
|
|
let response = await apiPost(`${API_URI}/download/clear_completed`);
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
|
return null
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Error!',
|
2022-04-01 16:45:59 -06:00
|
|
|
description: 'Could not clear completed downloads! See server logs.',
|
|
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function clearFailedDownloads() {
|
|
|
|
|
let response = await apiPost(`${API_URI}/download/clear_failed`);
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
|
return null
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Error!',
|
|
|
|
|
description: 'Could not clear failed downloads! See server logs.',
|
2022-04-01 16:31:49 -06:00
|
|
|
time: 5000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-01 21:03:30 -06:00
|
|
|
|
|
|
|
|
export async function getAPIStatus() {
|
|
|
|
|
let response = await apiGet(`${API_URI}/echo`);
|
|
|
|
|
return response.status === 200;
|
|
|
|
|
}
|
2022-12-15 11:48:46 -07:00
|
|
|
|
2022-12-16 11:10:01 -07:00
|
|
|
export async function getStatistics() {
|
|
|
|
|
let response = await apiGet(`${API_URI}/statistics`);
|
2022-12-15 11:48:46 -07:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
const contents = await response.json();
|
2022-12-16 11:10:01 -07:00
|
|
|
return contents;
|
2022-12-15 11:48:46 -07:00
|
|
|
} else {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Error!',
|
|
|
|
|
description: 'Unable to get file statistics',
|
|
|
|
|
time: 5000,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-23 16:04:09 -07:00
|
|
|
|
|
|
|
|
export async function getEvents(after) {
|
|
|
|
|
let uri = `${API_URI}/events/feed`;
|
|
|
|
|
if (after) {
|
2023-01-03 08:53:00 -07:00
|
|
|
uri = `${uri}?after=${encodeURIComponent(after)}`
|
2022-12-23 16:04:09 -07:00
|
|
|
}
|
|
|
|
|
let response = await apiGet(uri);
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
}
|
|
|
|
|
}
|