refactor: safely strip prefix and extensions from filename

This commit is contained in:
Roushan Singh 2025-09-26 15:00:10 +05:30
parent 7d6e0c22ac
commit c091b8cd77
2 changed files with 5 additions and 10 deletions

View File

@ -1,9 +1,8 @@
import { getJanDataFolderPath, fs, joinPath, events } from '@janhq/core'
import { invoke } from '@tauri-apps/api/core'
import { getProxyConfig } from './util'
import { getProxyConfig, basenameNoExt } from './util'
import { dirname, basename } from '@tauri-apps/api/path'
import { getSystemInfo } from '@janhq/tauri-plugin-hardware-api'
/*
* Reads currently installed backends in janDataFolderPath
*
@ -73,10 +72,7 @@ async function fetchRemoteSupportedBackends(
if (!name.startsWith(prefix)) continue
const backend = name
.replace(prefix, '')
.replace('.tar.gz', '')
.replace('.zip', '')
const backend = basenameNoExt(name).slice(prefix.length)
if (supportedBackends.includes(backend)) {
remote.push({ version, backend })

View File

@ -41,6 +41,7 @@ import { useLlamacppDevices } from '@/hooks/useLlamacppDevices'
import { PlatformFeatures } from '@/lib/platform/const'
import { PlatformFeature } from '@/lib/platform/types'
import { useBackendUpdater } from '@/hooks/useBackendUpdater'
import { basenameNoExt } from '@/lib/utils'
// as route.threadsDetail
export const Route = createFileRoute('/settings/providers/$providerName')({
@ -382,7 +383,7 @@ function ProviderDetail() {
filters: [
{
name: 'Backend Archives',
extensions: ['tar.gz', 'zip'],
extensions: ['tar.gz', 'zip', 'gz'],
},
],
})
@ -394,9 +395,7 @@ function ProviderDetail() {
await installBackend(selectedFile)
// Extract filename from the selected file path and replace spaces with dashes
const fileName = (
selectedFile.split(/[/\\]/).pop() || selectedFile
).replace(/\s+/g, '-')
const fileName = basenameNoExt(selectedFile).replace(/\s+/g, "-")
toast.success(t('settings:backendInstallSuccess'), {
description: `Llamacpp ${fileName} installed`,