From 69acb5bf76d7a905fcbcf592fa6eb1135457f778 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Thu, 17 Dec 2020 10:13:47 -0500 Subject: [PATCH] Set last write time on downloaded media to the value provided in the Last-Modified header (#459) --- .../Internal/Extensions/HttpClientExtensions.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/DiscordChatExporter.Domain/Internal/Extensions/HttpClientExtensions.cs b/DiscordChatExporter.Domain/Internal/Extensions/HttpClientExtensions.cs index 789fd00..f230891 100644 --- a/DiscordChatExporter.Domain/Internal/Extensions/HttpClientExtensions.cs +++ b/DiscordChatExporter.Domain/Internal/Extensions/HttpClientExtensions.cs @@ -1,4 +1,7 @@ -using System.IO; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -8,11 +11,17 @@ namespace DiscordChatExporter.Domain.Internal.Extensions { public static async ValueTask DownloadAsync(this HttpClient httpClient, string uri, string outputFilePath) { - await using var input = await httpClient.GetStreamAsync(uri); + using var response = await httpClient.GetAsync(uri); var output = File.Create(outputFilePath); - await input.CopyToAsync(output); - await output.DisposeAsync(); + await response.Content.CopyToAsync(output); + + IEnumerable lastModifiedHeaderValues; + if (response.Content.Headers.TryGetValues("Last-Modified", out lastModifiedHeaderValues)) + { + await output.DisposeAsync(); + File.SetLastWriteTime(outputFilePath, DateTime.Parse(lastModifiedHeaderValues.First())); + } } } } \ No newline at end of file