Skip to content

Commit

Permalink
Add ability to export multiple .torrents at once (#8230)
Browse files Browse the repository at this point in the history
  • Loading branch information
egbertbouman authored Oct 22, 2024
2 parents f377d1a + 4d42f97 commit 321f4e8
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 28 deletions.
175 changes: 165 additions & 10 deletions src/tribler/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/tribler/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"i18next": "^23.11.4",
"javascript-time-ago": "^2.5.10",
"js-cookie": "^3.0.5",
"jszip": "^3.10.1",
"lucide-react": "^0.292.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
29 changes: 28 additions & 1 deletion src/tribler/ui/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import zh from 'javascript-time-ago/locale/zh'
import Cookies from "js-cookie";
import { useTranslation } from "react-i18next";
import { triblerService } from "@/services/tribler.service";
import { FileTreeItem } from "@/models/file.model";
import { FileLink, FileTreeItem } from "@/models/file.model";
import { CheckedState } from "@radix-ui/react-checkbox";
import JSZip from "jszip";

TimeAgo.setDefaultLocale(en.locale)
TimeAgo.addLocale(en)
Expand Down Expand Up @@ -225,3 +226,29 @@ export const getSelectedFilesFromTree = (tree: FileTreeItem, included: boolean =
selectedFiles.push(tree.index);
return selectedFiles;
}

export function downloadFile(file: FileLink) {
var link = document.createElement("a");
link.download = file.name;
link.href = file.uri;
link.click();
}

export async function downloadFilesAsZip(files: FileLink[], zipName: string) {
const zip = new JSZip();
for (let i = 0; i < files.length; i++) {
const response = await fetch(files[i].uri);
if (response.status != 200) continue;
const blob = await response.blob();

zip.file(files[i].name, blob);

if (i == files.length - 1) {
const zipData = await zip.generateAsync({ type: "blob" });
const link = document.createElement("a");
link.href = window.URL.createObjectURL(zipData);
link.download = zipName;
link.click();
}
}
}
5 changes: 5 additions & 0 deletions src/tribler/ui/src/models/file.model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export interface FileTreeItem {
included?: CheckedState;
subRows?: FileTreeItem[];
}

export interface FileLink {
uri: string;
name: string;
}
Loading

0 comments on commit 321f4e8

Please sign in to comment.