|
|
@ -1,11 +1,14 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using DiscordChatExporter.Domain.Internal;
|
|
|
|
using DiscordChatExporter.Domain.Internal;
|
|
|
|
using DiscordChatExporter.Domain.Internal.Extensions;
|
|
|
|
using DiscordChatExporter.Domain.Internal.Extensions;
|
|
|
|
|
|
|
|
using Polly;
|
|
|
|
|
|
|
|
using Polly.Retry;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DiscordChatExporter.Domain.Exporting
|
|
|
|
namespace DiscordChatExporter.Domain.Exporting
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -13,15 +16,22 @@ namespace DiscordChatExporter.Domain.Exporting
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly HttpClient _httpClient = Singleton.HttpClient;
|
|
|
|
private readonly HttpClient _httpClient = Singleton.HttpClient;
|
|
|
|
private readonly string _workingDirPath;
|
|
|
|
private readonly string _workingDirPath;
|
|
|
|
|
|
|
|
private readonly AsyncRetryPolicy _httpRequestPolicy;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<string, string> _pathMap = new Dictionary<string, string>();
|
|
|
|
private readonly Dictionary<string, string> _pathMap = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
|
|
public MediaDownloader(string workingDirPath)
|
|
|
|
public MediaDownloader(string workingDirPath)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_workingDirPath = workingDirPath;
|
|
|
|
_workingDirPath = workingDirPath;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_httpRequestPolicy = Policy
|
|
|
|
|
|
|
|
.Handle<IOException>()
|
|
|
|
|
|
|
|
.WaitAndRetryAsync(8, i => TimeSpan.FromSeconds(0.5 * i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async ValueTask<string> DownloadAsync(string url)
|
|
|
|
public async ValueTask<string> DownloadAsync(string url)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return await _httpRequestPolicy.ExecuteAsync(async () =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_pathMap.TryGetValue(url, out var cachedFilePath))
|
|
|
|
if (_pathMap.TryGetValue(url, out var cachedFilePath))
|
|
|
|
return cachedFilePath;
|
|
|
|
return cachedFilePath;
|
|
|
@ -34,6 +44,7 @@ namespace DiscordChatExporter.Domain.Exporting
|
|
|
|
await _httpClient.DownloadAsync(url, filePath);
|
|
|
|
await _httpClient.DownloadAsync(url, filePath);
|
|
|
|
|
|
|
|
|
|
|
|
return _pathMap[url] = filePath;
|
|
|
|
return _pathMap[url] = filePath;
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|