Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 36 additions & 28 deletions Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,66 @@
using Flow.Launcher.Infrastructure.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using FastCache;

namespace Flow.Launcher.Core.ExternalPlugins
{
public static class PluginsManifest
{
private const string manifestFileUrl = "https://cdn.jsdelivr.net/gh/Flow-Launcher/Flow.Launcher.PluginsManifest@plugin_api_v2/plugins.json";
private const string ManifestFileUrl =
"https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/plugin_api_v2/plugins.json";

private static readonly SemaphoreSlim manifestUpdateLock = new(1);
private static string _latestEtag = "";

private static string latestEtag = "";

public static List<UserPlugin> UserPlugins { get; private set; } = new List<UserPlugin>();

public static async Task UpdateManifestAsync(CancellationToken token = default)
public static async ValueTask<List<UserPlugin>> RetrieveManifestAsync()
{
try
if (Cached<List<UserPlugin>>.TryGet(0, out var cached))
{
await manifestUpdateLock.WaitAsync(token).ConfigureAwait(false);

var request = new HttpRequestMessage(HttpMethod.Get, manifestFileUrl);
request.Headers.Add("If-None-Match", latestEtag);
return cached;
}

using var response = await Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token).ConfigureAwait(false);
var result = await UpdateManifestAsync();

if (response.StatusCode == HttpStatusCode.OK)
{
Log.Info($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Fetched plugins from manifest repo");
return result;
}

await using var json = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false);
public static async Task<List<UserPlugin>> UpdateManifestAsync(CancellationToken token = default)
{
var request = new HttpRequestMessage(HttpMethod.Get, ManifestFileUrl);
request.Headers.Add("If-None-Match", _latestEtag);

UserPlugins = await JsonSerializer.DeserializeAsync<List<UserPlugin>>(json, cancellationToken: token).ConfigureAwait(false);
using var response = await Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token)
.ConfigureAwait(false);

latestEtag = response.Headers.ETag.Tag;
}
else if (response.StatusCode != HttpStatusCode.NotModified)
{
Log.Warn($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Http response for manifest file was {response.StatusCode}");
}
}
catch (Exception e)
if (response.StatusCode == HttpStatusCode.OK)
{
Log.Exception($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Http request failed", e);
Log.Info($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Fetched plugins from manifest repo");

await using var json = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false);

var plugins = await JsonSerializer
.DeserializeAsync<List<UserPlugin>>(json, cancellationToken: token).ConfigureAwait(false);

_latestEtag = response.Headers.ETag!.Tag;
if (plugins.Any())
Cached<List<UserPlugin>>.Save(0, plugins, TimeSpan.FromHours(6));
return plugins;
}
finally

if (response.StatusCode != HttpStatusCode.NotModified)
{
manifestUpdateLock.Release();
Log.Warn(
$"|PluginsManifest.{nameof(UpdateManifestAsync)}|Http response for manifest file was {response.StatusCode}");
}

return new List<UserPlugin>();
}
}
}
1 change: 1 addition & 0 deletions Flow.Launcher.Core/Flow.Launcher.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

<ItemGroup>
<PackageReference Include="Droplex" Version="1.6.0" />
<PackageReference Include="FastCache.Cached" Version="1.8.0" />
<PackageReference Include="FSharp.Core" Version="7.0.300" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
<PackageReference Include="squirrel.windows" Version="1.5.2" NoWarn="NU1701" />
Expand Down
Loading