Add configurable max_file_size option

- Default 5 MB for template files
- Configurable via ViewEngine.init options
This commit is contained in:
2026-01-18 00:04:44 +05:30
parent 458de03c02
commit 0bccce8bf9

View File

@@ -45,6 +45,8 @@ pub const Options = struct {
extension: []const u8 = ".pug", extension: []const u8 = ".pug",
/// Enable pretty-printing with indentation. /// Enable pretty-printing with indentation.
pretty: bool = true, pretty: bool = true,
/// Maximum template file size in bytes. Defaults to 5 MB.
max_file_size: usize = 5 * 1024 * 1024,
}; };
/// Error types for ViewEngine operations. /// Error types for ViewEngine operations.
@@ -88,7 +90,7 @@ pub const ViewEngine = struct {
defer allocator.free(full_path); defer allocator.free(full_path);
// Read template file // Read template file
const source = std.fs.cwd().readFileAlloc(allocator, full_path, 1024 * 1024) catch { const source = std.fs.cwd().readFileAlloc(allocator, full_path, self.options.max_file_size) catch {
return ViewEngineError.TemplateNotFound; return ViewEngineError.TemplateNotFound;
}; };
defer allocator.free(source); defer allocator.free(source);
@@ -148,7 +150,7 @@ pub const ViewEngine = struct {
return rt.renderOwned(doc); return rt.renderOwned(doc);
} }
/// Resolves a template path relative to views directory. /// Resolves a template path relative to views directory, adding extension if needed.
fn resolvePath(self: *const ViewEngine, allocator: std.mem.Allocator, template_path: []const u8) ![]const u8 { fn resolvePath(self: *const ViewEngine, allocator: std.mem.Allocator, template_path: []const u8) ![]const u8 {
// Add extension if not present // Add extension if not present
const with_ext = if (std.mem.endsWith(u8, template_path, self.options.extension)) const with_ext = if (std.mem.endsWith(u8, template_path, self.options.extension))
@@ -164,7 +166,7 @@ pub const ViewEngine = struct {
fn createFileResolver() runtime.FileResolver { fn createFileResolver() runtime.FileResolver {
return struct { return struct {
fn resolve(allocator: std.mem.Allocator, path: []const u8) ?[]const u8 { fn resolve(allocator: std.mem.Allocator, path: []const u8) ?[]const u8 {
return std.fs.cwd().readFileAlloc(allocator, path, 1024 * 1024) catch null; return std.fs.cwd().readFileAlloc(allocator, path, 5 * 1024 * 1024) catch null;
} }
}.resolve; }.resolve;
} }