1+ using Microsoft . AspNetCore . Authorization ;
2+ using Microsoft . AspNetCore . Mvc ;
3+ using HawkSyncShared . DTOs ;
4+
5+ namespace BHD_ServerManager . API . Controllers ;
6+
7+ [ ApiController ]
8+ [ Route ( "api/[controller]" ) ]
9+ [ Authorize ]
10+ public class FileSystemController : ControllerBase
11+ {
12+ /// <summary>
13+ /// Get list of available drives
14+ /// </summary>
15+ [ HttpGet ( "drives" ) ]
16+ public ActionResult < DirectoryListingResponse > GetDrives ( )
17+ {
18+ try
19+ {
20+ var drives = DriveInfo . GetDrives ( )
21+ . Where ( d => d . IsReady )
22+ . Select ( d => d . Name )
23+ . ToList ( ) ;
24+
25+ return Ok ( new DirectoryListingResponse
26+ {
27+ Success = true ,
28+ Message = "Drives retrieved successfully" ,
29+ Drives = drives ,
30+ CurrentPath = string . Empty ,
31+ Entries = new List < FileSystemEntry > ( )
32+ } ) ;
33+ }
34+ catch ( Exception ex )
35+ {
36+ return Ok ( new DirectoryListingResponse
37+ {
38+ Success = false ,
39+ Message = $ "Error retrieving drives: { ex . Message } "
40+ } ) ;
41+ }
42+ }
43+
44+ /// <summary>
45+ /// Get directory listing
46+ /// </summary>
47+ [ HttpPost ( "list" ) ]
48+ public ActionResult < DirectoryListingResponse > GetDirectoryListing ( [ FromBody ] DirectoryListingRequest request )
49+ {
50+ try
51+ {
52+ // Default to drives if no path specified
53+ if ( string . IsNullOrWhiteSpace ( request . Path ) )
54+ {
55+ return GetDrives ( ) ;
56+ }
57+
58+ var dirInfo = new DirectoryInfo ( request . Path ) ;
59+
60+ if ( ! dirInfo . Exists )
61+ {
62+ return Ok ( new DirectoryListingResponse
63+ {
64+ Success = false ,
65+ Message = "Directory does not exist"
66+ } ) ;
67+ }
68+
69+ var entries = new List < FileSystemEntry > ( ) ;
70+
71+ // Add subdirectories
72+ try
73+ {
74+ foreach ( var dir in dirInfo . GetDirectories ( ) )
75+ {
76+ try
77+ {
78+ entries . Add ( new FileSystemEntry
79+ {
80+ Name = dir . Name ,
81+ FullPath = dir . FullName ,
82+ IsDirectory = true ,
83+ Size = 0 ,
84+ LastModified = dir . LastWriteTime
85+ } ) ;
86+ }
87+ catch
88+ {
89+ // Skip directories we can't access
90+ }
91+ }
92+ }
93+ catch
94+ {
95+ // Continue even if we can't list all directories
96+ }
97+
98+ // Add files (with optional filter)
99+ try
100+ {
101+ var searchPattern = string . IsNullOrWhiteSpace ( request . FileFilter ) ? "*.*" : request . FileFilter ;
102+
103+ foreach ( var file in dirInfo . GetFiles ( searchPattern ) )
104+ {
105+ try
106+ {
107+ entries . Add ( new FileSystemEntry
108+ {
109+ Name = file . Name ,
110+ FullPath = file . FullName ,
111+ IsDirectory = false ,
112+ Size = file . Length ,
113+ LastModified = file . LastWriteTime
114+ } ) ;
115+ }
116+ catch
117+ {
118+ // Skip files we can't access
119+ }
120+ }
121+ }
122+ catch
123+ {
124+ // Continue even if we can't list all files
125+ }
126+
127+ // Get parent directory
128+ string ? parentPath = dirInfo . Parent ? . FullName ;
129+
130+ var drives = DriveInfo . GetDrives ( )
131+ . Where ( d => d . IsReady )
132+ . Select ( d => d . Name )
133+ . ToList ( ) ;
134+
135+ return Ok ( new DirectoryListingResponse
136+ {
137+ Success = true ,
138+ Message = "Directory listed successfully" ,
139+ CurrentPath = dirInfo . FullName ,
140+ ParentPath = parentPath ,
141+ Entries = entries . OrderBy ( e => ! e . IsDirectory ) . ThenBy ( e => e . Name ) . ToList ( ) ,
142+ Drives = drives
143+ } ) ;
144+ }
145+ catch ( UnauthorizedAccessException )
146+ {
147+ return Ok ( new DirectoryListingResponse
148+ {
149+ Success = false ,
150+ Message = "Access denied to this directory"
151+ } ) ;
152+ }
153+ catch ( Exception ex )
154+ {
155+ return Ok ( new DirectoryListingResponse
156+ {
157+ Success = false ,
158+ Message = $ "Error listing directory: { ex . Message } "
159+ } ) ;
160+ }
161+ }
162+
163+ /// <summary>
164+ /// Validate if a path exists
165+ /// </summary>
166+ [ HttpPost ( "validate-path" ) ]
167+ public ActionResult < CommandResult > ValidatePath ( [ FromBody ] DirectoryListingRequest request )
168+ {
169+ try
170+ {
171+ if ( string . IsNullOrWhiteSpace ( request . Path ) )
172+ {
173+ return Ok ( new CommandResult
174+ {
175+ Success = false ,
176+ Message = "Path cannot be empty"
177+ } ) ;
178+ }
179+
180+ bool exists = Directory . Exists ( request . Path ) ;
181+
182+ return Ok ( new CommandResult
183+ {
184+ Success = exists ,
185+ Message = exists ? "Path exists" : "Path does not exist"
186+ } ) ;
187+ }
188+ catch ( Exception ex )
189+ {
190+ return Ok ( new CommandResult
191+ {
192+ Success = false ,
193+ Message = $ "Error validating path: { ex . Message } "
194+ } ) ;
195+ }
196+ }
197+ }
0 commit comments