irondrop 2.6.4

Drop files, not dependencies - a well tested fully featured & battle-ready server in a single Rust binary with support for indexing through 10M files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// SPDX-License-Identifier: MIT

//! Template loading and rendering system for modular HTML

use crate::error::AppError;
use log::{debug, trace};
use std::collections::HashMap;
use std::sync::OnceLock;

// Embed templates at compile time
// Base template
const BASE_HTML: &str = include_str!("../templates/common/base.html");

// Content templates
const DIRECTORY_CONTENT_HTML: &str = include_str!("../templates/directory/content.html");
const ERROR_CONTENT_HTML: &str = include_str!("../templates/error/content.html");
const UPLOAD_CONTENT_HTML: &str = include_str!("../templates/upload/content.html");
const UPLOAD_SUCCESS_HTML: &str = include_str!("../templates/upload/success.html");

// CSS and JS assets
const DIRECTORY_STYLES_CSS: &str = include_str!("../templates/directory/styles.css");
const DIRECTORY_SCRIPT_JS: &str = include_str!("../templates/directory/script.js");
const ERROR_STYLES_CSS: &str = include_str!("../templates/error/styles.css");
const ERROR_SCRIPT_JS: &str = include_str!("../templates/error/script.js");
const UPLOAD_STYLES_CSS: &str = include_str!("../templates/upload/styles.css");
const UPLOAD_SCRIPT_JS: &str = include_str!("../templates/upload/script.js");
const UPLOAD_FORM_HTML: &str = include_str!("../templates/upload/form.html");

// Monitor templates
const MONITOR_CONTENT_HTML: &str = include_str!("../templates/monitor/content.html");
const MONITOR_STYLES_CSS: &str = include_str!("../templates/monitor/styles.css");
const MONITOR_SCRIPT_JS: &str = include_str!("../templates/monitor/script.js");

// Common base styles
const BASE_CSS: &str = include_str!("../templates/common/base.css");

// Embed favicon files at compile time
const FAVICON_ICO: &[u8] = include_bytes!("../favicon.ico");
const FAVICON_16X16_PNG: &[u8] = include_bytes!("../favicon-16x16.png");
const FAVICON_32X32_PNG: &[u8] = include_bytes!("../favicon-32x32.png");
// Logo image
const IRONDROP_LOGO_PNG: &[u8] = include_bytes!("../irondrop-logo.png");

// Icon partials
const FOLDER_ICON_SVG: &str = include_str!("../templates/directory/folder_icon.svg");
const FILE_ICON_SVG: &str = include_str!("../templates/directory/file_icon.svg");
const BACK_ICON_SVG: &str = include_str!("../templates/directory/back_icon.svg");
const ZIP_ICON_SVG: &str = include_str!("../templates/directory/zip_icon.svg");
const IMAGE_ICON_SVG: &str = include_str!("../templates/directory/image_icon.svg");
const VIDEO_ICON_SVG: &str = include_str!("../templates/directory/video_icon.svg");

/// Template loader and renderer for modular HTML templates
pub struct TemplateEngine {
    templates: HashMap<&'static str, &'static str>,
}

static TEMPLATE_ENGINE: OnceLock<TemplateEngine> = OnceLock::new();

impl Default for TemplateEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl TemplateEngine {
    /// Create a new template engine with embedded templates
    pub fn new() -> Self {
        let mut templates: HashMap<&'static str, &'static str> = HashMap::new();

        // Load base template
        templates.insert("base", BASE_HTML);

        // Load content templates
        templates.insert("directory_content", DIRECTORY_CONTENT_HTML);
        templates.insert("error_content", ERROR_CONTENT_HTML);
        templates.insert("upload_content", UPLOAD_CONTENT_HTML);
        templates.insert("upload_success", UPLOAD_SUCCESS_HTML);
        templates.insert("upload_form", UPLOAD_FORM_HTML);
        templates.insert("monitor_content", MONITOR_CONTENT_HTML);

        Self { templates }
    }

    /// Global singleton instance to avoid per-request allocations
    pub fn global() -> &'static Self {
        TEMPLATE_ENGINE.get_or_init(Self::new)
    }

    /// Get appropriate icon SVG based on file extension
    fn get_file_icon(filename: &str) -> &'static str {
        let extension = filename.split('.').next_back().unwrap_or("").to_lowercase();
        match extension.as_str() {
            // Archive formats
            "zip" | "rar" | "7z" | "tar" | "gz" | "bz2" | "xz" => ZIP_ICON_SVG,
            // Image formats
            "jpg" | "jpeg" | "png" | "gif" | "bmp" | "webp" | "svg" | "ico" | "tiff" => {
                IMAGE_ICON_SVG
            }
            // Video formats
            "mp4" | "avi" | "mkv" | "mov" | "wmv" | "flv" | "webm" | "m4v" => VIDEO_ICON_SVG,
            // Default file icon
            _ => FILE_ICON_SVG,
        }
    }

    /// Load all templates - now uses embedded templates
    pub fn load_all_templates(&mut self) -> Result<(), AppError> {
        // Templates are already loaded in new(), this is kept for compatibility
        Ok(())
    }

    /// Get embedded static asset content
    pub fn get_static_asset(&self, path: &str) -> Option<(&'static str, &'static str)> {
        match path {
            // Common base styles
            "common/base.css" => Some((BASE_CSS, "text/css")),
            // Directory assets
            "directory/styles.css" => Some((DIRECTORY_STYLES_CSS, "text/css")),
            "directory/script.js" => Some((DIRECTORY_SCRIPT_JS, "application/javascript")),
            // Error assets
            "error/styles.css" => Some((ERROR_STYLES_CSS, "text/css")),
            "error/script.js" => Some((ERROR_SCRIPT_JS, "application/javascript")),
            // Upload assets
            "upload/styles.css" => Some((UPLOAD_STYLES_CSS, "text/css")),
            "upload/script.js" => Some((UPLOAD_SCRIPT_JS, "application/javascript")),
            // Monitor assets
            "monitor/styles.css" => Some((MONITOR_STYLES_CSS, "text/css")),
            "monitor/script.js" => Some((MONITOR_SCRIPT_JS, "application/javascript")),
            _ => None,
        }
    }

    /// Get embedded favicon as binary data
    pub fn get_favicon(&self, path: &str) -> Option<(&'static [u8], &'static str)> {
        match path {
            "favicon.ico" => Some((FAVICON_ICO, "image/x-icon")),
            "favicon-16x16.png" => Some((FAVICON_16X16_PNG, "image/png")),
            "favicon-32x32.png" => Some((FAVICON_32X32_PNG, "image/png")),
            "irondrop-logo.png" => Some((IRONDROP_LOGO_PNG, "image/png")),
            _ => None,
        }
    }

    /// Render a page using the base template system
    pub fn render_page(
        &self,
        content_template: &str,
        page_title: &str,
        page_styles: &str,
        page_scripts: &str,
        header_actions: &str,
        variables: &HashMap<String, String>,
    ) -> Result<String, AppError> {
        // First render the content template
        let content = self.render(content_template, variables)?;

        // Create variables for the base template
        let mut base_variables = variables.clone();
        base_variables.insert("PAGE_TITLE".to_string(), page_title.to_string());
        base_variables.insert("PAGE_STYLES".to_string(), page_styles.to_string());
        base_variables.insert("PAGE_SCRIPTS".to_string(), page_scripts.to_string());
        base_variables.insert("HEADER_ACTIONS".to_string(), header_actions.to_string());
        base_variables.insert("PAGE_CONTENT".to_string(), content);
        base_variables.insert("VERSION".to_string(), crate::VERSION.to_string());

        // Render the base template
        self.render("base", &base_variables)
    }

    /// Helper method to render directory page
    pub fn render_directory_page(
        &self,
        variables: &HashMap<String, String>,
    ) -> Result<String, AppError> {
        let default_path = "/".to_string();
        let raw_path = variables.get("PATH").unwrap_or(&default_path);
        // Clean up path for display: remove leading/trailing slashes, show "Root" for empty
        let page_title = if raw_path == "/" || raw_path.is_empty() {
            "Root".to_string()
        } else {
            raw_path
                .trim_start_matches('/')
                .trim_end_matches('/')
                .to_string()
        };
        let page_styles =
            r#"<link rel="stylesheet" href="/_irondrop/static/directory/styles.css">"#;
        let page_scripts = r#"<script src="/_irondrop/static/directory/script.js"></script>"#;

        // Build header actions based on upload status
        let header_actions = if variables
            .get("UPLOAD_ENABLED")
            .map(|v| v == "true")
            .unwrap_or(false)
        {
            let suffix = variables
                .get("QUERY_UPLOAD_SUFFIX")
                .unwrap_or(&String::new())
                .clone();
            format!(
                r#"<a href="/_irondrop/upload{suffix}" class="btn btn-light">
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
                        <polyline points="17,8 12,3 7,8" />
                        <line x1="12" y1="3" x2="12" y2="15" />
                    </svg>
                    Upload Files
                </a>"#
            )
        } else {
            String::new()
        };

        // Add cleaned path for display in the directory header
        let display_title = if raw_path == "/" || raw_path.is_empty() {
            "Root".to_string()
        } else {
            raw_path.trim_end_matches('/').to_string()
        };

        // Create a mutable copy of variables and add the display title
        let mut enhanced_variables = variables.clone();
        enhanced_variables.insert("DISPLAY_TITLE".to_string(), display_title);

        self.render_page(
            "directory_content",
            &page_title,
            page_styles,
            page_scripts,
            &header_actions,
            &enhanced_variables,
        )
    }

    /// Helper method to render error page
    pub fn render_error_page_new(
        &self,
        error_code: u16,
        error_message: &str,
        error_description: &str,
    ) -> Result<String, AppError> {
        let mut variables = HashMap::new();
        variables.insert("ERROR_CODE".to_string(), error_code.to_string());
        variables.insert("ERROR_MESSAGE".to_string(), error_message.to_string());
        variables.insert(
            "ERROR_DESCRIPTION".to_string(),
            error_description.to_string(),
        );

        // Generate request ID and timestamp
        let request_id = format!(
            "req_{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis()
        );
        let timestamp = {
            use std::time::{SystemTime, UNIX_EPOCH};
            let since_epoch = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();

            // Convert to UTC time components
            const SECONDS_IN_DAY: u64 = 86400;
            const SECONDS_IN_HOUR: u64 = 3600;
            const SECONDS_IN_MINUTE: u64 = 60;

            let days_since_epoch = since_epoch / SECONDS_IN_DAY;
            let remaining_seconds = since_epoch % SECONDS_IN_DAY;

            let hours = remaining_seconds / SECONDS_IN_HOUR;
            let minutes = (remaining_seconds % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE;
            let seconds = remaining_seconds % SECONDS_IN_MINUTE;

            // Simple epoch to date conversion (approximate)
            // Start from 1970-01-01 and add days
            let mut year = 1970;
            let mut remaining_days = days_since_epoch;

            // Handle leap years (simplified)
            while remaining_days >= 365 {
                let days_in_year = if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
                    366
                } else {
                    365
                };

                if remaining_days >= days_in_year {
                    remaining_days -= days_in_year;
                    year += 1;
                } else {
                    break;
                }
            }

            // Simplified month/day calculation
            let month = (remaining_days / 31) + 1;
            let day = (remaining_days % 31) + 1;

            format!(
                "{:04}-{:02}-{:02} {:02}:{:02}:{:02} UTC",
                year,
                month.min(12),
                day.max(1),
                hours,
                minutes,
                seconds
            )
        };
        variables.insert("REQUEST_ID".to_string(), request_id);
        variables.insert("TIMESTAMP".to_string(), timestamp);
        variables.insert("VERSION".to_string(), crate::VERSION.to_string());

        let page_title = format!("{error_code} {error_message}");
        let page_styles = r#"<link rel="stylesheet" href="/_irondrop/static/error/styles.css">"#;
        let page_scripts = r#"<script src="/_irondrop/static/error/script.js"></script>"#;
        let header_actions = ""; // No actions on error page

        self.render_page(
            "error_content",
            &page_title,
            page_styles,
            page_scripts,
            header_actions,
            &variables,
        )
    }

    /// Helper method to render upload page
    pub fn render_upload_page_new(&self, path: &str) -> Result<String, AppError> {
        let mut variables = HashMap::new();
        variables.insert("PATH".to_string(), path.to_string());

        let page_title = format!("Upload to {path}");
        let page_styles = r#"<link rel="stylesheet" href="/_irondrop/static/upload/styles.css">"#;
        let page_scripts = r#"<script src="/_irondrop/static/upload/script.js"></script>"#;

        // Header action is back to directory
        let header_actions = format!(
            r#"<a href="{path}" class="btn btn-light" id="backToDir">
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="m12 19-7-7 7-7" />
                        <path d="m19 12H5" />
                    </svg>
                    Back to Directory
                </a>"#
        );

        self.render_page(
            "upload_content",
            &page_title,
            page_styles,
            page_scripts,
            &header_actions,
            &variables,
        )
    }

    /// Render a template with variables, supporting conditionals (optimized single-scan passes)
    pub fn render(
        &self,
        template_name: &str,
        variables: &HashMap<String, String>,
    ) -> Result<String, AppError> {
        let template = self.templates.get(template_name).ok_or_else(|| {
            AppError::InternalServerError(format!("Template '{template_name}' not found"))
        })?;

        // First pass: process simple {{#if VAR}}...{{/if}} blocks (no nesting)
        let conditional_processed = self.process_conditionals_optimized(template, variables);

        // Second pass: substitute variables {{VAR}} in a single scan
        let rendered = Self::substitute_variables_single_pass(&conditional_processed, variables);

        Ok(rendered)
    }

    /// Optimized conditional processing with pre-reserved buffer (no nested ifs)
    fn process_conditionals_optimized(
        &self,
        template: &str,
        variables: &HashMap<String, String>,
    ) -> String {
        let mut out: Vec<u8> = Vec::with_capacity(template.len());
        let bytes = template.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            // Look for b"{{#if "
            if i + 6 <= bytes.len() && bytes[i..].starts_with(b"{{#if ") {
                let var_start = i + 6;
                // Find closing b"}}" for variable name
                if let Some(close_pos_rel) = bytes[var_start..].windows(2).position(|w| w == b"}}")
                {
                    let var_end = var_start + close_pos_rel;
                    // SAFETY: variable names are ASCII in our templates; validate UTF-8
                    let var_name = std::str::from_utf8(&bytes[var_start..var_end]).unwrap_or("");

                    // Find matching b"{{/if}}" from after }}
                    let block_search_start = var_end + 2;
                    if let Some(end_rel) = bytes[block_search_start..]
                        .windows(7)
                        .position(|w| w == b"{{/if}}")
                    {
                        let block_end = block_search_start + end_rel;
                        let include = variables
                            .get(var_name)
                            .map(|v| v == "true")
                            .unwrap_or(false);
                        if include {
                            out.extend_from_slice(&bytes[block_search_start..block_end]);
                        }
                        i = block_end + 7; // skip entire if-block
                        continue;
                    }
                }
            }
            // Default: copy one byte
            out.push(bytes[i]);
            i += 1;
        }
        String::from_utf8(out).unwrap_or_else(|_| template.to_string())
    }

    /// Single-pass variable substitution for placeholders {{VAR}}
    fn substitute_variables_single_pass(
        input: &str,
        variables: &HashMap<String, String>,
    ) -> String {
        // Heuristic capacity: base + total value sizes capped
        let extra: usize = variables
            .values()
            .map(|v| v.len())
            .sum::<usize>()
            .min(64 * 1024);
        let mut out: Vec<u8> = Vec::with_capacity(input.len() + extra);
        let bytes = input.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            if i + 2 <= bytes.len() && bytes[i..].starts_with(b"{{") {
                // Find closing }}
                if let Some(close_rel) = bytes[i + 2..].windows(2).position(|w| w == b"}}") {
                    let name_bytes = &bytes[i + 2..i + 2 + close_rel];
                    if let Ok(name) = std::str::from_utf8(name_bytes)
                        && let Some(val) = variables.get(name)
                    {
                        out.extend_from_slice(val.as_bytes());
                    }
                    i = i + 2 + close_rel + 2;
                    continue;
                }
            }
            out.push(bytes[i]);
            i += 1;
        }
        String::from_utf8(out).unwrap_or_else(|_| input.to_string())
    }

    /// Generate directory listing HTML using base template system
    pub fn render_directory_listing(
        &self,
        path: &str,
        entries: &[(String, String, String)], // (name, size, date)
        entry_count: usize,
        upload_enabled: bool,
        current_path: &str,
    ) -> Result<String, AppError> {
        debug!(
            "Rendering directory listing: path='{}', entries={}, upload_enabled={}",
            path, entry_count, upload_enabled
        );
        trace!("Directory listing current path: {}", current_path);
        let mut variables = HashMap::new();
        variables.insert("PATH".to_string(), path.to_string());
        variables.insert("ENTRY_COUNT".to_string(), entry_count.to_string());
        variables.insert("UPLOAD_ENABLED".to_string(), upload_enabled.to_string());
        variables.insert("CURRENT_PATH".to_string(), current_path.to_string());

        // Build a clean query suffix for the upload link (omit for root)
        let clean = current_path.trim_start_matches('/').trim_end_matches('/');
        let query_suffix = if clean.is_empty() {
            String::new()
        } else {
            // Percent-encode minimal set for URLs
            let encoded = percent_encode(clean);
            format!("?upload_to={encoded}")
        };
        variables.insert("QUERY_UPLOAD_SUFFIX".to_string(), query_suffix);

        // Generate entries HTML
        let mut entries_html = String::new();

        // Add parent directory link if not at root (as table row)
        if path != "/" && !path.is_empty() {
            entries_html.push_str(&format!(
                r#"<tr>
                    <td>
                        <a href="../" class="file-link">
                            <span class="file-type directory">{BACK_ICON_SVG}</span>
                            <span class="name">Back</span>
                        </a>
                    </td>
                    <td class="size" colspan="2"></td>
                </tr>"#
            ));
        }

        // Add file/directory entries with template-based icons
        for (name, size, date) in entries {
            let is_directory = name.ends_with('/');
            let type_class = if is_directory { "directory" } else { "file" };
            let display_name = if is_directory {
                name.trim_end_matches('/')
            } else {
                name
            };

            let icon_svg = if is_directory {
                FOLDER_ICON_SVG
            } else {
                Self::get_file_icon(name)
            };

            // Build absolute href using CURRENT_PATH to avoid base-URL ambiguity
            let base_clean = current_path.trim_end_matches('/');
            let href = if base_clean.is_empty() {
                format!("/{}", percent_encode(name))
            } else {
                format!(
                    "/{}/{}",
                    base_clean.trim_start_matches('/'),
                    percent_encode(name)
                )
            };

            entries_html.push_str(&format!(
                r#"<tr>
                    <td>
                        <a href="{}" class="file-link">
                            <span class="file-type {}">{}</span>
                            <span class="name">{}</span>
                        </a>
                    </td>
                    <td class="size">{}</td>
                    <td class="date">{}</td>
                </tr>"#,
                href,
                type_class,
                icon_svg,
                html_escape(display_name),
                size,
                date
            ));
        }

        variables.insert("ENTRIES".to_string(), entries_html);

        // Use the new base template system
        self.render_directory_page(&variables)
    }
    /// Generate error page HTML using base template system
    pub fn render_error_page(
        &self,
        status_code: u16,
        status_text: &str,
        description: &str,
    ) -> Result<String, AppError> {
        debug!(
            "Rendering error page: {} {} - {}",
            status_code, status_text, description
        );
        // Use the new base template system
        self.render_error_page_new(status_code, status_text, description)
    }

    /// Generate upload page HTML using base template system
    pub fn render_upload_page(&self, path: &str) -> Result<String, AppError> {
        debug!("Rendering upload page for path: {}", path);
        // Use the new base template system
        self.render_upload_page_new(path)
    }

    /// Render monitor page using the base template system
    pub fn render_monitor_page(&self) -> Result<String, AppError> {
        debug!("Rendering monitor page");
        let page_title = "Monitor";
        let page_styles = r#"<link rel="stylesheet" href="/_irondrop/static/monitor/styles.css">"#;
        let page_scripts = r#"
            <script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>
            <script src="/_irondrop/static/monitor/script.js"></script>
        "#;
        let header_actions = r#"<a href="/" class="btn btn-light">← Back to Files</a>"#;

        let variables = HashMap::new();

        self.render_page(
            "monitor_content",
            page_title,
            page_styles,
            page_scripts,
            header_actions,
            &variables,
        )
    }

    /// Get upload form component HTML
    pub fn get_upload_form(&self) -> Result<String, AppError> {
        self.render("upload_form", &HashMap::new())
    }

    /// Render upload success page
    pub fn render_upload_success(
        &self,
        file_count: usize,
        total_size: &str,
        processing_time: u64,
        files_list: &str,
        warnings: &str,
    ) -> Result<String, AppError> {
        let page_title = "Upload Successful";
        let page_styles = r#"<link rel="stylesheet" href="/_irondrop/static/upload/styles.css">"#;
        let page_scripts = "";
        let header_actions = r#"<a href="/" class="btn btn-light">← Back to Files</a>"#;

        let mut variables = HashMap::new();
        variables.insert("FILE_COUNT".to_string(), file_count.to_string());
        variables.insert("TOTAL_SIZE".to_string(), total_size.to_string());
        variables.insert("PROCESSING_TIME".to_string(), processing_time.to_string());
        variables.insert("FILES_LIST".to_string(), files_list.to_string());
        variables.insert("WARNINGS".to_string(), warnings.to_string());

        self.render_page(
            "upload_success",
            page_title,
            page_styles,
            page_scripts,
            header_actions,
            &variables,
        )
    }
}

/// Simple percent encoding for URLs
fn percent_encode(input: &str) -> String {
    input
        .chars()
        .map(|c| match c {
            ' ' => "%20".to_string(),
            '"' => "%22".to_string(),
            '#' => "%23".to_string(),
            '%' => "%25".to_string(),
            '<' => "%3C".to_string(),
            '>' => "%3E".to_string(),
            '?' => "%3F".to_string(),
            _ => c.to_string(),
        })
        .collect()
}

/// Simple HTML entity escaping
fn html_escape(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;")
}

/// Get human-friendly error descriptions
pub fn get_error_description(status_code: u16) -> &'static str {
    match status_code {
        400 => "The request could not be understood due to malformed syntax.",
        401 => "Authentication is required to access this resource.",
        403 => "Access to this resource is forbidden.",
        404 => "The requested file or directory could not be found.",
        405 => "The request method is not allowed for this resource.",
        500 => "An internal server error occurred while processing your request.",
        _ => "An unexpected error occurred while processing your request.",
    }
}