From c6be66e5959f1c53ded5a97c55ac545aa3bb4fd0 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Fri, 26 Sep 2025 14:57:07 +0530 Subject: [PATCH] refactor(utils): add helper to remove extensions from file paths --- extensions/llamacpp-extension/src/util.ts | 20 ++++++++++++++++++++ web-app/src/lib/utils.ts | 17 +++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/extensions/llamacpp-extension/src/util.ts b/extensions/llamacpp-extension/src/util.ts index 1511eafec..b72766579 100644 --- a/extensions/llamacpp-extension/src/util.ts +++ b/extensions/llamacpp-extension/src/util.ts @@ -1,3 +1,23 @@ +// File path utilities +export function basenameNoExt(filePath: string): string { + const VALID_EXTENSIONS = [".tar.gz", ".zip"]; + + // handle VALID extensions first + for (const ext of VALID_EXTENSIONS) { + if (filePath.toLowerCase().endsWith(ext)) { + return filePath.slice(0, -ext.length); + } + } + + // fallback: remove only the last extension + const lastDotIndex = filePath.lastIndexOf('.'); + if (lastDotIndex > 0) { + return filePath.slice(0, lastDotIndex); + } + + return filePath; +} + // Zustand proxy state structure interface ProxyState { proxyEnabled: boolean diff --git a/web-app/src/lib/utils.ts b/web-app/src/lib/utils.ts index 60a055720..d9bfa0ecb 100644 --- a/web-app/src/lib/utils.ts +++ b/web-app/src/lib/utils.ts @@ -1,11 +1,28 @@ import { type ClassValue, clsx } from 'clsx' import { twMerge } from 'tailwind-merge' import { ExtensionManager } from './extension' +import path from "path" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +export function basenameNoExt(filePath: string): string { + const base = path.basename(filePath); + const VALID_EXTENSIONS = [".tar.gz", ".zip"]; + + // handle VALID extensions first + for (const ext of VALID_EXTENSIONS) { + if (base.toLowerCase().endsWith(ext)) { + return base.slice(0, -ext.length); + } + } + + // fallback: remove only the last extension + return base.slice(0, -path.extname(base).length); +} + export function getProviderLogo(provider: string) { switch (provider) { case 'jan':