Skip to content

Commit fa8e367

Browse files
committed
fix: cancel download does not work
1 parent 460cb33 commit fa8e367

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

core/src/core.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ const downloadFile: (url: string, fileName: string) => Promise<any> = (
4747
const deleteFile: (path: string) => Promise<any> = (path) =>
4848
window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path);
4949

50+
/**
51+
* Aborts the download of a specific file.
52+
* @param {string} fileName - The name of the file whose download is to be aborted.
53+
* @returns {Promise<any>} A promise that resolves when the download has been aborted.
54+
*/
55+
const abortDownload: (fileName: string) => Promise<any> = (fileName) =>
56+
window.coreAPI?.abortDownload(fileName);
57+
5058
/**
5159
* Retrieves the path to the app data directory using the `coreAPI` object.
5260
* If the `coreAPI` object is not available, the function returns `undefined`.
@@ -79,6 +87,7 @@ export const core = {
7987
invokePluginFunc,
8088
executeOnMain,
8189
downloadFile,
90+
abortDownload,
8291
deleteFile,
8392
appDataPath,
8493
getUserSpace,
@@ -91,6 +100,7 @@ export {
91100
invokePluginFunc,
92101
executeOnMain,
93102
downloadFile,
103+
abortDownload,
94104
deleteFile,
95105
appDataPath,
96106
getUserSpace,

core/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ export { core, deleteFile, invokePluginFunc } from "./core";
88
* Core module exports.
99
* @module
1010
*/
11-
export { downloadFile, executeOnMain, appDataPath, getUserSpace } from "./core";
11+
export {
12+
downloadFile,
13+
executeOnMain,
14+
appDataPath,
15+
getUserSpace,
16+
abortDownload,
17+
} from "./core";
1218

1319
/**
1420
* Events module exports.

core/src/plugins/model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export abstract class ModelPlugin extends JanPlugin {
1616
*/
1717
abstract downloadModel(model: Model): Promise<void>;
1818

19+
/**
20+
* Cancels the download of a specific model.
21+
* @param {string} modelId - The ID of the model to cancel the download for.
22+
* @returns {Promise<void>} A promise that resolves when the download has been cancelled.
23+
*/
24+
abstract cancelModelDownload(modelId: string): Promise<void>;
25+
1926
/**
2027
* Deletes a model.
2128
* @param filePath - The file path of the model to delete.

plugins/model-plugin/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PluginType, fs, downloadFile } from '@janhq/core'
1+
import { PluginType, fs, downloadFile, abortDownload } from '@janhq/core'
22
import { ModelPlugin } from '@janhq/core/lib/plugins'
33
import { Model, ModelCatalog } from '@janhq/core/lib/types'
44
import { parseToModel } from './helpers/modelParser'
@@ -50,6 +50,15 @@ export default class JanModelPlugin implements ModelPlugin {
5050
downloadFile(model.downloadLink, path)
5151
}
5252

53+
/**
54+
* Cancels the download of a specific machine learning model.
55+
* @param {string} modelId - The ID of the model whose download is to be cancelled.
56+
* @returns {Promise<void>} A promise that resolves when the download has been cancelled.
57+
*/
58+
async cancelModelDownload(modelId: string): Promise<void> {
59+
return abortDownload(join(JanModelPlugin._homeDir, modelId, modelId))
60+
}
61+
5362
/**
5463
* Deletes a machine learning model.
5564
* @param filePath - The path to the model file to delete.

web/containers/ModalCancelDownload/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useMemo } from 'react'
22

3+
import { PluginType } from '@janhq/core'
4+
import { ModelPlugin } from '@janhq/core/lib/plugins'
35
import { ModelVersion } from '@janhq/core/lib/types'
46

57
import {
@@ -19,6 +21,8 @@ import { useDownloadState } from '@/hooks/useDownloadState'
1921

2022
import { formatDownloadPercentage } from '@/utils/converter'
2123

24+
import { pluginManager } from '@/plugin'
25+
2226
type Props = {
2327
suitableModel: ModelVersion
2428
isFromList?: boolean
@@ -67,9 +71,9 @@ export default function ModalCancelDownload({
6771
themes="danger"
6872
onClick={() => {
6973
if (downloadState?.fileName)
70-
window.coreAPI?.abortDownload(
71-
`models/${downloadState?.fileName}`
72-
)
74+
pluginManager
75+
.get<ModelPlugin>(PluginType.Model)
76+
?.cancelModelDownload(downloadState.fileName)
7377
}}
7478
>
7579
Yes

0 commit comments

Comments
 (0)