|
10 | 10 | using Serilog.Debugging; |
11 | 11 | using Serilog.Events; |
12 | 12 | using Syncfusion.Blazor; |
| 13 | +using System.Diagnostics; |
13 | 14 | using System.Net; |
14 | 15 | using System.Net.Http; |
| 16 | +using System.Runtime.InteropServices; |
15 | 17 | using TelegramDownloader.Data; |
16 | 18 | using TelegramDownloader.Data.db; |
17 | 19 | using TelegramDownloader.Helpers; |
|
206 | 208 |
|
207 | 209 | try |
208 | 210 | { |
209 | | - Log.Information("TelegramFileManager application started"); |
210 | | - app.Run(); |
| 211 | + // Check if running in Docker |
| 212 | + var isDocker = Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true" |
| 213 | + || File.Exists("/.dockerenv"); |
| 214 | + |
| 215 | + // Start the application |
| 216 | + await app.StartAsync(); |
| 217 | + |
| 218 | + // Get the URLs the application is listening on |
| 219 | + var urls = app.Urls; |
| 220 | + var serverAddresses = app.Services.GetRequiredService<Microsoft.AspNetCore.Hosting.Server.IServer>() |
| 221 | + .Features.Get<Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature>()?.Addresses; |
| 222 | + |
| 223 | + var listeningUrls = serverAddresses?.ToList() ?? urls.ToList(); |
| 224 | + |
| 225 | + // Display startup banner with URLs |
| 226 | + Console.WriteLine(); |
| 227 | + Console.WriteLine("╔══════════════════════════════════════════════════════════════╗"); |
| 228 | + Console.WriteLine("║ TelegramFileManager - Application Started ║"); |
| 229 | + Console.WriteLine("╠══════════════════════════════════════════════════════════════╣"); |
| 230 | + foreach (var url in listeningUrls) |
| 231 | + { |
| 232 | + var paddedUrl = url.PadRight(46); |
| 233 | + Console.WriteLine($"║ 🌐 Listening on: {paddedUrl}║"); |
| 234 | + } |
| 235 | + Console.WriteLine("╠══════════════════════════════════════════════════════════════╣"); |
| 236 | + if (isDocker) |
| 237 | + { |
| 238 | + Console.WriteLine("║ 🐳 Running in Docker container ║"); |
| 239 | + } |
| 240 | + else |
| 241 | + { |
| 242 | + Console.WriteLine("║ 💻 Running on local machine ║"); |
| 243 | + } |
| 244 | + Console.WriteLine("╚══════════════════════════════════════════════════════════════╝"); |
| 245 | + Console.WriteLine(); |
| 246 | + |
| 247 | + Log.Information("TelegramFileManager application started. Listening on: {Urls}", string.Join(", ", listeningUrls)); |
| 248 | + |
| 249 | + // Open browser automatically if not in Docker |
| 250 | + if (!isDocker && listeningUrls.Any()) |
| 251 | + { |
| 252 | + var urlToOpen = listeningUrls.FirstOrDefault(u => u.StartsWith("http://")) ?? listeningUrls.First(); |
| 253 | + |
| 254 | + // Replace 0.0.0.0 or * with localhost for browser |
| 255 | + urlToOpen = urlToOpen.Replace("://0.0.0.0", "://localhost") |
| 256 | + .Replace("://[::]", "://localhost") |
| 257 | + .Replace("://*", "://localhost"); |
| 258 | + |
| 259 | + Log.Information("Opening browser at: {Url}", urlToOpen); |
| 260 | + Console.WriteLine($"🚀 Opening browser at: {urlToOpen}"); |
| 261 | + |
| 262 | + try |
| 263 | + { |
| 264 | + OpenBrowser(urlToOpen); |
| 265 | + } |
| 266 | + catch (Exception ex) |
| 267 | + { |
| 268 | + Log.Warning(ex, "Could not open browser automatically. Please navigate to {Url} manually.", urlToOpen); |
| 269 | + Console.WriteLine($"⚠️ Could not open browser automatically. Please navigate to {urlToOpen} manually."); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + // Wait for the application to stop |
| 274 | + await app.WaitForShutdownAsync(); |
211 | 275 | } |
212 | 276 | catch (Exception ex) |
213 | 277 | { |
|
218 | 282 | Log.Information("TelegramFileManager application shutting down"); |
219 | 283 | Log.CloseAndFlush(); |
220 | 284 | } |
| 285 | + |
| 286 | +// Helper method to open browser cross-platform |
| 287 | +static void OpenBrowser(string url) |
| 288 | +{ |
| 289 | + try |
| 290 | + { |
| 291 | + Process.Start(url); |
| 292 | + } |
| 293 | + catch |
| 294 | + { |
| 295 | + // Windows |
| 296 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 297 | + { |
| 298 | + url = url.Replace("&", "^&"); |
| 299 | + Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true }); |
| 300 | + } |
| 301 | + // Linux |
| 302 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 303 | + { |
| 304 | + Process.Start("xdg-open", url); |
| 305 | + } |
| 306 | + // macOS |
| 307 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) |
| 308 | + { |
| 309 | + Process.Start("open", url); |
| 310 | + } |
| 311 | + else |
| 312 | + { |
| 313 | + throw; |
| 314 | + } |
| 315 | + } |
| 316 | +} |
0 commit comments