-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
128 lines (109 loc) · 4.21 KB
/
Program.cs
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
using Avalonia;
using DefaultBrowser.Models;
using Serilog;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace DefaultBrowser;
sealed class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args)
{
// Set up Serilog early on for command-line mode
var platformService = PlatformService.Instance;
// Create log directory if it doesn't exist
var logDirectory = Path.GetDirectoryName(platformService.GetLogFilePath());
if (!string.IsNullOrEmpty(logDirectory) && !Directory.Exists(logDirectory))
{
Directory.CreateDirectory(logDirectory);
}
// Configure Serilog with console logging and file logging
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug() // Set to Debug to capture more information
.WriteTo.File(platformService.GetLogFilePath(), rollingInterval: RollingInterval.Day)
.WriteTo.Console()
.CreateLogger();
// Log basic system information
Log.Information("System: {OS} / {Framework}",
Environment.OSVersion.ToString(),
Environment.Version.ToString());
Log.Information("Application started with {ArgCount} arguments", args.Length);
// Check if we have arguments (URL to open)
if (args.Length > 0)
{
// We're being called to open a URL
ProcessUrl(args).Wait();
}
else
{
// No arguments, open the settings UI
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
}
private static async Task ProcessUrl(string[] args)
{
try
{
Log.Information("Processing URL in command-line mode");
// Extract URL from arguments
string url = ExtractUrl(args);
if (string.IsNullOrEmpty(url))
{
Log.Warning("No valid URL found in arguments");
return;
}
Log.Information("URL to process: {Url}", url);
// Load settings
var platformService = PlatformService.Instance;
var settings = await AppSettings.LoadAsync(platformService.GetSettingsFilePath());
// Create URL redirector
var redirector = new UrlRedirector(settings);
// Process the URL
bool success = redirector.ProcessUrl(url);
if (success)
{
Log.Information("URL processed successfully");
}
else
{
Log.Warning("Failed to process URL");
}
}
catch (Exception ex)
{
Log.Error(ex, "Error processing URL");
}
finally
{
// Ensure logs are flushed
Log.CloseAndFlush();
}
}
private static string ExtractUrl(string[] args)
{
// Common URL protocols
string[] protocols = new[] { "http://", "https://", "ftp://", "file://", "mailto:" };
// First, try to find a complete URL in the arguments
foreach (var arg in args)
{
if (protocols.Any(p => arg.StartsWith(p, StringComparison.OrdinalIgnoreCase)))
{
return arg;
}
}
// If we couldn't find a fully qualified URL, just return the first argument
// as it might be a partial URL or a different format
return args.Length > 0 ? args[0] : string.Empty;
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}