2026-01-15 20:19:06 +02:00
|
|
|
import type {
|
|
|
|
|
DirectoryListResult,
|
|
|
|
|
FileSearchQuery,
|
|
|
|
|
FileSearchResult,
|
|
|
|
|
FilesAPI,
|
|
|
|
|
} from '@openchamber/ui/lib/api/types';
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
const normalizePath = (path: string): string => path.replace(/\\/g, '/');
|
|
|
|
|
|
2026-01-15 20:19:06 +02:00
|
|
|
type WebDirectoryEntry = {
|
|
|
|
|
name?: string;
|
|
|
|
|
path?: string;
|
|
|
|
|
isDirectory?: boolean;
|
|
|
|
|
isFile?: boolean;
|
|
|
|
|
isSymbolicLink?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type WebDirectoryListResponse = {
|
|
|
|
|
directory?: string;
|
|
|
|
|
path?: string;
|
|
|
|
|
entries?: WebDirectoryEntry[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type WebFileSearchResponse = {
|
|
|
|
|
root?: string;
|
|
|
|
|
directory?: string;
|
|
|
|
|
count?: number;
|
|
|
|
|
files?: Array<{
|
|
|
|
|
name?: string;
|
|
|
|
|
path?: string;
|
|
|
|
|
relativePath?: string;
|
|
|
|
|
extension?: string;
|
|
|
|
|
}>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const toDirectoryListResult = (fallbackDirectory: string, payload: WebDirectoryListResponse): DirectoryListResult => {
|
|
|
|
|
const directory = normalizePath(payload?.directory || payload?.path || fallbackDirectory);
|
|
|
|
|
const entries = Array.isArray(payload?.entries) ? payload.entries : [];
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
directory,
|
|
|
|
|
entries: entries
|
|
|
|
|
.filter((entry): entry is Required<Pick<WebDirectoryEntry, 'name' | 'path'>> & { isDirectory?: boolean } =>
|
|
|
|
|
Boolean(entry && typeof entry.name === 'string' && typeof entry.path === 'string')
|
|
|
|
|
)
|
|
|
|
|
.map((entry) => ({
|
|
|
|
|
name: entry.name,
|
|
|
|
|
path: normalizePath(entry.path),
|
|
|
|
|
isDirectory: Boolean(entry.isDirectory),
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
export const createWebFilesAPI = (): FilesAPI => ({
|
|
|
|
|
async listDirectory(path: string): Promise<DirectoryListResult> {
|
|
|
|
|
const target = normalizePath(path);
|
2026-01-15 20:19:06 +02:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
if (target) {
|
|
|
|
|
params.set('path', target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await fetch(`/api/fs/list${params.toString() ? `?${params.toString()}` : ''}`);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const error = await response.json().catch(() => ({ error: response.statusText }));
|
2026-01-15 20:19:06 +02:00
|
|
|
throw new Error((error as { error?: string }).error || 'Failed to list directory');
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 20:19:06 +02:00
|
|
|
const result = (await response.json()) as WebDirectoryListResponse;
|
|
|
|
|
return toDirectoryListResult(target, result);
|
2025-12-07 19:32:53 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async search(payload: FileSearchQuery): Promise<FileSearchResult[]> {
|
2026-01-15 20:19:06 +02:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
|
|
|
|
|
const directory = normalizePath(payload.directory);
|
|
|
|
|
if (directory) {
|
|
|
|
|
params.set('directory', directory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params.set('q', payload.query);
|
|
|
|
|
|
|
|
|
|
if (typeof payload.maxResults === 'number' && Number.isFinite(payload.maxResults)) {
|
|
|
|
|
params.set('limit', String(payload.maxResults));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await fetch(`/api/fs/search?${params.toString()}`);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const error = await response.json().catch(() => ({ error: response.statusText }));
|
2026-01-15 20:19:06 +02:00
|
|
|
throw new Error((error as { error?: string }).error || 'Failed to search files');
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 20:19:06 +02:00
|
|
|
const result = (await response.json()) as WebFileSearchResponse;
|
|
|
|
|
const files = Array.isArray(result?.files) ? result.files : [];
|
|
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
.filter((file): file is { path: string; relativePath?: string } =>
|
|
|
|
|
Boolean(file && typeof file.path === 'string')
|
|
|
|
|
)
|
|
|
|
|
.map((file) => ({
|
|
|
|
|
path: normalizePath(file.path),
|
|
|
|
|
preview: typeof file.relativePath === 'string' && file.relativePath.length > 0
|
|
|
|
|
? [normalizePath(file.relativePath)]
|
|
|
|
|
: undefined,
|
2025-12-07 19:32:53 +02:00
|
|
|
}));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async createDirectory(path: string): Promise<{ success: boolean; path: string }> {
|
|
|
|
|
const target = normalizePath(path);
|
|
|
|
|
const response = await fetch('/api/fs/mkdir', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ path: target }),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const error = await response.json().catch(() => ({ error: response.statusText }));
|
2026-01-15 20:19:06 +02:00
|
|
|
throw new Error((error as { error?: string }).error || 'Failed to create directory');
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await response.json();
|
|
|
|
|
return {
|
|
|
|
|
success: Boolean(result?.success),
|
|
|
|
|
path: typeof result?.path === 'string' ? normalizePath(result.path) : target,
|
|
|
|
|
};
|
|
|
|
|
},
|
2026-01-15 20:19:06 +02:00
|
|
|
|
|
|
|
|
async readFile(path: string): Promise<{ content: string; path: string }> {
|
|
|
|
|
const target = normalizePath(path);
|
|
|
|
|
const response = await fetch(`/api/fs/read?path=${encodeURIComponent(target)}`);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const error = await response.json().catch(() => ({ error: response.statusText }));
|
|
|
|
|
throw new Error((error as { error?: string }).error || 'Failed to read file');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const content = await response.text();
|
|
|
|
|
return { content, path: target };
|
|
|
|
|
},
|
2025-12-07 19:32:53 +02:00
|
|
|
});
|