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

View File

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