mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-08-20 09:49:46 +02:00
.github
Application
Browser
Configuration
Controls
Dialogs
Management
Plugins
Properties
Resources
Updates
Utils
bld
lib
TweetLib.Communication
TweetLib.Core
Application
Browser
Collections
Data
Features
Configuration
Notifications
Plugins
Twitter
Updates
IUpdateCheckClient.cs
UpdateCheckEventArgs.cs
UpdateDownloadStatus.cs
UpdateHandler.cs
UpdateInfo.cs
Serialization
Utils
App.cs
Lib.cs
TweetLib.Core.csproj
TweetTest.System
TweetTest.Unit
subprocess
video
.gitattributes
.gitignore
LICENSE.md
Program.cs
README.md
Reporter.cs
TweetDuck.csproj
TweetDuck.sln
TweetDuck.sln.DotSettings
Version.cs
packages.config
95 lines
3.2 KiB
C#
95 lines
3.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using TweetLib.Core.Utils;
|
|
|
|
namespace TweetLib.Core.Features.Updates{
|
|
public sealed class UpdateInfo{
|
|
public string VersionTag { get; }
|
|
public string ReleaseNotes { get; }
|
|
public string InstallerPath { get; }
|
|
|
|
public UpdateDownloadStatus DownloadStatus { get; private set; }
|
|
public Exception? DownloadError { get; private set; }
|
|
|
|
private readonly string downloadUrl;
|
|
private readonly string installerFolder;
|
|
private WebClient? currentDownload;
|
|
|
|
public UpdateInfo(string versionTag, string releaseNotes, string downloadUrl, string installerFolder){
|
|
this.downloadUrl = downloadUrl;
|
|
this.installerFolder = installerFolder;
|
|
|
|
this.VersionTag = versionTag;
|
|
this.ReleaseNotes = releaseNotes;
|
|
this.InstallerPath = Path.Combine(installerFolder, $"{Lib.BrandName}.{versionTag}.exe");
|
|
}
|
|
|
|
public void BeginSilentDownload(){
|
|
if (FileUtils.FileExistsAndNotEmpty(InstallerPath)){
|
|
DownloadStatus = UpdateDownloadStatus.Done;
|
|
return;
|
|
}
|
|
|
|
if (DownloadStatus == UpdateDownloadStatus.None || DownloadStatus == UpdateDownloadStatus.Failed){
|
|
DownloadStatus = UpdateDownloadStatus.InProgress;
|
|
|
|
if (string.IsNullOrEmpty(downloadUrl)){
|
|
DownloadError = new InvalidDataException("Missing installer asset.");
|
|
DownloadStatus = UpdateDownloadStatus.AssetMissing;
|
|
return;
|
|
}
|
|
|
|
try{
|
|
Directory.CreateDirectory(installerFolder);
|
|
}catch(Exception e){
|
|
DownloadError = e;
|
|
DownloadStatus = UpdateDownloadStatus.Failed;
|
|
return;
|
|
}
|
|
|
|
WebClient client = WebUtils.NewClient($"{Lib.BrandName} {TweetDuck.Version.Tag}");
|
|
|
|
client.DownloadFileCompleted += WebUtils.FileDownloadCallback(InstallerPath, () => {
|
|
DownloadStatus = UpdateDownloadStatus.Done;
|
|
currentDownload = null;
|
|
}, e => {
|
|
DownloadError = e;
|
|
DownloadStatus = UpdateDownloadStatus.Failed;
|
|
currentDownload = null;
|
|
});
|
|
|
|
client.DownloadFileAsync(new Uri(downloadUrl), InstallerPath);
|
|
}
|
|
}
|
|
|
|
public void DeleteInstaller(){
|
|
DownloadStatus = UpdateDownloadStatus.None;
|
|
|
|
if (currentDownload != null && currentDownload.IsBusy){
|
|
currentDownload.CancelAsync(); // deletes file when cancelled
|
|
return;
|
|
}
|
|
|
|
try{
|
|
File.Delete(InstallerPath);
|
|
}catch{
|
|
// rip
|
|
}
|
|
}
|
|
|
|
public void CancelDownload(){
|
|
DeleteInstaller();
|
|
DownloadStatus = UpdateDownloadStatus.Canceled;
|
|
}
|
|
|
|
public override bool Equals(object obj){
|
|
return obj is UpdateInfo info && VersionTag == info.VersionTag;
|
|
}
|
|
|
|
public override int GetHashCode(){
|
|
return VersionTag.GetHashCode();
|
|
}
|
|
}
|
|
}
|