|
|
|
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using DiscordChatExporter.Domain.Internal;
|
|
|
|
@ -16,13 +18,16 @@ namespace DiscordChatExporter.Domain.Exporting
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient _httpClient = Singleton.HttpClient;
|
|
|
|
|
private readonly string _workingDirPath;
|
|
|
|
|
|
|
|
|
|
private readonly bool _reuseMedia;
|
|
|
|
|
private readonly AsyncRetryPolicy _httpRequestPolicy;
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<string, string> _pathMap = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
public MediaDownloader(string workingDirPath)
|
|
|
|
|
public MediaDownloader(string workingDirPath, bool reuseMedia)
|
|
|
|
|
{
|
|
|
|
|
_workingDirPath = workingDirPath;
|
|
|
|
|
_reuseMedia = reuseMedia;
|
|
|
|
|
|
|
|
|
|
_httpRequestPolicy = Policy
|
|
|
|
|
.Handle<IOException>()
|
|
|
|
@ -37,11 +42,18 @@ namespace DiscordChatExporter.Domain.Exporting
|
|
|
|
|
return cachedFilePath;
|
|
|
|
|
|
|
|
|
|
var fileName = GetFileNameFromUrl(url);
|
|
|
|
|
var filePath = PathEx.MakeUniqueFilePath(Path.Combine(_workingDirPath, fileName));
|
|
|
|
|
var filePath = Path.Combine(_workingDirPath, fileName);
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(_workingDirPath);
|
|
|
|
|
if (!_reuseMedia)
|
|
|
|
|
{
|
|
|
|
|
filePath = PathEx.MakeUniqueFilePath(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _httpClient.DownloadAsync(url, filePath);
|
|
|
|
|
if (!_reuseMedia || !File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(_workingDirPath);
|
|
|
|
|
await _httpClient.DownloadAsync(url, filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _pathMap[url] = filePath;
|
|
|
|
|
});
|
|
|
|
@ -50,6 +62,23 @@ namespace DiscordChatExporter.Domain.Exporting
|
|
|
|
|
|
|
|
|
|
internal partial class MediaDownloader
|
|
|
|
|
{
|
|
|
|
|
private static int URL_HASH_LENGTH = 5;
|
|
|
|
|
private static string HashUrl(string url)
|
|
|
|
|
{
|
|
|
|
|
using (var md5 = MD5.Create())
|
|
|
|
|
{
|
|
|
|
|
var inputBytes = Encoding.UTF8.GetBytes(url);
|
|
|
|
|
var hashBytes = md5.ComputeHash(inputBytes);
|
|
|
|
|
|
|
|
|
|
var hashBuilder = new StringBuilder();
|
|
|
|
|
for (int i = 0; i < hashBytes.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
hashBuilder.Append(hashBytes[i].ToString("X2"));
|
|
|
|
|
}
|
|
|
|
|
return hashBuilder.ToString().Truncate(URL_HASH_LENGTH);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetRandomFileName() => Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
|
|
|
|
|
|
|
|
|
|
private static string GetFileNameFromUrl(string url)
|
|
|
|
@ -57,10 +86,10 @@ namespace DiscordChatExporter.Domain.Exporting
|
|
|
|
|
var originalFileName = Regex.Match(url, @".+/([^?]*)").Groups[1].Value;
|
|
|
|
|
|
|
|
|
|
var fileName = !string.IsNullOrWhiteSpace(originalFileName)
|
|
|
|
|
? $"{Path.GetFileNameWithoutExtension(originalFileName).Truncate(50)}{Path.GetExtension(originalFileName)}"
|
|
|
|
|
? $"{Path.GetFileNameWithoutExtension(originalFileName).Truncate(42)}-({HashUrl(url)}){Path.GetExtension(originalFileName)}"
|
|
|
|
|
: GetRandomFileName();
|
|
|
|
|
|
|
|
|
|
return PathEx.EscapePath(fileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|