import type { FileContentDTO, FileItemDTO, PathExistsDTO, SettingsDTO } from '@shared/types' import { fetchApi } from './http' export type FileItem = FileItemDTO export type FileContent = FileContentDTO export type Settings = SettingsDTO export type UploadedImage = { name: string; path: string } export const searchFiles = async (keywords: string[]): Promise => { const data = await fetchApi<{ items: FileItem[] }>('/api/search', { method: 'POST', body: { keywords }, }) return data.items } export const fetchFiles = async (path: string = ''): Promise => { const data = await fetchApi<{ items: FileItem[] }>(`/api/files?path=${encodeURIComponent(path)}`) return data.items } export const fetchFileContent = async (path: string): Promise => { return await fetchApi(`/api/files/content?path=${encodeURIComponent(path)}`) } export const saveFileContent = async (path: string, content: string): Promise => { await fetchApi('/api/files/save', { method: 'POST', body: { path, content }, }) } export const uploadClipboardImage = async (image: string): Promise => { return await fetchApi('/api/files/upload/image', { method: 'POST', body: { image }, }) } export const deleteFile = async (path: string): Promise => { await fetchApi('/api/files/delete', { method: 'DELETE', body: { path }, }) } export const createDirectory = async (path: string): Promise => { await fetchApi('/api/files/create/dir', { method: 'POST', body: { path }, }) } export const createFile = async (path: string): Promise => { await fetchApi('/api/files/create/file', { method: 'POST', body: { path }, }) } export const checkPathExists = async (path: string): Promise => { return await fetchApi('/api/files/exists', { method: 'POST', body: { path }, }) } export const renameItem = async (oldPath: string, newPath: string): Promise => { await fetchApi('/api/files/rename', { method: 'POST', body: { oldPath, newPath }, }) } export const runAiTask = async (task: string, path: string): Promise => { await fetchApi('/api/ai/doubao', { method: 'POST', body: { task, path }, }) } export const getSettings = async (): Promise => { return await fetchApi('/api/settings') } export const getSettingsConfig = async (): Promise<{ notebookRoot: string }> => { return await fetchApi<{ notebookRoot: string }>('/api/settings/config') } export const saveSettings = async (settings: Settings): Promise => { return await fetchApi('/api/settings', { method: 'POST', body: settings, }) } export const uploadPdfForParsing = async (file: File, targetPath: string): Promise => { const formData = new FormData() formData.append('file', file) formData.append('targetPath', targetPath) await fetchApi('/api/mineru/parse', { method: 'POST', body: formData, }) } export interface LocalHtmlInfo { htmlPath: string htmlDir: string assetsDirName?: string assetsFiles?: string[] } export const parseLocalHtml = async (info: LocalHtmlInfo, targetPath: string): Promise => { await fetchApi('/api/blog/parse-local', { method: 'POST', body: { htmlPath: info.htmlPath, htmlDir: info.htmlDir, assetsDirName: info.assetsDirName, assetsFiles: info.assetsFiles, targetPath, }, }) }