Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
fix: support latest lip protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Pd233 committed Jan 19, 2024
1 parent 3c00f5d commit be000b4
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/LipUI/InternalServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public static Color Invert(this in Color color)

internal static class InternalServices
{
public static BitmapImage CreateImageFromBytes(byte[] bytes)
public static BitmapImage CreateImageFromBytes(byte[] bytes, Action<BitmapImage>? onInit = null)
{
var image = new BitmapImage();
onInit?.Invoke(image);
using var stream = new MemoryStream(bytes);
image.SetSource(stream.AsRandomAccessStream());
return image;
Expand Down
2 changes: 1 addition & 1 deletion src/LipUI/Pages/Index/IndexPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static async ValueTask<LipIndex> RequestLipIndexAsync(string lipApiUrl)

//foreach all lip index pages

var text = await client.GetStringAsync($"https://{lipApiUrl}/search/teeth?page=1");
var text = await client.GetStringAsync($"https://{lipApiUrl}/search/teeth");
if (string.IsNullOrWhiteSpace(text))
ThrowException(lipApiUrl);

Expand Down
56 changes: 34 additions & 22 deletions src/LipUI/Pages/LipExecutionPanel/LipInstallerView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,47 @@ private async ValueTask<byte[]> DownloadLipPortable(InstallerInfo info)
var buffer = new byte[1024];
var output = File.Create(zipFilePath);

using var client = new HttpClient(
new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Automatic })
{
Timeout = TimeSpan.FromSeconds(5),
DefaultRequestHeaders = { ExpectContinue = false }
};
void InternalServices_WindowClosed() => output.Dispose();

HttpResponseMessage response;
InternalServices.WindowClosed += InternalServices_WindowClosed;

try
{
response = await client.GetAsync(info.AssetUrl, HttpCompletionOption.ResponseHeadersRead);
}
catch (Exception ex)
{
await InternalServices.ShowInfoBarAsync(ex, severity: InfoBarSeverity.Warning);
response = await client.GetAsync($"{Main.Config.GeneralSettings.GithubProxy}/{info.AssetUrl}");
}
using var client = new HttpClient(new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Automatic })
{
Timeout = TimeSpan.FromSeconds(5),
DefaultRequestHeaders = { ExpectContinue = false }
};

HttpResponseMessage response;

var input = await response.Content.ReadAsStreamAsync();
try
{
response = await client.GetAsync(info.AssetUrl, HttpCompletionOption.ResponseHeadersRead);
}
catch (Exception ex)
{
await InternalServices.ShowInfoBarAsync(ex, severity: InfoBarSeverity.Warning);
response = await client.GetAsync($"{Main.Config.GeneralSettings.GithubProxy}/{info.AssetUrl}");
}

int totalBytesRead = 0, bytesRead = 0;
while (true)
var input = await response.Content.ReadAsStreamAsync();

int totalBytesRead = 0, bytesRead = 0;
while (true)
{
bytesRead = await input.ReadAsync(buffer);
if (bytesRead is 0) break;
totalBytesRead += bytesRead;
DownloadProgressChanged?.Invoke(totalBytesRead);
await output.WriteAsync(buffer.AsMemory(0, bytesRead));
}
}
catch (Exception)
{
bytesRead = await input.ReadAsync(buffer);
if (bytesRead is 0) break;
totalBytesRead += bytesRead;
DownloadProgressChanged?.Invoke(totalBytesRead);
await output.WriteAsync(buffer.AsMemory(0, bytesRead));
output.Dispose();
InternalServices.WindowClosed -= InternalServices_WindowClosed;
throw;
}

output.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ private async void CheckBox_Checked(object sender, RoutedEventArgs e)
if (path is not null)
{
var image = InternalServices.CreateImageFromBytes(await File.ReadAllBytesAsync(path));

image.DecodePixelType = DecodePixelType.Logical;
image.DecodePixelWidth = 256;
PreviewImage.Source = image;
Expand Down
9 changes: 7 additions & 2 deletions src/LipUI/Pages/ToothInfoPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@
Foreground="{ThemeResource TextFillColorSecondary}"/>

<TextBlock
x:Name="LatestReleaseTime"
x:Name="LatestVersionReleasedAt"
Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondary}"/>

<TextBlock
x:Name="SourceRepoCreatedAt"
Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondary}"/>

<TextBlock
x:Name="DownloadCount"
x:Name="SourceRepoStarCount"
Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondary}"/>

Expand Down
13 changes: 4 additions & 9 deletions src/LipUI/Pages/ToothInfoPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Net.Http;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI;

Expand Down Expand Up @@ -40,12 +38,9 @@ private LipIndex.LipIndexData.LipToothItem ToothItem
Description.Text = toothItem.Description;
Author.Text = toothItem.Author;
LatestVersion.Text = toothItem.LatestVersion;
LatestReleaseTime.Text
= DateTimeOffset
.FromUnixTimeSeconds(toothItem.LatestVersionReleaseTime)
.LocalDateTime
.ToString();
DownloadCount.Text = toothItem.DownloadCount.ToString();
LatestVersionReleasedAt.Text = toothItem.LatestVersionReleasedAt;
SourceRepoCreatedAt.Text = toothItem.SourceRepoCreatedAt;
SourceRepoStarCount.Text = toothItem.SourceRepoStarCount.ToString();

}
}
Expand All @@ -54,7 +49,7 @@ private LipIndex.LipIndexData.LipToothItem ToothItem

public ToothInfoPage()
{
this.InitializeComponent();
InitializeComponent();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
Expand Down
27 changes: 18 additions & 9 deletions src/LipUI/Protocol/LipIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ public class LipIndexData

public class LipToothItem
{
[JsonPropertyName("toothRepoPath")]
[JsonPropertyName("repoPath")]
public string RepoPath { get; set; } = string.Empty;

[JsonPropertyName("toothRepoOwner")]
[JsonPropertyName("repoOwner")]
public string RepoOwner { get; set; } = string.Empty;

[JsonPropertyName("toothRepoName")]
[JsonPropertyName("repoName")]
public string RepoName { get; set; } = string.Empty;

[JsonPropertyName("latestVersion")]
public string LatestVersion { get; set; } = string.Empty;

[JsonPropertyName("latestVersionReleasedAt")]
public string LatestVersionReleasedAt { get; set; } = string.Empty;

[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;

Expand All @@ -47,14 +53,17 @@ public class LipToothItem
[JsonPropertyName("tags")]
public IReadOnlyList<string> Tags { get; set; } = new List<string>();

[JsonPropertyName("latestVersion")]
public string LatestVersion { get; set; } = string.Empty;
[JsonPropertyName("avatarUrl")]
public string AvatarUrl { get; set; } = string.Empty;

[JsonPropertyName("source")]
public string Source { get; set; } = string.Empty;

[JsonPropertyName("latestVersionReleaseTime")]
public long LatestVersionReleaseTime { get; set; }
[JsonPropertyName("sourceRepoCreatedAt")]
public string SourceRepoCreatedAt { get; set; } = string.Empty;

[JsonPropertyName("downloadCount")]
public int DownloadCount { get; set; }
[JsonPropertyName("sourceRepoStarCount")]
public int SourceRepoStarCount { get; set; }
}
}

Expand Down
61 changes: 46 additions & 15 deletions src/LipUI/Protocol/LipTooth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@

namespace LipUI.Protocol;

public struct LipToothVersion
{
public LipToothVersion()
{
}

[JsonPropertyName("version")]
public string Version { get; set; } = string.Empty;

[JsonPropertyName("releasedAt")]
public string ReleasedAt { get; set; } = string.Empty;
}

public class LipTooth
{
[JsonPropertyName("apiVersion")]
Expand All @@ -15,32 +28,50 @@ public class LipTooth

public class LipToothData
{
[JsonPropertyName("toothRepoOwner")]
public string ToothRepoOwner { get; set; } = string.Empty;
[JsonPropertyName("repoPath")]
public string RepoPath { get; set; } = string.Empty;

[JsonPropertyName("repoOwner")]
public string RepoOwner { get; set; } = string.Empty;

[JsonPropertyName("toothRepoName")]
public string ToothRepoName { get; set; } = string.Empty;
[JsonPropertyName("repoName")]
public string RepoName { get; set; } = string.Empty;

[JsonPropertyName("version")]
public string Version { get; set; } = string.Empty;

[JsonPropertyName("releaseTime")]
public long ReleaseTime { get; set; }
[JsonPropertyName("releasedAt")]
public string ReleasedAt { get; set; } = string.Empty;

[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;

[JsonPropertyName("description")]
public string Description { get; set; } = string.Empty;

[JsonPropertyName("author")]
public string Author { get; set; } = string.Empty;

[JsonPropertyName("tags")]
public IReadOnlyList<string> Tags { get; set; } = new List<string>();

[JsonPropertyName("avatarUrl")]
public string AvatarUrl { get; set; } = string.Empty;

[JsonPropertyName("Source")]
public string Source { get; set; } = string.Empty;

[JsonPropertyName("sourceRepoCreatedAt")]
public string SourceRepoCreatedAt { get; set; } = string.Empty;

[JsonPropertyName("sourceRepoStarCount")]
public int SourceRepoStarCount { get; set; }

[JsonPropertyName("versions")]
public IReadOnlyList<LipToothVersion> Versions { get; set; } = new List<LipToothVersion>();

[JsonPropertyName("downloadCount")]
public int DownloadCount { get; set; }

public struct LipToothVersion
{
[JsonPropertyName("version")]
public string Version { get; set; }

[JsonPropertyName("releaseTime")]
public long ReleaseTime { get; set; }
}
}

public static LipTooth Deserialize(string json)
Expand Down

0 comments on commit be000b4

Please sign in to comment.