Ignore failures when downloading media

pull/352/head
Alexey Golub 4 years ago
parent ac5ccc3fa4
commit e58c7aefff

@ -2,6 +2,7 @@
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordChatExporter.Domain.Discord.Models; using DiscordChatExporter.Domain.Discord.Models;
@ -60,10 +61,18 @@ namespace DiscordChatExporter.Domain.Exporting
if (!Request.ShouldDownloadMedia) if (!Request.ShouldDownloadMedia)
return url; 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 relative path so that the output files can be copied around without breaking
return Path.GetRelativePath(Request.OutputBaseDirPath, filePath); return Path.GetRelativePath(Request.OutputBaseDirPath, filePath);
}
catch (HttpRequestException)
{
// We don't want this to crash the exporting process in case of failure
return url;
}
} }
} }
} }

@ -27,12 +27,12 @@ namespace DiscordChatExporter.Domain.Exporting
if (_mediaPathMap.TryGetValue(url, out var cachedFilePath)) if (_mediaPathMap.TryGetValue(url, out var cachedFilePath))
return cachedFilePath; return cachedFilePath;
Directory.CreateDirectory(_workingDirPath);
var extension = Path.GetExtension(GetFileNameFromUrl(url)); var extension = Path.GetExtension(GetFileNameFromUrl(url));
var fileName = $"{Guid.NewGuid()}{extension}"; var fileName = $"{Guid.NewGuid()}{extension}";
var filePath = Path.Combine(_workingDirPath, fileName); var filePath = Path.Combine(_workingDirPath, fileName);
Directory.CreateDirectory(_workingDirPath);
await _httpClient.DownloadAsync(url, filePath).ConfigureAwait(false); await _httpClient.DownloadAsync(url, filePath).ConfigureAwait(false);
return _mediaPathMap[url] = filePath; return _mediaPathMap[url] = filePath;
@ -41,7 +41,6 @@ namespace DiscordChatExporter.Domain.Exporting
internal partial class MediaDownloader internal partial class MediaDownloader
{ {
private static string GetFileNameFromUrl(string url) => private static string GetFileNameFromUrl(string url) => Regex.Match(url, @".+/([^?]*)").Groups[1].Value;
Regex.Match(url, @".+/([^?]*)").Groups[1].Value;
} }
} }
Loading…
Cancel
Save