feat(fs): add stat API for markdown file validation (#774)
Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
This commit is contained in:
committed by
GitHub
co-authored by
Bohdan Triapitsyn
parent
0e70422835
commit
14fc741cc9
@@ -180,6 +180,39 @@ export async function handleFsBridgeMessage(
|
||||
}
|
||||
}
|
||||
|
||||
case 'api:fs:stat': {
|
||||
const target = (payload as { path: string })?.path;
|
||||
if (!target) {
|
||||
return { id, type, success: false, error: 'Path is required' };
|
||||
}
|
||||
|
||||
const resolution = await deps.resolveFileReadPath(target);
|
||||
if (!resolution.ok) {
|
||||
return { id, type, success: false, error: resolution.error };
|
||||
}
|
||||
|
||||
try {
|
||||
const stats = await fs.promises.stat(resolution.resolvedPath);
|
||||
if (!stats.isFile()) {
|
||||
return { id, type, success: false, error: 'Specified path is not a file' };
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
type,
|
||||
success: true,
|
||||
data: {
|
||||
path: deps.normalizeFsPath(resolution.resolvedPath),
|
||||
isFile: true,
|
||||
size: stats.size,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to stat file';
|
||||
return { id, type, success: false, error: message };
|
||||
}
|
||||
}
|
||||
|
||||
case 'api:fs:write': {
|
||||
const { path: targetPath, content } = (payload as { path: string; content: string }) || {};
|
||||
if (!targetPath) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as fs from 'fs';
|
||||
import { getFsMimeType, resolveFileReadPath, type FsReadPathResolution } from './bridge-fs-helpers-runtime';
|
||||
import { getFsMimeType, normalizeFsPath, resolveFileReadPath, type FsReadPathResolution } from './bridge-fs-helpers-runtime';
|
||||
|
||||
type ApiProxyResponsePayload = {
|
||||
status: number;
|
||||
@@ -48,7 +48,7 @@ export const tryHandleLocalFsProxy = async (method: string, requestPath: string)
|
||||
return buildProxyJsonError(400, 'Invalid request path');
|
||||
}
|
||||
|
||||
if (parsed.pathname !== '/api/fs/read' && parsed.pathname !== '/api/fs/raw') {
|
||||
if (parsed.pathname !== '/api/fs/stat' && parsed.pathname !== '/api/fs/read' && parsed.pathname !== '/api/fs/raw') {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -68,6 +68,21 @@ export const tryHandleLocalFsProxy = async (method: string, requestPath: string)
|
||||
return buildProxyJsonError(400, 'Specified path is not a file');
|
||||
}
|
||||
|
||||
if (parsed.pathname === '/api/fs/stat') {
|
||||
return {
|
||||
status: 200,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'cache-control': 'no-store',
|
||||
},
|
||||
bodyBase64: base64EncodeUtf8(JSON.stringify({
|
||||
path: normalizeFsPath(resolution.resolvedPath),
|
||||
isFile: true,
|
||||
size: stats.size,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
if (parsed.pathname === '/api/fs/read') {
|
||||
const content = await fs.promises.readFile(resolution.resolvedPath, 'utf8');
|
||||
return {
|
||||
@@ -94,6 +109,9 @@ export const tryHandleLocalFsProxy = async (method: string, requestPath: string)
|
||||
if (err?.code === 'ENOENT') {
|
||||
return buildProxyJsonError(404, 'File not found');
|
||||
}
|
||||
if (parsed.pathname === '/api/fs/stat') {
|
||||
return buildProxyJsonError(500, 'Unable to stat file');
|
||||
}
|
||||
return buildProxyJsonError(500, 'Unable to read file');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user