From 0d37ac61c1de6176c0b49096ba3bbf13a8ebc295 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 8 Feb 2021 22:23:47 +1100 Subject: [PATCH] fix filepath containing extension when using Downloader standalone --- Droplex/Downloader.cs | 10 +++++++--- Droplex/Droplex.csproj | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Droplex/Downloader.cs b/Droplex/Downloader.cs index cb9720c..9ec6201 100644 --- a/Droplex/Downloader.cs +++ b/Droplex/Downloader.cs @@ -22,13 +22,17 @@ public static async Task Get(string url, string filePath) if (File.Exists(filePath)) File.Delete(filePath); + // filePath could be passed in with or without extension + var filePathWithoutExtension = + Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath)); + var client = new HttpClient(new SocketsHttpHandler(), false); client.DefaultRequestHeaders.Add("User-Agent", UserAgent); using var response = await client.GetAsync(url).ConfigureAwait(false); if (response.StatusCode == HttpStatusCode.OK) { - await using var fileStream = new FileStream(filePath, FileMode.CreateNew); + await using var fileStream = new FileStream(filePathWithoutExtension, FileMode.CreateNew); await response.Content.CopyToAsync(fileStream).ConfigureAwait(false); } else @@ -37,12 +41,12 @@ public static async Task Get(string url, string filePath) } var extension = Path.GetExtension(url); - var downloadedFile = $"{filePath}{extension}"; + var downloadedFile = $"{filePathWithoutExtension}{extension}"; if (File.Exists(downloadedFile)) File.Delete(downloadedFile); - File.Move(filePath, downloadedFile); + File.Move(filePathWithoutExtension, downloadedFile); } } } diff --git a/Droplex/Droplex.csproj b/Droplex/Droplex.csproj index db27ec9..d3fdaa8 100644 --- a/Droplex/Droplex.csproj +++ b/Droplex/Droplex.csproj @@ -10,9 +10,9 @@ - 1.0.4 - 1.0.4 - 1.0.4 + 1.0.5 + 1.0.5 + 1.0.5 Droplex Jeremy Wu MIT