fix: cancel download does not work

This commit is contained in:
Louis 2023-11-28 14:38:46 +07:00
parent 460cb3377a
commit 1407c22f29
6 changed files with 68 additions and 10 deletions

View File

@ -47,6 +47,14 @@ const downloadFile: (url: string, fileName: string) => Promise<any> = (
const deleteFile: (path: string) => Promise<any> = (path) => const deleteFile: (path: string) => Promise<any> = (path) =>
window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path); window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path);
/**
* Aborts the download of a specific file.
* @param {string} fileName - The name of the file whose download is to be aborted.
* @returns {Promise<any>} A promise that resolves when the download has been aborted.
*/
const abortDownload: (fileName: string) => Promise<any> = (fileName) =>
window.coreAPI?.abortDownload(fileName);
/** /**
* Retrieves the path to the app data directory using the `coreAPI` object. * Retrieves the path to the app data directory using the `coreAPI` object.
* If the `coreAPI` object is not available, the function returns `undefined`. * If the `coreAPI` object is not available, the function returns `undefined`.
@ -79,6 +87,7 @@ export const core = {
invokePluginFunc, invokePluginFunc,
executeOnMain, executeOnMain,
downloadFile, downloadFile,
abortDownload,
deleteFile, deleteFile,
appDataPath, appDataPath,
getUserSpace, getUserSpace,
@ -91,6 +100,7 @@ export {
invokePluginFunc, invokePluginFunc,
executeOnMain, executeOnMain,
downloadFile, downloadFile,
abortDownload,
deleteFile, deleteFile,
appDataPath, appDataPath,
getUserSpace, getUserSpace,

View File

@ -8,7 +8,13 @@ export { core, deleteFile, invokePluginFunc } from "./core";
* Core module exports. * Core module exports.
* @module * @module
*/ */
export { downloadFile, executeOnMain, appDataPath, getUserSpace } from "./core"; export {
downloadFile,
executeOnMain,
appDataPath,
getUserSpace,
abortDownload,
} from "./core";
/** /**
* Events module exports. * Events module exports.

View File

@ -16,6 +16,14 @@ export abstract class ModelPlugin extends JanPlugin {
*/ */
abstract downloadModel(model: Model): Promise<void>; abstract downloadModel(model: Model): Promise<void>;
/**
* Cancels the download of a specific model.
* @param {string} name - The name of the model to cancel the download for.
* @param {string} modelId - The ID of the model to cancel the download for.
* @returns {Promise<void>} A promise that resolves when the download has been cancelled.
*/
abstract cancelModelDownload(name: string, modelId: string): Promise<void>;
/** /**
* Deletes a model. * Deletes a model.
* @param filePath - The file path of the model to delete. * @param filePath - The file path of the model to delete.

View File

@ -1,4 +1,4 @@
import { PluginType, fs, downloadFile } from '@janhq/core' import { PluginType, fs, downloadFile, abortDownload } from '@janhq/core'
import { ModelPlugin } from '@janhq/core/lib/plugins' import { ModelPlugin } from '@janhq/core/lib/plugins'
import { Model, ModelCatalog } from '@janhq/core/lib/types' import { Model, ModelCatalog } from '@janhq/core/lib/types'
import { parseToModel } from './helpers/modelParser' import { parseToModel } from './helpers/modelParser'
@ -50,6 +50,15 @@ export default class JanModelPlugin implements ModelPlugin {
downloadFile(model.downloadLink, path) downloadFile(model.downloadLink, path)
} }
/**
* Cancels the download of a specific machine learning model.
* @param {string} modelId - The ID of the model whose download is to be cancelled.
* @returns {Promise<void>} A promise that resolves when the download has been cancelled.
*/
async cancelModelDownload(name: string, modelId: string): Promise<void> {
return abortDownload(join(JanModelPlugin._homeDir, name, modelId))
}
/** /**
* Deletes a machine learning model. * Deletes a machine learning model.
* @param filePath - The path to the model file to delete. * @param filePath - The path to the model file to delete.

View File

@ -1,5 +1,7 @@
import { Fragment } from 'react' import { Fragment } from 'react'
import { PluginType } from '@janhq/core'
import { ModelPlugin } from '@janhq/core/lib/plugins'
import { import {
Progress, Progress,
Modal, Modal,
@ -10,12 +12,18 @@ import {
ModalTrigger, ModalTrigger,
} from '@janhq/uikit' } from '@janhq/uikit'
import { useAtomValue } from 'jotai'
import { useDownloadState } from '@/hooks/useDownloadState' import { useDownloadState } from '@/hooks/useDownloadState'
import { formatDownloadPercentage } from '@/utils/converter' import { formatDownloadPercentage } from '@/utils/converter'
import { downloadingModelsAtom } from '@/helpers/atoms/Model.atom'
import { pluginManager } from '@/plugin'
export default function DownloadingState() { export default function DownloadingState() {
const { downloadStates } = useDownloadState() const { downloadStates } = useDownloadState()
const models = useAtomValue(downloadingModelsAtom)
const totalCurrentProgress = downloadStates const totalCurrentProgress = downloadStates
.map((a) => a.size.transferred + a.size.transferred) .map((a) => a.size.transferred + a.size.transferred)
@ -67,11 +75,17 @@ export default function DownloadingState() {
<Button <Button
themes="outline" themes="outline"
size="sm" size="sm"
onClick={() => onClick={() => {
window.coreAPI?.abortDownload( if (item?.fileName) {
`models/${item?.fileName}` const model = models.find(
) (e) => e.id === item?.fileName
} )
if (!model) return
pluginManager
.get<ModelPlugin>(PluginType.Model)
?.cancelModelDownload(model.name, item.fileName)
}
}}
> >
Cancel Cancel
</Button> </Button>

View File

@ -1,5 +1,7 @@
import { useMemo } from 'react' import { useMemo } from 'react'
import { PluginType } from '@janhq/core'
import { ModelPlugin } from '@janhq/core/lib/plugins'
import { ModelVersion } from '@janhq/core/lib/types' import { ModelVersion } from '@janhq/core/lib/types'
import { import {
@ -19,6 +21,9 @@ import { useDownloadState } from '@/hooks/useDownloadState'
import { formatDownloadPercentage } from '@/utils/converter' import { formatDownloadPercentage } from '@/utils/converter'
import { downloadingModelsAtom } from '@/helpers/atoms/Model.atom'
import { pluginManager } from '@/plugin'
type Props = { type Props = {
suitableModel: ModelVersion suitableModel: ModelVersion
isFromList?: boolean isFromList?: boolean
@ -34,6 +39,7 @@ export default function ModalCancelDownload({
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
[suitableModel.name] [suitableModel.name]
) )
const models = useAtomValue(downloadingModelsAtom)
const downloadState = useAtomValue(downloadAtom) const downloadState = useAtomValue(downloadAtom)
return ( return (
@ -66,10 +72,15 @@ export default function ModalCancelDownload({
<Button <Button
themes="danger" themes="danger"
onClick={() => { onClick={() => {
if (downloadState?.fileName) if (downloadState?.fileName) {
window.coreAPI?.abortDownload( const model = models.find(
`models/${downloadState?.fileName}` (e) => e.id === downloadState?.fileName
) )
if (!model) return
pluginManager
.get<ModelPlugin>(PluginType.Model)
?.cancelModelDownload(model.name, downloadState.fileName)
}
}} }}
> >
Yes Yes