Skip to content

Commit

Permalink
Refactor to centralise DownloadFile from URL as well as correct paths…
Browse files Browse the repository at this point in the history
… for Debian packages.
  • Loading branch information
CartBlanche committed Jun 27, 2024
1 parent 44ec1bb commit e1cee70
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions Source/v2/Meadow.Dfu/DfuUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ namespace Meadow.CLI.Core.Internals.Dfu;

public static class DfuUtils
{
private static readonly int _osAddress = 0x08000000;

private const string DFU_UTIL_AMD64_URL = "http://ftp.de.debian.org/debian/pool/main/d/dfu-util/dfu-util_0.11-1_amd64.deb";
private const string DFU_UTIL_ARM64_URL = "http://ftp.de.debian.org/debian/pool/main/d/dfu-util/dfu-util_0.11-1_arm64.deb";
private const string DFU_UTIL = "dfu-util";

public const string DEFAULT_DFU_VERSION = "0.11";
private static readonly int _osAddress = 0x08000000;

public enum DfuFlashFormat
{
Expand Down Expand Up @@ -238,24 +242,12 @@ private static async Task<bool> InstallDfuUtilOnWindows(string tempFolder, strin

var downloadUrl = $"https://s3-us-west-2.amazonaws.com/downloads.wildernesslabs.co/public/dfu-util-{dfuUtilVersion}-binaries.zip";

var downloadFileName = downloadUrl.Substring(downloadUrl.LastIndexOf("/", StringComparison.Ordinal) + 1);

var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

if (response.IsSuccessStatusCode == false)
{
throw new Exception("Failed to download dfu-util");
}
var downloadedFileName = downloadUrl.Substring(downloadUrl.LastIndexOf("/", StringComparison.Ordinal) + 1);

using (var stream = await response.Content.ReadAsStreamAsync())
using (var downloadFileStream = new DownloadFileStream(stream))
using (var fs = File.OpenWrite(Path.Combine(tempFolder, downloadFileName)))
{
await downloadFileStream.CopyToAsync(fs);
}
await DownloadFile(downloadUrl, downloadedFileName, cancellationToken);

ZipFile.ExtractToDirectory(
Path.Combine(tempFolder, downloadFileName),
Path.Combine(tempFolder, downloadedFileName),
tempFolder);

var is64Bit = Environment.Is64BitOperatingSystem;
Expand Down Expand Up @@ -423,9 +415,6 @@ public static async Task<bool> CheckIfDfuUtilIsInstalledOnLinux(
}
}

private const string Arm64Url = "http://archive.ubuntu.com/ubuntu/pool/universe/d/dfu-util/dfu-util_0.11-1_arm64.deb";
private const string Amd64Url = "http://archive.ubuntu.com/ubuntu/pool/universe/d/dfu-util/dfu-util_0.11-1_amd64.deb";

private static async Task<bool> InstallPackageOnLinux(string package)
{
string osReleaseFile = "/etc/os-release";
Expand Down Expand Up @@ -456,25 +445,25 @@ private static async Task<bool> InstallPackageOnLinux(string package)
// Install the default package for this distro
await InstallPackageOnUbuntu(package);

// We check the version again, because on some versions of Ubuntu the default dfu-util version is 0.9 :(
var installedDfuUtilVersion = new Version(await GetDfuUtilVersion());
var expectedDfuUtilVersion = new Version(DEFAULT_DFU_VERSION);
if (installedDfuUtilVersion.CompareTo(expectedDfuUtilVersion) < 0)
{
var dfuPackageUrl = RuntimeInformation.OSArchitecture switch
{
Architecture.Arm64 => Arm64Url,
Architecture.X64 => Amd64Url,
Architecture.Arm64 => DFU_UTIL_ARM64_URL,
Architecture.X64 => DFU_UTIL_AMD64_URL,
_ => throw new PlatformNotSupportedException("Unsupported architecture")
};

var fileName = Path.GetFileName(new Uri(dfuPackageUrl).LocalPath);
await DownloadFileAsync(dfuPackageUrl, fileName);
await DownloadFile(dfuPackageUrl, fileName);
await InstallDownloadedDebianPackage(fileName);

var recentlyInstalledDfuUtilVersion = new Version(await GetDfuUtilVersion());
if (recentlyInstalledDfuUtilVersion.CompareTo(expectedDfuUtilVersion) != 0)
{
//throw new Exception($"Package {package} Installation Failed. Error:{exitCode}");
throw new Exception($"Unable to install the version {expectedDfuUtilVersion} of {package}.");
}
}

Expand All @@ -497,14 +486,17 @@ private static async Task InstallDownloadedDebianPackage(string fileName)
await RunProcessCommand("sudo", $"dpkg -i {fileName}");
}

private static async Task DownloadFileAsync(string dfuPackageUrl, string fileName)
private static async Task DownloadFile(string downloadUrl, string downloadedFileName, CancellationToken cancellationToken = default)
{
using HttpClient client = new();
HttpResponseMessage response = await client.GetAsync(dfuPackageUrl);
response.EnsureSuccessStatusCode();
using var client = new HttpClient();
var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"Failed to download {downloadedFileName} from {downloadUrl}");
}

using Stream contentStream = await response.Content.ReadAsStreamAsync();
using FileStream fileStream = new(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
using FileStream fileStream = new(downloadedFileName, FileMode.Create, FileAccess.Write, FileShare.None);
await contentStream.CopyToAsync(fileStream);
}

Expand Down

0 comments on commit e1cee70

Please sign in to comment.