Skip to content

Build your personal knowledge base with Trilium Notes

feat(resources): refuse a download that does not fit in the free space

Elian Doranaec1adc6581f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index d971bac26a..ae90f8459e 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -4614,12 +4614,13 @@
"sources": "From {{sources}}",
"total": "Download size",
"free_space": "Free space: {{size}}",
- "not_enough_space": "There is not enough free space for this download.",
+ "not_enough_space": "The download needs {{size}}, but only {{free}} is free on this device. Free up some space, then close this and try again.",
"progress": "{{received}} of {{total}}",
"current_file": "Downloading {{file}}…",
- "failed": "The download failed: {{error}}",
"download": "Download",
"retry": "Retry",
- "cancel": "Cancel"
+ "cancel": "Cancel",
+ "not_enough_space_title": "Not enough free space",
+ "failed_title": "Download failed"
}
}
diff --git a/apps/client/src/widgets/dialogs/resource_download.spec.tsx b/apps/client/src/widgets/dialogs/resource_download.spec.tsx
index d52395753c..c5e939494a 100644
--- a/apps/client/src/widgets/dialogs/resource_download.spec.tsx
+++ b/apps/client/src/widgets/dialogs/resource_download.spec.tsx
@@ -37,7 +37,7 @@ const STATUS: ResourcesStatusResponse = {
sources: [ "huggingface.co", "github.com" ],
installed: false
} ],
- freeBytes: 100,
+ freeBytes: 10_000,
download: null
};
@@ -69,8 +69,8 @@ describe("ResourceDownloadDialog", () => {
expect(text(".resource-download-meta")).toContain(`"size":"200 B"`);
expect(text(".resource-download-item .resource-download-size")).toBe("700 B");
expect(text(".resource-download-total .resource-download-size")).toBe("500 B");
- expect(text(".resource-download-total")).toContain(`free_space:{"size":"100 B"}`);
- expect(container.querySelector(".alert-warning")).not.toBeNull();
+ expect(text(".resource-download-total")).toContain(`free_space:{"size":"9.77 KiB"}`);
+ expect(container.querySelector(".admonition")).toBeNull();
await click("resource_download.download");
expect(mocks.post).toHaveBeenCalledExactlyOnceWith("resources/download",
@@ -95,10 +95,17 @@ describe("ResourceDownloadDialog", () => {
await send(progress({ status: "failed", error: "checksum" }));
await act(async () => respond(progress()));
- expect(text(".alert-danger")).toContain("checksum");
+ expect(text(".admonition.caution")).toContain("checksum");
expect(findButton("resource_download.retry")).toBeDefined();
});
+ it("refuses to start a download that cannot fit in the free space", async () => {
+ await open({ ...STATUS, freeBytes: 100 });
+
+ expect(text(".admonition.caution")).toContain(`"size":"500 B","free":"100 B"`);
+ expect(findButton("resource_download.download")?.disabled).toBe(true);
+ });
+
it("cancels the running download when closed, and resolves false", async () => {
mocks.post.mockResolvedValue(progress());
await open();
@@ -111,11 +118,11 @@ describe("ResourceDownloadDialog", () => {
expect(results).toEqual([ false ]);
});
- async function open() {
+ async function open(status = STATUS) {
await act(async () => {
await host.handleEvent("showResourceDownloadDialog", {
ids: [ "transcription.model" ],
- status: STATUS,
+ status,
callback: (installed: boolean) => results.push(installed)
});
});
diff --git a/apps/client/src/widgets/dialogs/resource_download.tsx b/apps/client/src/widgets/dialogs/resource_download.tsx
index f74498f023..7819225da8 100644
--- a/apps/client/src/widgets/dialogs/resource_download.tsx
+++ b/apps/client/src/widgets/dialogs/resource_download.tsx
@@ -9,7 +9,7 @@ import { t } from "../../services/i18n";
import server from "../../services/server";
import { formatSize } from "../../services/utils";
import { subscribeToMessages, unsubscribeToMessage } from "../../services/ws";
-import Alert from "../react/Alert";
+import { ExtendedAdmonition } from "../react/Admonition";
import Button from "../react/Button";
import { Card, CardSection } from "../react/Card";
import { useTriliumEvent } from "../react/hooks";
@@ -80,6 +80,9 @@ export default function ResourceDownloadDialog() {
const isDownloading = download?.status === "downloading";
const isFailed = download?.status === "failed";
+ const remaining = opts ? getRemainingSize(opts.status) : 0;
+ const freeBytes = opts?.status.freeBytes ?? null;
+ const lacksSpace = freeBytes !== null && freeBytes < remaining;
return (
<Modal
@@ -99,23 +102,42 @@ export default function ResourceDownloadDialog() {
text={isFailed ? t("resource_download.retry") : t("resource_download.download")}
icon="bx bx-download"
kind="primary"
+ disabled={lacksSpace}
+ disabledTooltip={t("resource_download.not_enough_space_title")}
onClick={startDownload}
/>
)}
</>}
>
- {opts && <ResourceList status={opts.status} />}
+ {opts && <ResourceList status={opts.status} remaining={remaining} />}
{isDownloading && <DownloadProgress download={download} />}
+ {lacksSpace && (
+ <ExtendedAdmonition
+ type="caution"
+ icon="bx bx-error-circle"
+ title={t("resource_download.not_enough_space_title")}
+ >
+ {t("resource_download.not_enough_space", {
+ size: formatSize(remaining),
+ free: formatSize(freeBytes)
+ })}
+ </ExtendedAdmonition>
+ )}
{isFailed && (
- <Alert type="danger">{t("resource_download.failed", { error: download.error })}</Alert>
+ <ExtendedAdmonition
+ type="caution"
+ icon="bx bx-error-circle"
+ title={t("resource_download.failed_title")}
+ >
+ {download.error}
+ </ExtendedAdmonition>
)}
</Modal>
);
}
-function ResourceList({ status }: { status: ResourcesStatusResponse }) {
+function ResourceList({ status, remaining }: { status: ResourcesStatusResponse; remaining: number }) {
const missing = status.resources.filter(({ installed }) => !installed);
- const remaining = missing.reduce((total, { size, downloadedSize }) => total + size - downloadedSize, 0);
return (
<>
@@ -138,9 +160,6 @@ function ResourceList({ status }: { status: ResourcesStatusResponse }) {
</span>
</CardSection>
</Card>
- {status.freeBytes !== null && status.freeBytes < remaining && (
- <Alert type="warning">{t("resource_download.not_enough_space")}</Alert>
- )}
</>
);
}
@@ -186,3 +205,10 @@ function DownloadProgress({ download }: { download: ResourceDownloadProgress })
</div>
);
}
+
+/** The bytes still to download for the resources not installed. */
+function getRemainingSize(status: ResourcesStatusResponse) {
+ return status.resources
+ .filter(({ installed }) => !installed)
+ .reduce((total, { size, downloadedSize }) => total + size - downloadedSize, 0);
+}