mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-02 16:19:12 +00:00
Ignore failures when downloading media
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user