Clean up last commit

This commit is contained in:
Tyrrrz 2023-02-13 18:52:18 +02:00
parent ea4cb43479
commit f1ae0266f1

View file

@ -15,11 +15,12 @@ namespace DiscordChatExporter.Core.Exporting;
internal partial class ExportAssetDownloader internal partial class ExportAssetDownloader
{ {
private static readonly AsyncKeyedLocker<string> _locker = new(o => private static readonly AsyncKeyedLocker<string> Locker = new(o =>
{ {
o.PoolSize = 20; o.PoolSize = 20;
o.PoolInitialFill = 1; o.PoolInitialFill = 1;
}); });
private readonly string _workingDirPath; private readonly string _workingDirPath;
private readonly bool _reuse; private readonly bool _reuse;
@ -37,48 +38,48 @@ internal partial class ExportAssetDownloader
var fileName = GetFileNameFromUrl(url); var fileName = GetFileNameFromUrl(url);
var filePath = Path.Combine(_workingDirPath, fileName); 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)) if (_pathCache.TryGetValue(url, out var cachedFilePath))
return cachedFilePath; return cachedFilePath;
// Reuse existing files if we're allowed to // 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 () =>
{ {
Directory.CreateDirectory(_workingDirPath); // Download the file
using var response = await Http.Client.GetAsync(url, cancellationToken);
await using (var output = File.Create(filePath))
await response.Content.CopyToAsync(output, cancellationToken);
await Http.ResiliencePolicy.ExecuteAsync(async () => // Try to set the file date according to the last-modified header
try
{ {
// Download the file var lastModified = response.Content.Headers.TryGetValue("Last-Modified")?.Pipe(s =>
using var response = await Http.Client.GetAsync(url, cancellationToken); DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out var instant)
await using (var output = File.Create(filePath)) ? instant
await response.Content.CopyToAsync(output, cancellationToken); : (DateTimeOffset?)null
);
// Try to set the file date according to the last-modified header if (lastModified is not null)
try
{ {
var lastModified = response.Content.Headers.TryGetValue("Last-Modified")?.Pipe(s => File.SetCreationTimeUtc(filePath, lastModified.Value.UtcDateTime);
DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out var instant) File.SetLastWriteTimeUtc(filePath, lastModified.Value.UtcDateTime);
? instant File.SetLastAccessTimeUtc(filePath, lastModified.Value.UtcDateTime);
: (DateTimeOffset?)null
);
if (lastModified is not null)
{
File.SetCreationTimeUtc(filePath, lastModified.Value.UtcDateTime);
File.SetLastWriteTimeUtc(filePath, lastModified.Value.UtcDateTime);
File.SetLastAccessTimeUtc(filePath, lastModified.Value.UtcDateTime);
}
} }
catch }
{ catch
// This can apparently fail for some reason. {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/585 // This can apparently fail for some reason.
// Updating file dates is not a critical task, so we'll just // https://github.com/Tyrrrz/DiscordChatExporter/issues/585
// ignore exceptions thrown here. // Updating file dates is not a critical task, so we'll just
} // ignore exceptions thrown here.
}); }
} });
return _pathCache[url] = filePath; return _pathCache[url] = filePath;
} }