Skip to content

Commit

Permalink
Implement zipped release download
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed May 8, 2024
1 parent fc3071e commit 11d89d5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
36 changes: 26 additions & 10 deletions src/ImeSense.Launchers.Belarus.Core/Services/UpdaterService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.IO.Compression;

using ImeSense.Launchers.Belarus.Core.Manager;
using ImeSense.Launchers.Belarus.Core.Storage;

Expand All @@ -13,37 +15,51 @@ public class UpdaterService(ILogger<UpdaterService> logger, IGitStorageApiServic

public async Task UpdaterAsync(Uri uri, string fileSavePath, CancellationToken cancellationToken = default)
{
var appName = Path.GetFileNameWithoutExtension(fileSavePath);
var fullAppName = Path.GetFileName(fileSavePath);
var fileName = Path.GetFileNameWithoutExtension(fileSavePath);
var fullFileName = Path.GetFileName(fileSavePath);

var lastRelease = await _gitStorageApiService.GetLastReleaseAsync(uri, cancellationToken)
?? throw new NullReferenceException("Latest release is null!");
var sblauncher = lastRelease.Assets?.FirstOrDefault(x => x.Name.Equals(fullAppName))
?? throw new NullReferenceException($"{appName} asset is null!");
var sblauncher = lastRelease.Assets?.FirstOrDefault(x => x.Name.Equals(fullFileName))
?? throw new NullReferenceException($"{fileName} asset is null!");

if (sblauncher.BrowserDownloadUrl == null) {
throw new NullReferenceException("Browser download url is null!");
}
var progress = new Progress<int>(percentage => {
_logger.LogInformation("{appName} is {percentage}% downloaded", appName, percentage);
_logger.LogInformation("{fileName} is {percentage}% downloaded", fileName, percentage);
});

var pathDownloadFolder = Path.Combine(DirectoryStorage.Base, "temp");
var fileDownloadPath = Path.Combine(pathDownloadFolder, fullAppName);
var fileDownloadPath = Path.Combine(pathDownloadFolder, fullFileName);

if (!Directory.Exists(pathDownloadFolder)) {
Directory.CreateDirectory(pathDownloadFolder);
}

await _fileDownloadManager.DownloadAsync(sblauncher.BrowserDownloadUrl, fileDownloadPath, progress, cancellationToken);

if (File.Exists(fileSavePath)) {
File.Delete(fileSavePath);
ExtractFile(fileDownloadPath, fileSavePath);
CleanUpTempFiles();
}

private static void ExtractFile(string sourcePath, string destinationPath)
{
if (Path.GetExtension(sourcePath).Equals(".zip", StringComparison.OrdinalIgnoreCase)) {
ZipFile.ExtractToDirectory(sourcePath, DirectoryStorage.Base, true);
} else {
if (File.Exists(destinationPath)) {
File.Delete(destinationPath);
}
File.Move(sourcePath, destinationPath);
}
File.Move(fileDownloadPath, fileSavePath);
}

private static void CleanUpTempFiles()
{
var pathDownloadFolder = Path.Combine(DirectoryStorage.Base, "temp");
if (Directory.Exists(pathDownloadFolder)) {
Directory.Delete(pathDownloadFolder);
Directory.Delete(pathDownloadFolder, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class FileNameStorage
public static string CryptoHasherLog => "CryptoHasherReport-";
public static string WebResources => "WebResources.json";
public static string SBLauncher => "SBLauncher.exe";
public static string SBLauncherZip => "sblauncher-publish.zip";
public static string SBLauncherUpdater => "SBLauncherUpdater.exe";
public static string GameSetting => "user.ltx";
public static string LauncherSetting => "sblauncher.json";
Expand Down
1 change: 1 addition & 0 deletions src/ImeSense.Launchers.Belarus.Core/Storage/PathStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class PathStorage
// Актуально для версии 3.0 и новее
public static string LauncherSetting => Path.Combine(DirectoryStorage.LaucherData, FileNameStorage.LauncherSetting);
public static string CurrentRelease => Path.Combine(DirectoryStorage.LauncherCache, FileNameStorage.CurrentRelease);
public static string LegacyGameUser => Path.Combine(DirectoryStorage.LegacyUserData, FileNameStorage.GameSetting);
public static string GameUser => Path.Combine(DirectoryStorage.AppData, FileNameStorage.GameSetting);
public static string NewsCache => Path.Combine(DirectoryStorage.LauncherCache, FileNameStorage.NewsContent);
public static string WebResourcesCache => Path.Combine(DirectoryStorage.LauncherCache, FileNameStorage.WebResources);
Expand Down
9 changes: 6 additions & 3 deletions src/ImeSense.Launchers.Belarus.Updater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
}

logger.LogInformation($"Start update");
var fileSavePath = Path.Combine(DirectoryStorage.Base, FileNameStorage.SBLauncher);
var fileSavePath = Path.Combine(DirectoryStorage.Base, FileNameStorage.SBLauncherZip);

using var httpClient = new HttpClient(new HttpClientHandler {
SslProtocols = System.Security.Authentication.SslProtocols.Tls12
});

using var httpClient = new HttpClient();
httpClient.BaseAddress = UriStorage.LauncherApiUri;
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
Expand All @@ -38,7 +41,7 @@
await updaterService.UpdaterAsync(UriStorage.LauncherApiUri, fileSavePath);

logger.LogInformation($"Finish!");
Launcher.Launch(fileSavePath)?.Start();
Launcher.Launch(Path.Combine(DirectoryStorage.Base, FileNameStorage.SBLauncher))?.Start();
} catch (Exception ex) {
logger.LogInformation("{Message}", ex.Message);
logger.LogInformation("{StackTrace}", ex.StackTrace);
Expand Down

0 comments on commit 11d89d5

Please sign in to comment.