Improve Pandoc download and extraction process; fixed *.tar.gz handling

This commit is contained in:
Thorsten Sommer 2025-05-30 14:32:51 +02:00
parent df3c28e55f
commit 7f19a6b07d
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -1,4 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using System.Formats.Tar;
using System.IO.Compression; using System.IO.Compression;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -112,33 +113,38 @@ public static partial class Pandoc
{ {
if (!Directory.Exists(installDir)) if (!Directory.Exists(installDir))
Directory.CreateDirectory(installDir); Directory.CreateDirectory(installDir);
using var client = new HttpClient(); // Create a temporary file to download the archive to:
var pandocTempDownloadFile = Path.GetTempFileName();
//
// Download the latest Pandoc archive from GitHub:
//
var uri = await GenerateArchiveUriAsync(); var uri = await GenerateArchiveUriAsync();
using (var client = new HttpClient())
var response = await client.GetAsync(uri);
if (!response.IsSuccessStatusCode)
{ {
await MessageBus.INSTANCE.SendError(new (Icons.Material.Filled.Error, "Pandoc was not installed successfully, because the archive was not found.")); var response = await client.GetAsync(uri);
LOG.LogError("Pandoc was not installed, the release archive was not found (status code {StatusCode}): url='{Uri}', message='{Message}'", response.StatusCode, uri, response.RequestMessage); if (!response.IsSuccessStatusCode)
return; {
} await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Error, "Pandoc was not installed successfully, because the archive was not found."));
LOG.LogError("Pandoc was not installed, the release archive was not found (status code {StatusCode}): url='{Uri}', message='{Message}'", response.StatusCode, uri, response.RequestMessage);
var fileBytes = await response.Content.ReadAsByteArrayAsync(); return;
}
if (uri.Contains(".zip")) // Download the archive to the temporary file:
{ await using var tempFileStream = File.Create(pandocTempDownloadFile);
var tempZipPath = Path.Join(Path.GetTempPath(), "pandoc.zip"); await response.Content.CopyToAsync(tempFileStream);
await File.WriteAllBytesAsync(tempZipPath, fileBytes);
ZipFile.ExtractToDirectory(tempZipPath, installDir);
File.Delete(tempZipPath);
} }
else if (uri.Contains(".tar.gz"))
if (uri.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
{ {
var tempTarPath = Path.Join(Path.GetTempPath(), "pandoc.tar.gz"); ZipFile.ExtractToDirectory(pandocTempDownloadFile, installDir);
await File.WriteAllBytesAsync(tempTarPath, fileBytes); }
ZipFile.ExtractToDirectory(tempTarPath, installDir); else if (uri.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase))
File.Delete(tempTarPath); {
await using var tgzStream = File.Open(pandocTempDownloadFile, FileMode.Open, FileAccess.Read, FileShare.Read);
await using var uncompressedStream = new GZipStream(tgzStream, CompressionMode.Decompress);
await TarFile.ExtractToDirectoryAsync(uncompressedStream, installDir, true);
} }
else else
{ {
@ -147,6 +153,7 @@ public static partial class Pandoc
return; return;
} }
File.Delete(pandocTempDownloadFile);
await MessageBus.INSTANCE.SendSuccess(new(Icons.Material.Filled.CheckCircle, $"Pandoc {await FetchLatestVersionAsync()} was installed successfully.")); await MessageBus.INSTANCE.SendSuccess(new(Icons.Material.Filled.CheckCircle, $"Pandoc {await FetchLatestVersionAsync()} was installed successfully."));
} }
catch (Exception ex) catch (Exception ex)