From bd40dbde48f1f000a086d26138535e5b6222e8de Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Mon, 14 Jun 2021 20:43:44 +0300 Subject: [PATCH] Don't throw if file dates cannot be modified when downloading media Fixes #585 --- .../Exporting/MediaDownloader.cs | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/DiscordChatExporter.Core/Exporting/MediaDownloader.cs b/DiscordChatExporter.Core/Exporting/MediaDownloader.cs index 007a33b..b09dd8e 100644 --- a/DiscordChatExporter.Core/Exporting/MediaDownloader.cs +++ b/DiscordChatExporter.Core/Exporting/MediaDownloader.cs @@ -45,7 +45,7 @@ namespace DiscordChatExporter.Core.Exporting Directory.CreateDirectory(_workingDirPath); - // This catches IOExceptions which is dangerous as we're working also with files + // This retries on IOExceptions which is dangerous as we're also working with files await Http.ExceptionPolicy.ExecuteAsync(async () => { // Download the file @@ -56,17 +56,27 @@ namespace DiscordChatExporter.Core.Exporting } // Try to set the file date according to the last-modified header - var lastModified = response.Content.Headers.TryGetValue("Last-Modified")?.Pipe(s => - DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) - ? date - : (DateTimeOffset?) null - ); - - if (lastModified is not null) + try + { + var lastModified = response.Content.Headers.TryGetValue("Last-Modified")?.Pipe(s => + DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) + ? date + : (DateTimeOffset?) null + ); + + if (lastModified is not null) + { + File.SetCreationTimeUtc(filePath, lastModified.Value.UtcDateTime); + File.SetLastWriteTimeUtc(filePath, lastModified.Value.UtcDateTime); + File.SetLastAccessTimeUtc(filePath, lastModified.Value.UtcDateTime); + } + } + catch { - File.SetCreationTimeUtc(filePath, lastModified.Value.UtcDateTime); - File.SetLastWriteTimeUtc(filePath, lastModified.Value.UtcDateTime); - File.SetLastAccessTimeUtc(filePath, lastModified.Value.UtcDateTime); + // This can apparently fail for some reason. + // https://github.com/Tyrrrz/DiscordChatExporter/issues/585 + // Updating file dates is not a critical task, so we'll just + // ignore exceptions thrown here. } }); @@ -102,4 +112,4 @@ namespace DiscordChatExporter.Core.Exporting return PathEx.EscapePath(fileNameWithoutExtension.Truncate(42) + '-' + urlHash + fileExtension); } } -} +} \ No newline at end of file