Ignore failures when downloading media

This commit is contained in:
Alexey Golub
2020-07-18 22:08:06 +03:00
parent ac5ccc3fa4
commit e58c7aefff
2 changed files with 15 additions and 7 deletions

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}