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
@@ -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