mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-22 10:55:15 -04:00
parent
e58c7aefff
commit
1f20915b4d
1 changed files with 17 additions and 11 deletions
|
@ -9,38 +9,44 @@ using DiscordChatExporter.Domain.Internal.Extensions;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Domain.Exporting
|
namespace DiscordChatExporter.Domain.Exporting
|
||||||
{
|
{
|
||||||
internal partial class MediaDownloader
|
internal class MediaDownloader
|
||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient = Singleton.HttpClient;
|
private readonly HttpClient _httpClient = Singleton.HttpClient;
|
||||||
private readonly string _workingDirPath;
|
private readonly string _workingDirPath;
|
||||||
|
|
||||||
private readonly Dictionary<string, string> _mediaPathMap = 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GetRandomSuffix() => Guid.NewGuid().ToString().Replace("-", "").Substring(0, 8);
|
||||||
|
|
||||||
|
private string GetFileNameFromUrl(string url)
|
||||||
|
{
|
||||||
|
var originalFileName = Regex.Match(url, @".+/([^?]*)").Groups[1].Value;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(originalFileName))
|
||||||
|
return GetRandomSuffix();
|
||||||
|
|
||||||
|
return $"{Path.GetFileNameWithoutExtension(originalFileName)}-{GetRandomSuffix()}{Path.GetExtension(originalFileName)}";
|
||||||
|
}
|
||||||
|
|
||||||
// HACK: ConfigureAwait() is crucial here to enable sync-over-async in HtmlMessageWriter
|
// HACK: ConfigureAwait() is crucial here to enable sync-over-async in HtmlMessageWriter
|
||||||
public async ValueTask<string> DownloadAsync(string url)
|
public async ValueTask<string> DownloadAsync(string url)
|
||||||
{
|
{
|
||||||
if (_mediaPathMap.TryGetValue(url, out var cachedFilePath))
|
if (_pathMap.TryGetValue(url, out var cachedFilePath))
|
||||||
return cachedFilePath;
|
return cachedFilePath;
|
||||||
|
|
||||||
var extension = Path.GetExtension(GetFileNameFromUrl(url));
|
var fileName = GetFileNameFromUrl(url);
|
||||||
var fileName = $"{Guid.NewGuid()}{extension}";
|
|
||||||
var filePath = Path.Combine(_workingDirPath, fileName);
|
var filePath = Path.Combine(_workingDirPath, fileName);
|
||||||
|
|
||||||
Directory.CreateDirectory(_workingDirPath);
|
Directory.CreateDirectory(_workingDirPath);
|
||||||
|
|
||||||
await _httpClient.DownloadAsync(url, filePath).ConfigureAwait(false);
|
await _httpClient.DownloadAsync(url, filePath).ConfigureAwait(false);
|
||||||
|
|
||||||
return _mediaPathMap[url] = filePath;
|
return _pathMap[url] = filePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal partial class MediaDownloader
|
|
||||||
{
|
|
||||||
private static string GetFileNameFromUrl(string url) => Regex.Match(url, @".+/([^?]*)").Groups[1].Value;
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue