Skip to content

Commit 41e81af

Browse files
committed
feat: add url and port in terminal
1 parent 7a2475a commit 41e81af

2 files changed

Lines changed: 99 additions & 3 deletions

File tree

TelegramDownloader/Program.cs

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
using Serilog.Debugging;
1111
using Serilog.Events;
1212
using Syncfusion.Blazor;
13+
using System.Diagnostics;
1314
using System.Net;
1415
using System.Net.Http;
16+
using System.Runtime.InteropServices;
1517
using TelegramDownloader.Data;
1618
using TelegramDownloader.Data.db;
1719
using TelegramDownloader.Helpers;
@@ -206,8 +208,70 @@
206208

207209
try
208210
{
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();
211275
}
212276
catch (Exception ex)
213277
{
@@ -218,3 +282,35 @@
218282
Log.Information("TelegramFileManager application shutting down");
219283
Log.CloseAndFlush();
220284
}
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+
}

TelegramDownloader/TelegramDownloader.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<Version>3.0.1.0</Version>
4+
<Version>3.1.0.0</Version>
55
<Authors>Mateo</Authors>
66
<Product>TelegramFileManager</Product>
77
<TargetFramework>net10.0</TargetFramework>

0 commit comments

Comments
 (0)