瀏覽代碼

Added appsettings based config and removed webhooks

Krzysztof Krysiński 1 周之前
父節點
當前提交
cc057e421b

+ 0 - 12
src/PixiEditor/BuildConstants.cs

@@ -1,12 +0,0 @@
-namespace PixiEditor;
-
-public static class BuildConstants
-{
-    public const string CrashReportWebhookUrl = "${crash-report-webhook-url}";
-
-    public const string AnalyticsUrl = "${analytics-url}";
-
-    public const string PixiEditorApiUrl = "${api-url}";
-
-    public const string? PixiEditorApiKey = "${api-key}";
-}

+ 0 - 25
src/PixiEditor/Helpers/CrashHelper.cs

@@ -156,34 +156,9 @@ internal partial class CrashHelper
 
         var report = CrashReport.Generate(e, new NonCrashInfo(filePath, memberName));
         
-        await SendReportTextToWebhookAsync(report, $"{filePath}; Method {memberName}");
         await SendReportToAnalyticsApiAsync(report);
     }
 
-    public static async Task SendReportTextToWebhookAsync(CrashReport report, string catchLocation = null)
-    {
-        string reportText = report.ReportText;
-        if (catchLocation is not null)
-        {
-            reportText = $"The report was generated from an exception caught in {Path.GetFileName(catchLocation)}.\r\n{reportText}";
-        }
-
-        byte[] bytes = Encoding.UTF8.GetBytes(reportText);
-        string filename = Path.GetFileNameWithoutExtension(report.FilePath) + ".txt";
-
-        MultipartFormDataContent formData = new MultipartFormDataContent
-        {
-            { new ByteArrayContent(bytes, 0, bytes.Length), "crash-report", filename }
-        };
-        try
-        {
-            using HttpClient httpClient = new HttpClient();
-            string url = BuildConstants.CrashReportWebhookUrl;
-            await httpClient.PostAsync(url, formData);
-        }
-        catch { }
-    }
-
     public static async Task SendReportToAnalyticsApiAsync(CrashReport report)
     {
         if (AnalyticsClient.GetAnalyticsUrl() is not { } analyticsUrl)

+ 4 - 4
src/PixiEditor/Initialization/ClassicDesktopEntry.cs

@@ -278,9 +278,9 @@ internal class ClassicDesktopEntry
 
     private string GetApiUrl()
     {
-        string baseUrl = BuildConstants.PixiEditorApiUrl;
+        string? baseUrl = RuntimeConstants.PixiEditorApiUrl;
 #if DEBUG
-        if (baseUrl.Contains('{') && baseUrl.Contains('}'))
+        if (baseUrl != null && baseUrl.Contains('{') && baseUrl.Contains('}'))
         {
             string? envUrl = Environment.GetEnvironmentVariable("PIXIEDITOR_API_URL");
             if (envUrl != null)
@@ -290,12 +290,12 @@ internal class ClassicDesktopEntry
         }
 #endif
 
-        return baseUrl;
+        return baseUrl ?? "";
     }
 
     private string GetApiKey()
     {
-        string? apiKey = BuildConstants.PixiEditorApiKey;
+        string? apiKey = RuntimeConstants.PixiEditorApiKey;
 #if DEBUG
         if (apiKey != null && apiKey.Contains('{') && apiKey.Contains('}'))
         {

+ 1 - 1
src/PixiEditor/Models/AnalyticsAPI/AnalyticsClient.cs

@@ -98,7 +98,7 @@ public class AnalyticsClient : IDisposable
 
     public static string? GetAnalyticsUrl()
     {
-        string url = BuildConstants.AnalyticsUrl;
+        string url = RuntimeConstants.AnalyticsUrl;
 
         if (url == "${analytics-url}")
         {

+ 6 - 0
src/PixiEditor/PixiEditor.csproj

@@ -150,5 +150,11 @@
   <ItemGroup>
     <Folder Include="Extensions\"/>
   </ItemGroup>
+
+  <ItemGroup>
+    <None Include="appsettings.json">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+  </ItemGroup>
   
 </Project>

+ 24 - 0
src/PixiEditor/RuntimeConstants.cs

@@ -0,0 +1,24 @@
+namespace PixiEditor;
+
+public static class RuntimeConstants
+{
+    private static Dictionary<string, string> appSettings = File.Exists("appsettings.json")
+        ? System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(ReadAppSettings())
+        : new Dictionary<string, string>();
+
+    private static string ReadAppSettings()
+    {
+        using StreamReader reader = new StreamReader("appsettings.json");
+        return reader.ReadToEnd();
+    }
+
+
+    public static string? AnalyticsUrl =>
+        appSettings.TryGetValue("AnalyticsUrl", out string? url) ? url : null;
+
+    public static string? PixiEditorApiUrl =
+        appSettings.TryGetValue("PixiEditorApiUrl", out string? apiUrl) ? apiUrl : null;
+
+    public static string? PixiEditorApiKey =
+        appSettings.TryGetValue("PixiEditorApiKey", out string? apiKey) ? apiKey : null;
+}

+ 0 - 2
src/PixiEditor/ViewModels/CrashReportViewModel.cs

@@ -38,8 +38,6 @@ internal partial class CrashReportViewModel : Window
         DocumentCount = report.GetDocumentCount();
         OpenSendCrashReportCommand = new RelayCommand(() => new SendCrashReportDialog(CrashReport).Show());
 
-        if (!IsDebugBuild)
-            _ = CrashHelper.SendReportTextToWebhookAsync(report);
         _ = CrashHelper.SendReportToAnalyticsApiAsync(report);
     }
 

+ 1 - 0
src/PixiEditor/appsettings.json

@@ -0,0 +1 @@
+{}