Skip to content

Commit 57b18a9

Browse files
committed
feat: add 50mb limit for local files
1 parent a9510bf commit 57b18a9

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/strategies/LocalFileStrategy.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { JsonIngestionResult } from '../types/JsonIngestion.js';
77
* Strategy for ingesting JSON from local file system files
88
*/
99
export class LocalFileStrategy extends JsonIngestionStrategy {
10+
private readonly maxFileSize: number = 50 * 1024 * 1024; // 50MB limit
1011
canHandle(source: string): boolean {
1112
// Handle local file paths - not URLs
1213
// Covers: ./file.json, /abs/path/file.json, C:\Windows\file.json, file.json
@@ -18,6 +19,37 @@ export class LocalFileStrategy extends JsonIngestionStrategy {
1819
// Resolve and validate file path - keeping existing logic
1920
const resolvedPath = resolve(source);
2021

22+
// Check file size before reading
23+
let fileStats;
24+
try {
25+
fileStats = await fs.stat(resolvedPath);
26+
} catch (error) {
27+
return {
28+
success: false,
29+
error: {
30+
type: 'file_not_found',
31+
message: `File not found: ${resolvedPath}`,
32+
details: error
33+
}
34+
};
35+
}
36+
37+
// Check if file size exceeds limit
38+
if (fileStats.size > this.maxFileSize) {
39+
return {
40+
success: false,
41+
error: {
42+
type: 'content_too_large',
43+
message: `File too large (${Math.round(fileStats.size / 1024 / 1024)}MB). This tool is optimized for JSON files under ${Math.round(this.maxFileSize / 1024 / 1024)}MB.`,
44+
details: {
45+
fileSize: fileStats.size,
46+
maxSize: this.maxFileSize,
47+
filePath: resolvedPath
48+
}
49+
}
50+
};
51+
}
52+
2153
// Read file content - keeping existing error handling
2254
let jsonContent: string;
2355
try {
@@ -27,7 +59,7 @@ export class LocalFileStrategy extends JsonIngestionStrategy {
2759
success: false,
2860
error: {
2961
type: 'file_not_found',
30-
message: `File not found: ${resolvedPath}`,
62+
message: `Failed to read file: ${resolvedPath}`,
3163
details: error
3264
}
3365
};
@@ -51,8 +83,8 @@ export class LocalFileStrategy extends JsonIngestionStrategy {
5183
getMetadata() {
5284
return {
5385
name: 'LocalFileStrategy',
54-
description: 'Reads JSON data from local file system',
55-
supportedSources: ['Local file paths (relative and absolute)']
86+
description: 'Reads JSON data from local file system with 50MB size limit',
87+
supportedSources: ['Local file paths (relative and absolute)', 'JSON files under 50MB']
5688
};
5789
}
5890
}

0 commit comments

Comments
 (0)