|
|
|
@ -15,11 +15,12 @@ namespace DiscordChatExporter.Core.Exporting;
|
|
|
|
|
|
|
|
|
|
internal partial class ExportAssetDownloader
|
|
|
|
|
{
|
|
|
|
|
private static readonly AsyncKeyedLocker<string> _locker = new(o =>
|
|
|
|
|
private static readonly AsyncKeyedLocker<string> Locker = new(o =>
|
|
|
|
|
{
|
|
|
|
|
o.PoolSize = 20;
|
|
|
|
|
o.PoolInitialFill = 1;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
private readonly string _workingDirPath;
|
|
|
|
|
private readonly bool _reuse;
|
|
|
|
|
|
|
|
|
@ -37,14 +38,15 @@ internal partial class ExportAssetDownloader
|
|
|
|
|
var fileName = GetFileNameFromUrl(url);
|
|
|
|
|
var filePath = Path.Combine(_workingDirPath, fileName);
|
|
|
|
|
|
|
|
|
|
using (await _locker.LockAsync(filePath, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
using (await Locker.LockAsync(filePath, cancellationToken))
|
|
|
|
|
{
|
|
|
|
|
if (_pathCache.TryGetValue(url, out var cachedFilePath))
|
|
|
|
|
return cachedFilePath;
|
|
|
|
|
|
|
|
|
|
// Reuse existing files if we're allowed to
|
|
|
|
|
if (!_reuse || !File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
if (_reuse && File.Exists(filePath))
|
|
|
|
|
return _pathCache[url] = filePath;
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(_workingDirPath);
|
|
|
|
|
|
|
|
|
|
await Http.ResiliencePolicy.ExecuteAsync(async () =>
|
|
|
|
@ -78,7 +80,6 @@ internal partial class ExportAssetDownloader
|
|
|
|
|
// ignore exceptions thrown here.
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _pathCache[url] = filePath;
|
|
|
|
|
}
|
|
|
|
|