feat(downloads): add qBittorrent integration
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
- Rust client for the WebUI API: password/API-key auth, categories, magnet and .torrent add, sync polling, pause/resume/delete - downloads page with progress, speed, ETA, filters, notifications and per-torrent actions - settings page with OS keyring storage, self-signed TLS, Jellyfin refresh and polling options - cross-platform keyring setup for Windows, macOS and Linux
This commit is contained in:
1 parent
fe30c4f1f9
commit
87315d9679
22 files changed
+4031
-21
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type {
|
||||
QbAddRequest,
|
||||
QbAddResult,
|
||||
QbCategory,
|
||||
QbConnectionInfo,
|
||||
QbError,
|
||||
QbSettings,
|
||||
QbSnapshot,
|
||||
} from "./types";
|
||||
|
||||
export const qbGetSettings = () => invoke<QbSettings>("qb_get_settings");
|
||||
|
||||
export const qbSaveSettings = (settings: QbSettings, secret?: string) =>
|
||||
invoke<QbSettings>("qb_save_settings", {
|
||||
settings,
|
||||
secret: secret?.trim() ? secret : null,
|
||||
});
|
||||
|
||||
export const qbForgetSecret = () => invoke<QbSettings>("qb_forget_secret");
|
||||
|
||||
export const qbTestConnection = (settings: QbSettings, secret?: string) =>
|
||||
invoke<QbConnectionInfo>("qb_test_connection", {
|
||||
settings,
|
||||
secret: secret?.trim() ? secret : null,
|
||||
});
|
||||
|
||||
export const qbCategories = () => invoke<QbCategory[]>("qb_categories");
|
||||
|
||||
export const qbAddTorrent = (request: QbAddRequest) =>
|
||||
invoke<QbAddResult>("qb_add_torrent", { request });
|
||||
|
||||
export const qbPoll = () => invoke<QbSnapshot>("qb_poll");
|
||||
|
||||
export const qbPause = (hashes: string[]) =>
|
||||
invoke<void>("qb_pause", { hashes });
|
||||
|
||||
export const qbResume = (hashes: string[]) =>
|
||||
invoke<void>("qb_resume", { hashes });
|
||||
|
||||
export const qbDelete = (hashes: string[], deleteFiles: boolean) =>
|
||||
invoke<void>("qb_delete", { hashes, deleteFiles });
|
||||
|
||||
export const qbSetCategory = (hashes: string[], category: string) =>
|
||||
invoke<void>("qb_set_category", { hashes, category });
|
||||
|
||||
/** Normalise an unknown `invoke` rejection into the shape the UI renders. */
|
||||
export const toQbError = (error: unknown): QbError => {
|
||||
if (
|
||||
typeof error === "object" &&
|
||||
error !== null &&
|
||||
"message" in error &&
|
||||
typeof (error as { message?: unknown }).message === "string"
|
||||
) {
|
||||
const value = error as Partial<QbError>;
|
||||
return {
|
||||
kind: value.kind ?? "http",
|
||||
message: value.message ?? "Неизвестная ошибка qBittorrent",
|
||||
status: value.status ?? null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
kind: "http",
|
||||
message:
|
||||
typeof error === "string"
|
||||
? error
|
||||
: "Не удалось выполнить запрос к qBittorrent",
|
||||
status: null,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,129 @@
|
||||
const BYTE_UNITS = ["Б", "КБ", "МБ", "ГБ", "ТБ", "ПБ"];
|
||||
|
||||
export const formatBytes = (bytes: number) => {
|
||||
if (!Number.isFinite(bytes) || bytes <= 0) return "0 Б";
|
||||
let value = bytes;
|
||||
let unit = 0;
|
||||
while (value >= 1024 && unit < BYTE_UNITS.length - 1) {
|
||||
value /= 1024;
|
||||
unit += 1;
|
||||
}
|
||||
const digits = value >= 100 || unit === 0 ? 0 : 1;
|
||||
return `${value.toFixed(digits).replace(".", ",")} ${BYTE_UNITS[unit]}`;
|
||||
};
|
||||
|
||||
export const formatSpeed = (bytesPerSecond: number) =>
|
||||
`${formatBytes(bytesPerSecond)}/с`;
|
||||
|
||||
export const formatEta = (seconds: number) => {
|
||||
if (!Number.isFinite(seconds) || seconds < 0 || seconds >= 8_640_000) {
|
||||
return "∞";
|
||||
}
|
||||
if (seconds < 60) return `${Math.max(1, Math.round(seconds))} с`;
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
if (minutes < 60) return `${minutes} мин`;
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const rest = minutes % 60;
|
||||
return rest > 0 ? `${hours} ч ${rest} мин` : `${hours} ч`;
|
||||
};
|
||||
|
||||
export const formatPercent = (progress: number) =>
|
||||
`${Math.min(100, Math.max(0, Math.round(progress * 100)))}%`;
|
||||
|
||||
export type TorrentTone =
|
||||
| "downloading"
|
||||
| "seeding"
|
||||
| "paused"
|
||||
| "error"
|
||||
| "neutral";
|
||||
|
||||
export type TorrentStateInfo = {
|
||||
label: string;
|
||||
tone: TorrentTone;
|
||||
/** The download has finished, regardless of seeding/paused state. */
|
||||
done: boolean;
|
||||
/** The torrent is stopped by the user. */
|
||||
paused: boolean;
|
||||
error: boolean;
|
||||
};
|
||||
|
||||
const state = (
|
||||
label: string,
|
||||
tone: TorrentTone,
|
||||
done = false,
|
||||
paused = false,
|
||||
error = false,
|
||||
): TorrentStateInfo => ({ label, tone, done, paused, error });
|
||||
|
||||
export const getTorrentState = (value: string): TorrentStateInfo => {
|
||||
switch (value) {
|
||||
case "metaDL":
|
||||
case "forcedMetaDL":
|
||||
return state("Метаданные", "downloading");
|
||||
case "downloading":
|
||||
case "forcedDL":
|
||||
return state("Скачивается", "downloading");
|
||||
case "stalledDL":
|
||||
return state("Нет пиров", "downloading");
|
||||
case "queuedDL":
|
||||
return state("В очереди", "downloading");
|
||||
case "checkingDL":
|
||||
return state("Проверка", "downloading");
|
||||
case "allocating":
|
||||
return state("Выделение места", "downloading");
|
||||
case "moving":
|
||||
return state("Перемещение", "downloading");
|
||||
case "checkingResumeData":
|
||||
return state("Проверка данных", "downloading");
|
||||
case "pausedDL":
|
||||
case "stoppedDL":
|
||||
return state("Пауза", "paused", false, true);
|
||||
case "uploading":
|
||||
case "forcedUP":
|
||||
return state("Раздаётся", "seeding", true);
|
||||
case "stalledUP":
|
||||
return state("Раздаётся", "seeding", true);
|
||||
case "queuedUP":
|
||||
return state("Завершено", "seeding", true);
|
||||
case "checkingUP":
|
||||
return state("Проверка", "seeding", true);
|
||||
case "pausedUP":
|
||||
case "stoppedUP":
|
||||
return state("Завершено", "seeding", true, true);
|
||||
case "error":
|
||||
return state("Ошибка", "error", false, false, true);
|
||||
case "missingFiles":
|
||||
return state("Файлы не найдены", "error", false, false, true);
|
||||
default:
|
||||
return state("Неизвестно", "neutral");
|
||||
}
|
||||
};
|
||||
|
||||
export type TorrentFilter = "all" | "downloading" | "completed" | "error";
|
||||
|
||||
export const matchesFilter = (
|
||||
torrent: { progress: number; state: string },
|
||||
filter: TorrentFilter,
|
||||
) => {
|
||||
const info = getTorrentState(torrent.state);
|
||||
if (filter === "all") return true;
|
||||
if (filter === "downloading") return !info.done && !info.error;
|
||||
if (filter === "completed") return info.done;
|
||||
return info.error;
|
||||
};
|
||||
|
||||
export const compareTorrents = (
|
||||
left: { progress: number; state: string; addedOn: number },
|
||||
right: { progress: number; state: string; addedOn: number },
|
||||
) => {
|
||||
const leftInfo = getTorrentState(left.state);
|
||||
const rightInfo = getTorrentState(right.state);
|
||||
const rank = (info: TorrentStateInfo) => {
|
||||
if (info.error) return 0;
|
||||
if (!info.done) return 1;
|
||||
if (info.paused) return 3;
|
||||
return 2;
|
||||
};
|
||||
const diff = rank(leftInfo) - rank(rightInfo);
|
||||
return diff !== 0 ? diff : right.addedOn - left.addedOn;
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
import { queryOptions, useQuery } from "@tanstack/react-query";
|
||||
import { qbCategories, qbGetSettings, qbPoll } from "./api";
|
||||
import type { QbSettings } from "./types";
|
||||
|
||||
export const qbSettingsQueryOptions = queryOptions({
|
||||
queryKey: ["qbittorrent", "settings"],
|
||||
queryFn: qbGetSettings,
|
||||
staleTime: Number.POSITIVE_INFINITY,
|
||||
retry: false,
|
||||
networkMode: "always",
|
||||
});
|
||||
|
||||
export const useQbSettings = () => useQuery(qbSettingsQueryOptions);
|
||||
|
||||
export const qbCategoriesQueryOptions = (enabled: boolean) =>
|
||||
queryOptions({
|
||||
queryKey: ["qbittorrent", "categories"],
|
||||
queryFn: qbCategories,
|
||||
enabled,
|
||||
staleTime: 60_000,
|
||||
retry: false,
|
||||
networkMode: "always",
|
||||
});
|
||||
|
||||
const pollInterval = (settings?: QbSettings) =>
|
||||
Math.max(1000, settings?.pollIntervalMs || 2000);
|
||||
|
||||
export const qbSnapshotQueryOptions = (settings?: QbSettings) =>
|
||||
queryOptions({
|
||||
queryKey: ["qbittorrent", "snapshot"],
|
||||
queryFn: qbPoll,
|
||||
enabled: Boolean(settings?.baseUrl && settings.hasSecret),
|
||||
refetchInterval: () =>
|
||||
document.visibilityState === "visible" ? pollInterval(settings) : false,
|
||||
refetchIntervalInBackground: false,
|
||||
retry: 1,
|
||||
networkMode: "always",
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from "./api";
|
||||
export * from "./format";
|
||||
export * from "./hooks";
|
||||
export type * from "./types";
|
||||
@@ -0,0 +1,101 @@
|
||||
export type QbSettings = {
|
||||
baseUrl: string;
|
||||
username: string;
|
||||
useApiKey: boolean;
|
||||
rememberSecret: boolean;
|
||||
defaultCategory: string;
|
||||
autoTmm: boolean;
|
||||
addPaused: boolean;
|
||||
allowInvalidCerts: boolean;
|
||||
refreshJellyfin: boolean;
|
||||
notifyOnComplete: boolean;
|
||||
pollIntervalMs: number;
|
||||
secretSaved: boolean;
|
||||
hasSecret: boolean;
|
||||
};
|
||||
|
||||
export type QbCategory = {
|
||||
name: string;
|
||||
savePath: string;
|
||||
downloadPath?: string | null;
|
||||
};
|
||||
|
||||
export type QbTorrent = {
|
||||
hash: string;
|
||||
name: string;
|
||||
category: string;
|
||||
size: number;
|
||||
completed: number;
|
||||
downloaded: number;
|
||||
progress: number;
|
||||
dlspeed: number;
|
||||
upspeed: number;
|
||||
eta: number;
|
||||
state: string;
|
||||
savePath: string;
|
||||
contentPath: string;
|
||||
addedOn: number;
|
||||
completionOn: number;
|
||||
ratio: number;
|
||||
numSeeds: number;
|
||||
numLeeches: number;
|
||||
error?: string | null;
|
||||
tags: string;
|
||||
seedingTime: number;
|
||||
timeActive: number;
|
||||
amountLeft: number;
|
||||
tracker: string;
|
||||
};
|
||||
|
||||
export type QbServerState = {
|
||||
dlInfoSpeed: number;
|
||||
upInfoSpeed: number;
|
||||
dlInfoData: number;
|
||||
upInfoData: number;
|
||||
freeSpaceOnDisk: number;
|
||||
connectionStatus: string;
|
||||
queueing: boolean;
|
||||
dhtNodes: number;
|
||||
};
|
||||
|
||||
export type QbSnapshot = {
|
||||
torrents: QbTorrent[];
|
||||
serverState: QbServerState;
|
||||
synced: boolean;
|
||||
};
|
||||
|
||||
export type QbAddResult = {
|
||||
status: "added" | "pending" | "duplicate" | "invalid";
|
||||
addedHashes: string[];
|
||||
message?: string | null;
|
||||
};
|
||||
|
||||
export type QbConnectionInfo = {
|
||||
appVersion: string;
|
||||
apiVersion: string;
|
||||
defaultSavePath: string;
|
||||
};
|
||||
|
||||
export type QbAddRequest = {
|
||||
magnet?: string | null;
|
||||
filePath?: string | null;
|
||||
category?: string | null;
|
||||
paused?: boolean | null;
|
||||
autoTmm?: boolean | null;
|
||||
};
|
||||
|
||||
export type QbError = {
|
||||
kind:
|
||||
| "unreachable"
|
||||
| "auth"
|
||||
| "banned"
|
||||
| "tls"
|
||||
| "duplicate"
|
||||
| "torrent"
|
||||
| "http"
|
||||
| "config"
|
||||
| "internal"
|
||||
| string;
|
||||
message: string;
|
||||
status?: number | null;
|
||||
};
|
||||
Reference in new issue
Block a user