diff --git a/DiscordChatExporter.Domain/Exporting/ExportContext.cs b/DiscordChatExporter.Domain/Exporting/ExportContext.cs index 660884d6..7c8eed85 100644 --- a/DiscordChatExporter.Domain/Exporting/ExportContext.cs +++ b/DiscordChatExporter.Domain/Exporting/ExportContext.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.IO; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; using DiscordChatExporter.Domain.Discord.Models; @@ -60,10 +61,18 @@ namespace DiscordChatExporter.Domain.Exporting if (!Request.ShouldDownloadMedia) return url; - var filePath = await _mediaDownloader.DownloadAsync(url).ConfigureAwait(false); + try + { + var filePath = await _mediaDownloader.DownloadAsync(url).ConfigureAwait(false); - // Return relative path so that the output files can be copied around without breaking - return Path.GetRelativePath(Request.OutputBaseDirPath, filePath); + // Return relative path so that the output files can be copied around without breaking + return Path.GetRelativePath(Request.OutputBaseDirPath, filePath); + } + catch (HttpRequestException) + { + // We don't want this to crash the exporting process in case of failure + return url; + } } } } \ No newline at end of file diff --git a/DiscordChatExporter.Domain/Exporting/MediaDownloader.cs b/DiscordChatExporter.Domain/Exporting/MediaDownloader.cs index 9fc681b2..e5a74f83 100644 --- a/DiscordChatExporter.Domain/Exporting/MediaDownloader.cs +++ b/DiscordChatExporter.Domain/Exporting/MediaDownloader.cs @@ -27,12 +27,12 @@ namespace DiscordChatExporter.Domain.Exporting if (_mediaPathMap.TryGetValue(url, out var cachedFilePath)) return cachedFilePath; - Directory.CreateDirectory(_workingDirPath); - var extension = Path.GetExtension(GetFileNameFromUrl(url)); var fileName = $"{Guid.NewGuid()}{extension}"; var filePath = Path.Combine(_workingDirPath, fileName); + Directory.CreateDirectory(_workingDirPath); + await _httpClient.DownloadAsync(url, filePath).ConfigureAwait(false); return _mediaPathMap[url] = filePath; @@ -41,7 +41,6 @@ namespace DiscordChatExporter.Domain.Exporting internal partial class MediaDownloader { - private static string GetFileNameFromUrl(string url) => - Regex.Match(url, @".+/([^?]*)").Groups[1].Value; + private static string GetFileNameFromUrl(string url) => Regex.Match(url, @".+/([^?]*)").Groups[1].Value; } } \ No newline at end of file