Skip to content

Commit

Permalink
Fix file selection window on Windows that didnt split file with prope…
Browse files Browse the repository at this point in the history
…r separator
  • Loading branch information
ikatson committed Jan 3, 2024
1 parent b808382 commit b289f1e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 40 deletions.
5 changes: 5 additions & 0 deletions crates/librqbit/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub struct TorrentListResponse {
#[derive(Serialize, Deserialize)]
pub struct TorrentDetailsResponseFile {
pub name: String,
pub components: Vec<String>,
pub length: u64,
pub included: bool,
}
Expand All @@ -253,6 +254,7 @@ pub struct EmptyJsonResponse {}
#[derive(Serialize, Deserialize)]
pub struct TorrentDetailsResponse {
pub info_hash: String,
pub name: Option<String>,
pub files: Vec<TorrentDetailsResponseFile>,
}

Expand Down Expand Up @@ -281,16 +283,19 @@ fn make_torrent_details(
"<INVALID NAME>".to_string()
}
};
let components = filename_it.to_vec().unwrap_or_default();
let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true);
TorrentDetailsResponseFile {
name,
components,
length,
included,
}
})
.collect();
Ok(TorrentDetailsResponse {
info_hash: info_hash.as_string(),
name: info.name.as_ref().map(|b| b.to_string()),
files,
})
}
2 changes: 2 additions & 0 deletions crates/librqbit/webui/src/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export interface TorrentId {

export interface TorrentFile {
name: string;
components: string[];
length: number;
included: boolean;
}

// Interface for the Torrent Details API response
export interface TorrentDetails {
name: string | null;
info_hash: string;
files: Array<TorrentFile>;
}
Expand Down
43 changes: 19 additions & 24 deletions crates/librqbit/webui/src/components/FileListInput.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useMemo, useState } from "react";
import { AddTorrentResponse } from "../api-types";
import { AddTorrentResponse, TorrentFile } from "../api-types";
import { FormCheckbox } from "./forms/FormCheckbox";
import { CiSquarePlus, CiSquareMinus } from "react-icons/ci";
import { IconButton } from "./buttons/IconButton";
import { formatBytes } from "../helper/formatBytes";

type TorrentFileForCheckbox = {
id: number;
name: string;
filename: string;
pathComponents: string[];
length: number;
};

Expand All @@ -18,19 +19,12 @@ type FileTree = {
files: TorrentFileForCheckbox[];
};

const splitOnce = (s: string, sep: string): [string, string | undefined] => {
if (s.indexOf(sep) === -1) {
return [s, undefined];
}
return [s.slice(0, s.indexOf(sep)), s.slice(s.indexOf(sep) + 1)];
};

const newFileTree = (listTorrentResponse: AddTorrentResponse): FileTree => {
const separator = "/";
const newFileTreeInner = (
name: string,
id: string,
files: TorrentFileForCheckbox[]
files: TorrentFileForCheckbox[],
depth: number
): FileTree => {
let directFiles: TorrentFileForCheckbox[] = [];
let groups: FileTree[] = [];
Expand All @@ -41,22 +35,17 @@ const newFileTree = (listTorrentResponse: AddTorrentResponse): FileTree => {
return groupsByName[prefix];
};

files.forEach((file) => {
let [prefix, name] = splitOnce(file.name, separator);
if (name === undefined) {
files.forEach((file: TorrentFileForCheckbox) => {
if (depth == file.pathComponents.length - 1) {
directFiles.push(file);
return;
}
getGroup(prefix).push({
id: file.id,
name: name,
length: file.length,
});
getGroup(file.pathComponents[0]).push(file);
});

let childId = 0;
for (const [key, value] of Object.entries(groupsByName)) {
groups.push(newFileTreeInner(key, id + "." + childId, value));
groups.push(newFileTreeInner(key, id + "." + childId, value, depth + 1));
childId += 1;
}
return {
Expand All @@ -70,9 +59,15 @@ const newFileTree = (listTorrentResponse: AddTorrentResponse): FileTree => {
return newFileTreeInner(
"",
"filetree-root",
listTorrentResponse.details.files.map((data, id) => {
return { id, name: data.name, length: data.length };
})
listTorrentResponse.details.files.map((file, id) => {
return {
id,
filename: file.components[file.components.length - 1],
pathComponents: file.components,
length: file.length,
};
}),
0
);
};

Expand Down Expand Up @@ -168,7 +163,7 @@ const FileTreeComponent: React.FC<{
<FormCheckbox
checked={selectedFiles.has(file.id)}
key={file.id}
label={`${file.name} (${formatBytes(file.length)})`}
label={`${file.filename} (${formatBytes(file.length)})`}
name={`file-${file.id}`}
onChange={() => handleToggleFile(file.id)}
></FormCheckbox>
Expand Down
4 changes: 2 additions & 2 deletions crates/librqbit/webui/src/components/TorrentRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TorrentActions } from "./buttons/TorrentActions";
import { ProgressBar } from "./ProgressBar";
import { Speed } from "./Speed";
import { formatBytes } from "../helper/formatBytes";
import { getLargestFileName } from "../helper/getLargestFileName";
import { torrentDisplayName } from "../helper/getTorrentDisplayName";
import { getCompletionETA } from "../helper/getCompletionETA";
import { StatusIcon } from "./StatusIcon";

Expand Down Expand Up @@ -54,7 +54,7 @@ export const TorrentRow: React.FC<{
<div className="flex items-center gap-2">
<div className="md:hidden">{statusIcon("w-5 h-5")}</div>
<div className="text-left text-lg text-gray-900 text-ellipsis break-all dark:text-slate-200">
{getLargestFileName(detailsResponse)}
{torrentDisplayName(detailsResponse)}
</div>
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { TorrentDetails } from "../api-types";

export function getLargestFileName(torrentDetails: TorrentDetails): string {
function getLargestFileName(torrentDetails: TorrentDetails): string {
const largestFile = torrentDetails.files
.filter((f) => f.included)
.reduce((prev: any, current: any) =>
prev.length > current.length ? prev : current
);
return largestFile.name;
}

export function torrentDisplayName(torrentDetails: TorrentDetails): string {
return torrentDetails.name ?? getLargestFileName(torrentDetails);
}
27 changes: 14 additions & 13 deletions crates/librqbit_core/src/torrent_metainfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct TorrentMetaV1Info<BufType> {
pub files: Option<Vec<TorrentMetaV1File<BufType>>>,
}

#[derive(Clone, Copy)]
pub enum FileIteratorName<'a, ByteBuf> {
Single(Option<&'a ByteBuf>),
Tree(&'a [ByteBuf]),
Expand All @@ -91,11 +92,17 @@ where
}
}

impl<'a, ByteBuf> FileIteratorName<'a, ByteBuf> {
pub fn to_string(&self) -> anyhow::Result<String>
where
ByteBuf: AsRef<[u8]>,
{
impl<'a, ByteBuf> FileIteratorName<'a, ByteBuf>
where
ByteBuf: AsRef<[u8]>,
{
pub fn to_vec(&self) -> anyhow::Result<Vec<String>> {
self.iter_components()
.map(|c| c.map(|s| s.to_owned()))
.collect()
}

pub fn to_string(&self) -> anyhow::Result<String> {
let mut buf = String::new();
for (idx, bit) in self.iter_components().enumerate() {
let bit = bit?;
Expand All @@ -106,21 +113,15 @@ impl<'a, ByteBuf> FileIteratorName<'a, ByteBuf> {
}
Ok(buf)
}
pub fn to_pathbuf(&self) -> anyhow::Result<PathBuf>
where
ByteBuf: AsRef<[u8]>,
{
pub fn to_pathbuf(&self) -> anyhow::Result<PathBuf> {
let mut buf = PathBuf::new();
for bit in self.iter_components() {
let bit = bit?;
buf.push(bit)
}
Ok(buf)
}
pub fn iter_components(&self) -> impl Iterator<Item = anyhow::Result<&'a str>>
where
ByteBuf: AsRef<[u8]>,
{
pub fn iter_components(&self) -> impl Iterator<Item = anyhow::Result<&'a str>> {
let it = match self {
FileIteratorName::Single(None) => return Either::Left(once(Ok("torrent-content"))),
FileIteratorName::Single(Some(name)) => Either::Left(once((*name).as_ref())),
Expand Down

0 comments on commit b289f1e

Please sign in to comment.