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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
// SPDX-License-Identifier: MIT

//! Direct file upload handler for IronDrop
//!
//! This module provides a simplified, efficient direct upload system that:
//! - Processes raw HTTP body data without multipart parsing
//! - Uses a 2MB threshold for memory vs disk streaming
//! - Provides comprehensive security validations
//! - Supports filename extraction from URL path or headers
//! - Implements atomic file operations with temporary files
//! - Includes progress tracking capabilities
//!
//! # Design Philosophy
//!
//! This implementation removes all multipart parsing complexity and focuses on:
//! - Direct binary data streaming
//! - Memory efficiency for large files
//! - Simple, robust error handling
//! - Security-first approach
//!
//! # Example Usage
//!
//! The direct upload handler processes raw binary uploads without multipart parsing,
//! providing constant memory usage regardless of file size.

use crate::cli::Cli;
use crate::error::AppError;
use crate::http::{Request, RequestBody};
use crate::response::{HttpResponse, get_mime_type};
use crate::templates::TemplateEngine;
use glob::Pattern;
use log::{debug, error, info, trace, warn};
use std::collections::HashMap;
use std::env;
use std::fs::{self, File, OpenOptions};
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use std::time::SystemTime;

/// Memory threshold: files <= 2MB processed in memory, >2MB streamed to disk
const MEMORY_THRESHOLD: u64 = 2 * 1024 * 1024; // 2MB

/// Temporary file prefix for atomic operations
const TEMP_FILE_PREFIX: &str = ".irondrop_temp_";

/// Buffer size for streaming operations
const STREAM_BUFFER_SIZE: usize = 64 * 1024; // 64KB

/// Progress tracking information for uploads
#[derive(Debug, Clone)]
pub struct UploadProgress {
    /// Total expected size in bytes
    pub total_size: u64,
    /// Bytes processed so far
    pub processed_size: u64,
    /// Current processing stage
    pub stage: UploadStage,
}

/// Different stages of upload processing
#[derive(Debug, Clone, PartialEq)]
pub enum UploadStage {
    /// Receiving upload data
    Receiving,
    /// Validating file
    Validating,
    /// Writing file to disk
    Writing,
    /// Finalizing upload
    Finalizing,
    /// Upload completed
    Completed,
}

/// Information about a successfully uploaded file
#[derive(Debug, Clone)]
pub struct UploadedFile {
    /// Original filename (from URL path or header)
    pub original_name: String,
    /// Final filename on disk (may be different due to conflicts)
    pub saved_name: String,
    /// Full path where file was saved
    pub saved_path: PathBuf,
    /// File size in bytes
    pub size: u64,
    /// MIME type detected
    pub mime_type: String,
    /// Whether filename was modified to resolve conflicts
    pub renamed: bool,
}

/// Upload operation result
#[derive(Debug)]
pub struct UploadResult {
    /// Successfully uploaded file
    pub uploaded_file: UploadedFile,
    /// Upload processing time in milliseconds
    pub processing_time_ms: u64,
    /// Any warnings during processing
    pub warnings: Vec<String>,
}

/// Direct upload handler with security and configuration
pub struct DirectUploadHandler {
    /// Target directory for uploads
    target_dir: PathBuf,
    /// Maximum upload size in bytes
    max_upload_size: u64,
    /// Allowed file extensions (glob patterns)
    allowed_extensions: Vec<Pattern>,
    /// Whether upload functionality is enabled
    upload_enabled: bool,
}

impl DirectUploadHandler {
    /// Create a new direct upload handler from CLI configuration
    pub fn new(cli: &Cli) -> Result<Self, AppError> {
        if !cli.enable_upload.unwrap_or(false) {
            return Err(AppError::upload_disabled());
        }

        // Always use the directory being served as the base for uploads
        Self::new_with_directory(cli, cli.directory.clone())
    }

    /// Create upload handler with custom target directory
    pub fn new_with_directory(cli: &Cli, target_dir: PathBuf) -> Result<Self, AppError> {
        if !cli.enable_upload.unwrap_or(false) {
            return Err(AppError::upload_disabled());
        }

        // Ensure target directory exists
        Self::ensure_directory_exists(&target_dir)?;

        // Parse allowed extensions from CLI
        let allowed_extensions = cli
            .allowed_extensions
            .as_deref()
            .unwrap_or("*")
            .split(',')
            .map(|ext| ext.trim())
            .filter(|ext| !ext.is_empty())
            .map(Pattern::new)
            .collect::<Result<Vec<Pattern>, _>>()
            .map_err(AppError::from)?;

        let max_upload_bytes = cli.max_upload_size_bytes();

        Ok(Self {
            target_dir,
            max_upload_size: max_upload_bytes,
            allowed_extensions,
            upload_enabled: true,
        })
    }

    /// Detect the OS-specific download directory
    pub fn detect_os_download_directory() -> Result<PathBuf, AppError> {
        let download_dir = if cfg!(target_os = "windows") {
            // Windows: %USERPROFILE%\Downloads
            env::var("USERPROFILE")
                .map(|profile| PathBuf::from(profile).join("Downloads"))
                .unwrap_or_else(|_| PathBuf::from("Downloads"))
        } else if cfg!(target_os = "macos") {
            // macOS: ~/Downloads
            env::var("HOME")
                .map(|home| PathBuf::from(home).join("Downloads"))
                .unwrap_or_else(|_| PathBuf::from("Downloads"))
        } else {
            // Linux and other Unix-like: Check XDG_DOWNLOAD_DIR, fallback to ~/Downloads
            if let Ok(xdg_download) = env::var("XDG_DOWNLOAD_DIR") {
                PathBuf::from(xdg_download)
            } else if let Ok(home) = env::var("HOME") {
                PathBuf::from(home).join("Downloads")
            } else {
                PathBuf::from("Downloads")
            }
        };

        // If the standard download directory doesn't exist, fallback to current working directory
        if !download_dir.exists() {
            warn!(
                "Standard download directory {download_dir:?} does not exist, falling back to current directory"
            );
            env::current_dir().map_err(AppError::from)
        } else {
            Ok(download_dir)
        }
    }

    /// Ensure the target directory exists, create if necessary
    fn ensure_directory_exists(dir: &Path) -> Result<(), AppError> {
        if !dir.exists() {
            info!("Creating upload directory: {dir:?}");
            fs::create_dir_all(dir).map_err(|e| {
                error!("Failed to create upload directory {dir:?}: {e}");
                AppError::from(e)
            })?;
        } else if !dir.is_dir() {
            return Err(AppError::InternalServerError(format!(
                "Upload path {dir:?} exists but is not a directory"
            )));
        }

        // Check if directory is writable
        let test_file = dir.join(".write_test");
        match File::create(&test_file) {
            Ok(_) => {
                let _ = fs::remove_file(&test_file); // Ignore errors on cleanup
                Ok(())
            }
            Err(e) => {
                error!("Upload directory {dir:?} is not writable: {e}");
                Err(AppError::from(e))
            }
        }
    }

    /// Handle a direct file upload request with statistics tracking
    pub fn handle_upload_with_stats(
        &mut self,
        request: &Request,
        stats: Option<&crate::server::ServerStats>,
    ) -> Result<HttpResponse, AppError> {
        debug!(
            "Starting upload handling to directory: {}",
            self.target_dir.display()
        );
        trace!(
            "Upload request method: {}, path: {}",
            request.method, request.path
        );

        let result = self.handle_upload(request, stats);

        // If there was an error, record failure statistics
        if result.is_err() {
            if let Some(stats) = stats {
                stats.record_upload_request(false, 0, 0, 0, 0); // Record failure
                stats.finish_upload();
            }
            debug!("Upload error type: {:?}", result.as_ref().err());
        } else {
            trace!("Upload processing completed without errors");
        }

        result
    }

    /// Handle a direct file upload request
    pub fn handle_upload(
        &mut self,
        request: &Request,
        stats: Option<&crate::server::ServerStats>,
    ) -> Result<HttpResponse, AppError> {
        debug!(
            "Starting upload processing for request: {} {}",
            request.method, request.path
        );
        trace!(
            "Upload handler config - max_size: {} bytes, enabled: {}",
            self.max_upload_size, self.upload_enabled
        );

        if !self.upload_enabled {
            warn!("Upload attempt rejected - uploads are disabled");
            return Err(AppError::upload_disabled());
        }
        debug!("Upload enabled check passed");

        let start_time = std::time::Instant::now();

        // Track upload start
        if let Some(stats) = stats {
            stats.start_upload();
        }

        // Validate request method
        if request.method != "POST" && request.method != "PUT" {
            debug!(
                "Invalid method for upload: {}, expected POST or PUT",
                request.method
            );
            return Err(AppError::MethodNotAllowed);
        }

        trace!("Request method validation passed");

        // Get request body
        let body = request.body.as_ref().ok_or_else(|| {
            debug!("Missing request body in upload request");
            AppError::BadRequest
        })?;

        debug!(
            "Request body found: {} bytes",
            match body {
                RequestBody::Memory(data) => data.len(),
                RequestBody::File { size, .. } => *size as usize,
            }
        );
        trace!(
            "Body type: {}",
            match body {
                RequestBody::Memory(_) => "memory",
                RequestBody::File { .. } => "file",
            }
        );
        trace!("Request body validation passed");

        // Check total upload size
        let body_size = match body {
            RequestBody::Memory(data) => data.len() as u64,
            RequestBody::File { size, .. } => *size,
        };

        debug!(
            "Upload body size: {} bytes (limit: {} bytes)",
            body_size, self.max_upload_size
        );

        if body_size > self.max_upload_size {
            warn!(
                "Upload rejected - size {} exceeds limit of {} bytes",
                body_size, self.max_upload_size
            );
            return Err(AppError::payload_too_large(self.max_upload_size));
        }
        debug!(
            "Upload size check passed: {} bytes (limit: {})",
            body_size, self.max_upload_size
        );
        trace!("Upload size validation passed");

        // Extract filename from URL path or Content-Disposition header
        let filename = self.extract_filename(request)?;
        debug!("Extracted filename: '{}'", filename);

        // Validate filename
        debug!("Validating filename: '{}'", filename);
        self.validate_filename(&filename)?;
        trace!("Filename validation passed");

        // Validate file extension
        self.validate_file_extension(&filename)?;
        debug!("Filename validation passed");
        trace!("File extension validation passed");

        // Check available disk space
        debug!("Checking disk space for {} bytes", body_size);
        self.check_disk_space(body_size)?;
        debug!("Disk space check passed");

        // Process upload based on body type and size
        let uploaded_file = if body_size <= MEMORY_THRESHOLD {
            debug!(
                "Processing upload in memory (size: {} <= threshold: {})",
                body_size, MEMORY_THRESHOLD
            );
            self.handle_memory_upload(body, &filename)?
        } else {
            debug!(
                "Processing upload with streaming (size: {} > threshold: {})",
                body_size, MEMORY_THRESHOLD
            );
            self.handle_streaming_upload(body, &filename)?
        };

        let processing_time = start_time.elapsed().as_millis() as u64;

        debug!(
            "Upload result - renamed: {}, mime_type: {}, path: {}",
            uploaded_file.renamed,
            uploaded_file.mime_type,
            uploaded_file.saved_path.display()
        );

        let upload_result = UploadResult {
            uploaded_file,
            processing_time_ms: processing_time,
            warnings: Vec::new(),
        };

        // Record successful upload statistics
        if let Some(stats) = stats {
            stats.record_upload_request(
                true, // success
                1,    // file count
                upload_result.uploaded_file.size,
                processing_time,
                upload_result.uploaded_file.size, // largest file is the only file
            );
            stats.finish_upload();
        }

        // Generate appropriate response based on Accept header
        self.generate_upload_response(request, upload_result)
    }

    /// Extract filename from URL path or headers
    fn extract_filename(&self, request: &Request) -> Result<String, AppError> {
        // First, try to get filename from Content-Disposition header
        if let Some(content_disposition) = request.headers.get("content-disposition")
            && let Some(filename) = Self::parse_filename_from_disposition(content_disposition)
        {
            return Ok(filename);
        }

        // Next, try to get filename from custom X-Filename header
        if let Some(filename) = request.headers.get("x-filename")
            && !filename.trim().is_empty()
        {
            return Ok(filename.trim().to_string());
        }

        // Finally, extract from URL path (last segment after /, excluding query params)
        let path_without_query = request.path.split('?').next().unwrap_or(&request.path);
        let path_segments: Vec<&str> = path_without_query.split('/').collect();
        if let Some(last_segment) = path_segments.last()
            && !last_segment.is_empty()
            && last_segment.contains('.')
        {
            return Ok(last_segment.to_string());
        }

        // If no filename found anywhere, generate a default one
        let timestamp = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Ok(format!("upload_{}.bin", timestamp))
    }

    /// Parse filename from Content-Disposition header
    fn parse_filename_from_disposition(disposition: &str) -> Option<String> {
        for part in disposition.split(';') {
            let part = part.trim();
            if part.to_lowercase().starts_with("filename=") {
                let filename_part = &part[9..]; // Skip "filename="
                let filename = filename_part.trim_matches('"').trim();
                if !filename.is_empty() {
                    return Some(filename.to_string());
                }
            }
        }
        None
    }

    /// Handle uploads that fit in memory (≤2MB)
    fn handle_memory_upload(
        &mut self,
        body: &RequestBody,
        filename: &str,
    ) -> Result<UploadedFile, AppError> {
        debug!("Processing memory upload for file: {}", filename);

        let data = match body {
            RequestBody::Memory(data) => data,
            RequestBody::File { path, .. } => {
                // If body is in file but small enough for memory processing,
                // read it into memory for simpler handling
                return self.handle_file_based_upload(path, filename);
            }
        };

        // Generate unique filename to avoid conflicts
        let (final_filename, was_renamed) = self.generate_unique_filename(filename)?;
        debug!(
            "Generated filename: '{}' (renamed: {})",
            final_filename, was_renamed
        );
        let target_path = self.target_dir.join(&final_filename);
        trace!("Target path: {}", target_path.display());

        // Create temporary file for atomic write
        let temp_filename = format!(
            "{}{}_{}_{:x}.tmp",
            TEMP_FILE_PREFIX,
            std::process::id(),
            SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos(),
            data.len() // Use data length as part of unique identifier
        );
        let temp_path = self.target_dir.join(&temp_filename);

        // Write data to temporary file
        debug!(
            "Writing {} bytes to temporary file: {}",
            data.len(),
            temp_path.display()
        );
        {
            let mut temp_file = OpenOptions::new()
                .create_new(true)
                .write(true)
                .open(&temp_path)
                .map_err(|e| {
                    error!("Failed to create temporary file {temp_path:?}: {e}");
                    AppError::from(e)
                })?;

            temp_file.write_all(data).map_err(|e| {
                error!("Failed to write data to temporary file {temp_path:?}: {e}");
                let _ = fs::remove_file(&temp_path); // Cleanup on error
                AppError::from(e)
            })?;

            temp_file.sync_all().map_err(|e| {
                error!("Failed to sync temporary file {temp_path:?}: {e}");
                let _ = fs::remove_file(&temp_path); // Cleanup on error
                AppError::from(e)
            })?;
        }

        // Atomically rename temporary file to final location
        debug!("Atomically moving temporary file to final location");
        fs::rename(&temp_path, &target_path).map_err(|e| {
            error!("Failed to rename {temp_path:?} to {target_path:?}: {e}");
            let _ = fs::remove_file(&temp_path); // Cleanup on error
            AppError::from(e)
        })?;
        trace!("File successfully moved to: {}", target_path.display());

        // Determine MIME type
        let mime_type = get_mime_type(&target_path).to_string();
        trace!("Detected MIME type: {}", mime_type);

        info!(
            "Successfully uploaded file: {} ({} bytes) to {}",
            final_filename,
            data.len(),
            target_path.display()
        );

        Ok(UploadedFile {
            original_name: filename.to_string(),
            saved_name: final_filename,
            saved_path: target_path,
            size: data.len() as u64,
            mime_type,
            renamed: was_renamed,
        })
    }

    /// Handle uploads that are streamed to disk (>2MB)
    fn handle_streaming_upload(
        &mut self,
        body: &RequestBody,
        filename: &str,
    ) -> Result<UploadedFile, AppError> {
        debug!("Processing streaming upload for file: {}", filename);

        match body {
            RequestBody::Memory(_) => {
                // This shouldn't happen due to size checks, but handle gracefully
                return self.handle_memory_upload(body, filename);
            }
            RequestBody::File { path, size: _ } => self.handle_file_based_upload(path, filename),
        }
    }

    /// Handle uploads from file (used for both small files read from disk and large streaming files)
    fn handle_file_based_upload(
        &mut self,
        source_path: &PathBuf,
        filename: &str,
    ) -> Result<UploadedFile, AppError> {
        debug!(
            "Processing file-based upload: {} -> {}",
            source_path.display(),
            filename
        );

        // Generate unique filename to avoid conflicts
        let (final_filename, was_renamed) = self.generate_unique_filename(filename)?;
        debug!(
            "Generated filename: '{}' (renamed: {})",
            final_filename, was_renamed
        );
        let target_path = self.target_dir.join(&final_filename);
        trace!("Target path: {}", target_path.display());

        // Create temporary file for atomic operation
        let temp_filename = format!(
            "{}{}_{}_{:x}.tmp",
            TEMP_FILE_PREFIX,
            std::process::id(),
            SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos(),
            source_path.to_string_lossy().len() // Use path length as part of unique identifier
        );
        let temp_path = self.target_dir.join(&temp_filename);

        // Stream copy from source to temporary file
        debug!(
            "Starting streaming copy from {} to {}",
            source_path.display(),
            temp_path.display()
        );
        let file_size = {
            let source_file = File::open(source_path).map_err(|e| {
                error!("Failed to open source file {source_path:?}: {e}");
                AppError::from(e)
            })?;

            let temp_file = OpenOptions::new()
                .create_new(true)
                .write(true)
                .open(&temp_path)
                .map_err(|e| {
                    error!("Failed to create temporary file {temp_path:?}: {e}");
                    AppError::from(e)
                })?;

            // Use buffered streams for better performance
            let mut reader = BufReader::new(source_file);
            let mut writer = BufWriter::new(temp_file);

            let mut buffer = vec![0u8; STREAM_BUFFER_SIZE];
            let mut total_bytes = 0u64;
            trace!("Using buffer size: {} bytes", STREAM_BUFFER_SIZE);

            loop {
                let bytes_read = reader.read(&mut buffer).map_err(|e| {
                    error!("Failed to read from source file {source_path:?}: {e}");
                    let _ = fs::remove_file(&temp_path); // Cleanup on error
                    AppError::from(e)
                })?;

                if bytes_read == 0 {
                    break; // EOF
                }

                writer.write_all(&buffer[..bytes_read]).map_err(|e| {
                    error!("Failed to write to temporary file {temp_path:?}: {e}");
                    let _ = fs::remove_file(&temp_path); // Cleanup on error
                    AppError::from(e)
                })?;

                total_bytes += bytes_read as u64;

                // Log progress for large files
                if total_bytes.is_multiple_of(1024 * 1024) || total_bytes < 1024 * 1024 {
                    trace!("Streamed {} bytes so far", total_bytes);
                }

                // Check size limit during streaming
                if total_bytes > self.max_upload_size {
                    warn!(
                        "Streaming upload exceeded size limit: {} > {}",
                        total_bytes, self.max_upload_size
                    );
                    let _ = fs::remove_file(&temp_path); // Cleanup
                    return Err(AppError::payload_too_large(self.max_upload_size));
                }
            }

            // Ensure all data is written
            writer.flush().map_err(|e| {
                error!("Failed to flush temporary file {temp_path:?}: {e}");
                let _ = fs::remove_file(&temp_path); // Cleanup on error
                AppError::from(e)
            })?;

            // Sync to disk
            writer
                .into_inner()
                .map_err(|e| {
                    error!("Failed to finalize temporary file {temp_path:?}: {e}");
                    let _ = fs::remove_file(&temp_path); // Cleanup on error
                    AppError::from(e.into_error())
                })?
                .sync_all()
                .map_err(|e| {
                    error!("Failed to sync temporary file {temp_path:?}: {e}");
                    let _ = fs::remove_file(&temp_path); // Cleanup on error
                    AppError::from(e)
                })?;

            total_bytes
        };

        // Atomically rename temporary file to final location
        fs::rename(&temp_path, &target_path).map_err(|e| {
            error!("Failed to rename {temp_path:?} to {target_path:?}: {e}");
            let _ = fs::remove_file(&temp_path); // Cleanup on error
            AppError::from(e)
        })?;

        // Determine MIME type
        let mime_type = get_mime_type(&target_path).to_string();

        info!(
            "Successfully uploaded file: {} ({} bytes) to {}",
            final_filename,
            file_size,
            target_path.display()
        );

        Ok(UploadedFile {
            original_name: filename.to_string(),
            saved_name: final_filename,
            saved_path: target_path,
            size: file_size,
            mime_type,
            renamed: was_renamed,
        })
    }

    /// Check available disk space
    fn check_disk_space(&self, required_bytes: u64) -> Result<(), AppError> {
        // Simple heuristic: Check if we can create a test file
        // In a production system, you might use platform-specific APIs to get actual disk space

        let test_size = std::cmp::min(required_bytes / 100, 1024 * 1024); // Test with 1% or max 1MB
        let test_path = self.target_dir.join(".space_test");

        match OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(&test_path)
        {
            Ok(mut file) => {
                let test_data = vec![0u8; test_size as usize];
                match file.write_all(&test_data) {
                    Ok(_) => {
                        let _ = fs::remove_file(&test_path); // Cleanup
                        Ok(())
                    }
                    Err(_) => {
                        let _ = fs::remove_file(&test_path); // Cleanup
                        Err(AppError::upload_disk_full(0)) // We don't have exact available space
                    }
                }
            }
            Err(_) => Err(AppError::upload_disk_full(0)),
        }
    }

    /// Validate filename for security
    fn validate_filename(&self, filename: &str) -> Result<(), AppError> {
        if filename.is_empty() {
            return Err(AppError::invalid_filename("Empty filename"));
        }

        if filename.len() > 255 {
            return Err(AppError::invalid_filename("Filename too long"));
        }

        // Check for path traversal attempts
        if filename.contains("..") || filename.contains('/') || filename.contains('\\') {
            return Err(AppError::invalid_filename(filename));
        }

        // Check for dangerous characters
        let dangerous_chars = ['<', '>', ':', '"', '|', '?', '*'];
        if filename
            .chars()
            .any(|c| dangerous_chars.contains(&c) || c.is_control())
        {
            return Err(AppError::invalid_filename(filename));
        }

        // Check for Windows reserved names (case-insensitive)
        let base_name = if let Some(dot_pos) = filename.rfind('.') {
            &filename[..dot_pos]
        } else {
            filename
        };

        let reserved_names = [
            "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",
            "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
        ];

        if reserved_names
            .iter()
            .any(|&reserved| base_name.eq_ignore_ascii_case(reserved))
        {
            return Err(AppError::invalid_filename(filename));
        }

        Ok(())
    }

    /// Validate file extension against allowed patterns
    fn validate_file_extension(&self, filename: &str) -> Result<(), AppError> {
        if self.allowed_extensions.is_empty() {
            return Ok(()); // No restrictions
        }

        let path = Path::new(filename);

        let matches = self
            .allowed_extensions
            .iter()
            .any(|pattern| pattern.matches_path(path));

        if !matches {
            let extension = path
                .extension()
                .and_then(|ext| ext.to_str())
                .unwrap_or("(no extension)");
            return Err(AppError::unsupported_media_type(format!(
                "File extension '{extension}' not allowed"
            )));
        }

        Ok(())
    }

    /// Generate a unique filename to avoid conflicts
    fn generate_unique_filename(&self, original: &str) -> Result<(String, bool), AppError> {
        // Try the original filename first by checking if it exists
        let target_path = self.target_dir.join(original);

        // Check if file exists without creating it
        if !target_path.exists() {
            return Ok((original.to_string(), false));
        }

        // File exists, generate a unique name
        let path = Path::new(original);
        let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("file");
        let extension = path
            .extension()
            .and_then(|ext| ext.to_str())
            .map(|ext| format!(".{ext}"))
            .unwrap_or_default();

        for i in 1..=9999 {
            let new_filename = format!("{stem}_{i}{extension}");
            let new_path = self.target_dir.join(&new_filename);

            // Check if this filename is available
            if !new_path.exists() {
                return Ok((new_filename, true));
            }
        }

        Err(AppError::InternalServerError(
            "Unable to generate unique filename after 9999 attempts".to_string(),
        ))
    }

    /// Generate appropriate response based on request Accept header
    fn generate_upload_response(
        &self,
        request: &Request,
        result: UploadResult,
    ) -> Result<HttpResponse, AppError> {
        let accept_header = request
            .headers
            .get("accept")
            .map(|s| s.as_str())
            .unwrap_or("");

        // Determine if client wants JSON response
        let wants_json = accept_header.contains("application/json")
            || request.headers.contains_key("x-requested-with");

        if wants_json {
            self.generate_json_response(result)
        } else {
            self.generate_html_response(result)
        }
    }

    /// Generate JSON response for API clients
    fn generate_json_response(&self, result: UploadResult) -> Result<HttpResponse, AppError> {
        let file = &result.uploaded_file;

        let response_body = format!(
            r#"{{
    "status": "success",
    "message": "Upload completed successfully",
    "file": {{
        "name": "{}",
        "originalName": "{}",
        "size": {},
        "mimeType": "{}",
        "renamed": {}
    }},
    "statistics": {{
        "processingTimeMs": {}
    }},
    "warnings": []
}}"#,
            file.saved_name,
            file.original_name,
            file.size,
            file.mime_type,
            file.renamed,
            result.processing_time_ms
        );

        Ok(HttpResponse::new(200, "OK")
            .add_header(
                "Content-Type".to_string(),
                "application/json; charset=utf-8".to_string(),
            )
            .add_header("Cache-Control".to_string(), "no-cache".to_string())
            .with_html_body(response_body))
    }

    /// Generate HTML response for form submissions
    fn generate_html_response(&self, result: UploadResult) -> Result<HttpResponse, AppError> {
        let file = &result.uploaded_file;

        let rename_note = if file.renamed {
            format!(" <em>(renamed from {})</em>", file.original_name)
        } else {
            String::new()
        };

        let files_list = format!(
            r"<li><strong>{}</strong>{} - {} bytes</li>",
            file.saved_name,
            rename_note,
            format_bytes(file.size)
        );

        // Use the template engine
        let template_engine = TemplateEngine::global();
        let response_body = template_engine.render_upload_success(
            1, // one file uploaded
            &format_bytes(file.size),
            result.processing_time_ms,
            &files_list,
            "", // no warnings
        )?;

        Ok(HttpResponse::new(200, "OK").with_html_body(response_body))
    }

    /// Get upload handler configuration for debugging
    pub fn get_config_info(&self) -> HashMap<String, String> {
        let mut info = HashMap::new();
        info.insert(
            "target_directory".to_string(),
            self.target_dir.to_string_lossy().to_string(),
        );
        info.insert(
            "max_upload_size_mb".to_string(),
            (self.max_upload_size / 1024 / 1024).to_string(),
        );
        info.insert(
            "upload_enabled".to_string(),
            self.upload_enabled.to_string(),
        );
        info.insert(
            "allowed_extensions".to_string(),
            self.allowed_extensions
                .iter()
                .map(|p| p.as_str())
                .collect::<Vec<_>>()
                .join(", "),
        );
        info.insert(
            "memory_threshold_mb".to_string(),
            (MEMORY_THRESHOLD / 1024 / 1024).to_string(),
        );
        info
    }
}

/// Format bytes into human-readable format
fn format_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
    let mut size = bytes as f64;
    let mut unit_index = 0;

    while size >= 1024.0 && unit_index < UNITS.len() - 1 {
        size /= 1024.0;
        unit_index += 1;
    }

    if unit_index == 0 {
        format!("{} {}", bytes, UNITS[unit_index])
    } else {
        format!("{:.1} {}", size, UNITS[unit_index])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    fn create_test_cli(upload_dir: PathBuf) -> Cli {
        Cli {
            // Use the provided temp directory as the server base directory
            directory: upload_dir,
            listen: Some("127.0.0.1".to_string()),
            port: Some(8080),
            allowed_extensions: Some("*.txt,*.pdf".to_string()),
            threads: Some(4),
            chunk_size: Some(1024),
            verbose: Some(false),
            detailed_logging: Some(false),
            username: None,
            password: None,
            enable_upload: Some(true),
            max_upload_size: Some(100), // 100MB for testing
            config_file: None,
            log_dir: None,
        }
    }

    #[test]
    fn test_upload_handler_creation() {
        let temp_dir = TempDir::new().unwrap();
        let cli = create_test_cli(temp_dir.path().to_path_buf());

        let handler = DirectUploadHandler::new(&cli);
        assert!(handler.is_ok());

        let handler = handler.unwrap();
        assert_eq!(handler.target_dir, temp_dir.path());
        assert_eq!(handler.max_upload_size, 100 * 1024 * 1024);
        assert!(handler.upload_enabled);
    }

    #[test]
    fn test_upload_disabled() {
        let temp_dir = TempDir::new().unwrap();
        let mut cli = create_test_cli(temp_dir.path().to_path_buf());
        cli.enable_upload = Some(false);

        let result = DirectUploadHandler::new(&cli);
        assert!(matches!(result, Err(AppError::UploadDisabled)));
    }

    #[test]
    fn test_filename_validation() {
        let temp_dir = TempDir::new().unwrap();
        let cli = create_test_cli(temp_dir.path().to_path_buf());
        let handler = DirectUploadHandler::new(&cli).unwrap();

        // Valid filenames
        assert!(handler.validate_filename("document.txt").is_ok());
        assert!(
            handler
                .validate_filename("file_with_underscores.pdf")
                .is_ok()
        );
        assert!(handler.validate_filename("file-with-dashes.txt").is_ok());

        // Invalid filenames
        assert!(handler.validate_filename("../etc/passwd").is_err());
        assert!(handler.validate_filename("file/with/slashes.txt").is_err());
        assert!(
            handler
                .validate_filename("file\\with\\backslashes.txt")
                .is_err()
        );
        assert!(handler.validate_filename("file<with>brackets.txt").is_err());
        assert!(handler.validate_filename("").is_err());
    }

    #[test]
    fn test_unique_filename_generation() {
        let temp_dir = TempDir::new().unwrap();
        let cli = create_test_cli(temp_dir.path().to_path_buf());
        let handler = DirectUploadHandler::new(&cli).unwrap();

        // Create an existing file
        let existing_path = temp_dir.path().join("test.txt");
        let mut file = File::create(&existing_path).unwrap();
        file.write_all(b"test content").unwrap();

        // Test unique filename generation
        let (unique_name, renamed) = handler.generate_unique_filename("test.txt").unwrap();
        assert_eq!(unique_name, "test_1.txt");
        assert!(renamed);

        // Test when original doesn't exist
        let (original_name, renamed) = handler.generate_unique_filename("nonexistent.txt").unwrap();
        assert_eq!(original_name, "nonexistent.txt");
        assert!(!renamed);
    }

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(0), "0 B");
        assert_eq!(format_bytes(512), "512 B");
        assert_eq!(format_bytes(1024), "1.0 KB");
        assert_eq!(format_bytes(1536), "1.5 KB");
        assert_eq!(format_bytes(1_048_576), "1.0 MB");
        assert_eq!(format_bytes(1_073_741_824), "1.0 GB");
    }

    #[test]
    fn test_detect_download_directory() {
        let result = DirectUploadHandler::detect_os_download_directory();
        assert!(result.is_ok());

        let dir = result.unwrap();
        // The detected path should be an absolute path and a directory
        assert!(dir.is_absolute(), "Detected path should be absolute");
        assert!(dir.is_dir(), "Detected path should be a directory");
    }

    #[test]
    fn test_extension_validation() {
        let temp_dir = TempDir::new().unwrap();
        let cli = create_test_cli(temp_dir.path().to_path_buf());
        let handler = DirectUploadHandler::new(&cli).unwrap();

        // Allowed extensions (from CLI: *.txt,*.pdf)
        assert!(handler.validate_file_extension("document.txt").is_ok());
        assert!(handler.validate_file_extension("document.pdf").is_ok());

        // Not allowed extensions
        assert!(handler.validate_file_extension("document.exe").is_err());
        assert!(handler.validate_file_extension("document.jpg").is_err());
    }

    #[test]
    fn test_filename_extraction_from_disposition() {
        // Test various Content-Disposition formats
        assert_eq!(
            DirectUploadHandler::parse_filename_from_disposition("attachment; filename=test.txt"),
            Some("test.txt".to_string())
        );

        assert_eq!(
            DirectUploadHandler::parse_filename_from_disposition(
                "attachment; filename=\"quoted-file.pdf\""
            ),
            Some("quoted-file.pdf".to_string())
        );

        assert_eq!(
            DirectUploadHandler::parse_filename_from_disposition("inline"),
            None
        );

        assert_eq!(
            DirectUploadHandler::parse_filename_from_disposition("attachment; filename="),
            None
        );
    }
}