-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
20 changed files
with
953 additions
and
940 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Totoro.Core.Contracts | ||
{ | ||
public interface IUpdateService | ||
{ | ||
IObservable<VersionInfo> OnUpdateAvailable { get; } | ||
Task<VersionInfo> DownloadUpdate(VersionInfo versionInfo); | ||
void InstallUpdate(VersionInfo versionInfo); | ||
} | ||
|
||
public class VersionInfo | ||
{ | ||
public Version Version { get; set; } | ||
public string Details { get; set; } | ||
public string Url { get; set; } | ||
public string FilePath { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
using System.Text.Json.Nodes; | ||
using Splat; | ||
|
||
namespace Totoro.Core.Services; | ||
|
||
public class WindowsUpdateService : IUpdateService, IEnableLogger | ||
{ | ||
private readonly IObservable<VersionInfo> _onUpdate; | ||
private readonly string _updateFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Totoro", "ApplicationData", "Updates"); | ||
public IObservable<VersionInfo> OnUpdateAvailable => _onUpdate; | ||
|
||
public WindowsUpdateService(HttpClient httpClient) | ||
{ | ||
_onUpdate = Observable | ||
.Timer(TimeSpan.Zero, TimeSpan.FromHours(1)) | ||
.ObserveOn(RxApp.TaskpoolScheduler) | ||
.SelectMany(_ => httpClient.GetStreamAsync("https://api.github.com/repos/athulrajts/AnimDL.GUI/releases/latest")) | ||
.Select(x => JsonNode.Parse(x)) | ||
.Select(jsonNode => new VersionInfo() | ||
{ | ||
Version = new Version(jsonNode["tag_name"].ToString()), | ||
Url = (string)jsonNode["assets"][0]["browser_download_url"].AsValue() | ||
}) | ||
.Log(this, "Latest Version", x => x.Version.ToString()) | ||
.Where(vi => vi.Version > Assembly.GetExecutingAssembly().GetName().Version) | ||
.Throttle(TimeSpan.FromSeconds(3)); | ||
} | ||
|
||
public async Task<VersionInfo> DownloadUpdate(VersionInfo versionInfo) | ||
{ | ||
Directory.CreateDirectory(_updateFolder); | ||
var ext = Path.GetExtension(versionInfo.Url); | ||
var fileName = $"Totoro_{versionInfo.Version}{ext}"; | ||
var fullPath = Path.Combine(_updateFolder, fileName); | ||
versionInfo.FilePath = fullPath; | ||
|
||
if(File.Exists(fullPath)) // already download before | ||
{ | ||
return versionInfo; | ||
} | ||
|
||
using var client = new HttpClient(); | ||
using var s = await client.GetStreamAsync(versionInfo.Url); | ||
using var fs = new FileStream(fullPath, FileMode.OpenOrCreate); | ||
await s.CopyToAsync(fs); | ||
return versionInfo; | ||
} | ||
|
||
public void InstallUpdate(VersionInfo versionInfo) | ||
{ | ||
Process.Start(new ProcessStartInfo | ||
{ | ||
FileName = "msiexec", | ||
Arguments = $"/i \"{versionInfo.FilePath}\"" | ||
}); | ||
Process.GetCurrentProcess().CloseMainWindow(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.