jan/web/hooks/useDownloadModel.ts
NamH 42da19a463
fix: download mutilple binaries (#2043)
Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
2024-02-16 11:32:14 +07:00

93 lines
2.2 KiB
TypeScript

import { useCallback, useContext } from 'react'
import {
Model,
ExtensionTypeEnum,
ModelExtension,
abortDownload,
joinPath,
ModelArtifact,
DownloadState,
} from '@janhq/core'
import { useSetAtom } from 'jotai'
import { FeatureToggleContext } from '@/context/FeatureToggle'
import { setDownloadStateAtom } from './useDownloadState'
import { extensionManager } from '@/extension/ExtensionManager'
import { addDownloadingModelAtom } from '@/helpers/atoms/Model.atom'
export default function useDownloadModel() {
const { ignoreSSL, proxy } = useContext(FeatureToggleContext)
const setDownloadState = useSetAtom(setDownloadStateAtom)
const addDownloadingModel = useSetAtom(addDownloadingModelAtom)
const downloadModel = useCallback(
async (model: Model) => {
const childProgresses: DownloadState[] = model.sources.map(
(source: ModelArtifact) => ({
fileName: source.filename,
modelId: model.id,
time: {
elapsed: 0,
remaining: 0,
},
speed: 0,
percent: 0,
size: {
total: 0,
transferred: 0,
},
downloadState: 'downloading',
})
)
// set an initial download state
setDownloadState({
fileName: '',
modelId: model.id,
time: {
elapsed: 0,
remaining: 0,
},
speed: 0,
percent: 0,
size: {
total: 0,
transferred: 0,
},
children: childProgresses,
downloadState: 'downloading',
})
addDownloadingModel(model)
await localDownloadModel(model, ignoreSSL, proxy)
},
[ignoreSSL, proxy, addDownloadingModel, setDownloadState]
)
const abortModelDownload = useCallback(async (model: Model) => {
for (const source of model.sources) {
const path = await joinPath(['models', model.id, source.filename])
await abortDownload(path)
}
}, [])
return {
downloadModel,
abortModelDownload,
}
}
const localDownloadModel = async (
model: Model,
ignoreSSL: boolean,
proxy: string
) =>
extensionManager
.get<ModelExtension>(ExtensionTypeEnum.Model)
?.downloadModel(model, { ignoreSSL, proxy })