refactor: centralize file API URL construction and path resolution (#194)

- Extract getFileApiUrl helper with options interface for type-safe URL building
- Handle paths containing '.' and '..' segments via query parameters
- Remove duplicate URL construction logic from FileBrowser, FilePreview, and FileTree components
- Add getSpecialRoutePathFromRequest helper in backend for consistent path extraction
This commit is contained in:
Chris Scott authored and GitHub committed 2026-04-23 17:21:44 -04:00
1 parent 31755c23f8
commit 7a7090101c
5 files changed
+58 -54

No files matched your search

+34 -10
View File
@@ -3,12 +3,34 @@ import { fetchWrapper, fetchWrapperBlob } from './fetchWrapper'
import { API_BASE_URL } from '@/config'
import type { FileInfo, ChunkedFileInfo, PatchOperation } from '@/types/files'
function getFileApiUrl(path: string): string {
if (path.includes('..')) {
return `${API_BASE_URL}/api/files/?path=${encodeURIComponent(path)}`
interface FileApiUrlOptions {
route?: string
params?: Record<string, string | number | boolean | undefined>
}
function pathRequiresQuery(path: string): boolean {
return path.split('/').some(segment => segment === '.' || segment === '..')
}
export function getFileApiUrl(path: string, options: FileApiUrlOptions = {}): string {
const searchParams = new URLSearchParams()
Object.entries(options.params ?? {}).forEach(([key, value]) => {
if (value !== undefined) {
searchParams.append(key, String(value))
}
})
const routePath = options.route ? `/${options.route}` : ''
if (pathRequiresQuery(path)) {
searchParams.set('path', path)
const query = searchParams.toString()
return `${API_BASE_URL}/api/files${routePath}${query ? `?${query}` : ''}`
}
return `${API_BASE_URL}/api/files/${path.split('/').map(encodeURIComponent).join('/')}`
const encodedPath = path.split('/').map(encodeURIComponent).join('/')
const query = searchParams.toString()
return `${API_BASE_URL}/api/files/${encodedPath}${routePath}${query ? `?${query}` : ''}`
}
async function fetchFile(path: string): Promise<FileInfo> {
@@ -38,7 +60,7 @@ export async function applyFilePatches(path: string, patches: PatchOperation[]):
}
export async function getIgnoredPaths(path: string): Promise<{ ignoredPaths: string[] }> {
return fetchWrapper(`${API_BASE_URL}/api/files/${path}/ignored-paths`)
return fetchWrapper(getFileApiUrl(path, { route: 'ignored-paths' }))
}
export interface DownloadOptions {
@@ -47,11 +69,13 @@ export interface DownloadOptions {
}
export async function downloadDirectoryAsZip(path: string, options?: DownloadOptions): Promise<void> {
const params = new URLSearchParams()
if (options?.includeGit) params.append('includeGit', 'true')
if (options?.includePaths?.length) params.append('includePaths', options.includePaths.join(','))
const url = `${API_BASE_URL}/api/files/${path}/download-zip${params.toString() ? '?' + params.toString() : ''}`
const url = getFileApiUrl(path, {
route: 'download-zip',
params: {
includeGit: options?.includeGit || undefined,
includePaths: options?.includePaths?.length ? options.includePaths.join(',') : undefined,
},
})
const blob = await fetchWrapperBlob(url)
const urlObj = window.URL.createObjectURL(blob)