Prefer message timestamp over the last-modified header for asset file dates (#1321)

This commit is contained in:
Ritiek Malhotra 2024-12-12 17:55:44 +00:00 committed by GitHub
parent bf417db80c
commit a9acf17375
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 32 deletions

View file

@ -23,7 +23,8 @@ internal partial class ExportAssetDownloader(string workingDirPath, bool reuse)
public async ValueTask<string> DownloadAsync( public async ValueTask<string> DownloadAsync(
string url, string url,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default,
DateTimeOffset? timestamp = null
) )
{ {
var fileName = GetFileNameFromUrl(url); var fileName = GetFileNameFromUrl(url);
@ -48,34 +49,12 @@ internal partial class ExportAssetDownloader(string workingDirPath, bool reuse)
await using (var output = File.Create(filePath)) await using (var output = File.Create(filePath))
await response.Content.CopyToAsync(output, innerCancellationToken); await response.Content.CopyToAsync(output, innerCancellationToken);
// Try to set the file date according to the last-modified header // Try to set the file date according to the message timestamp
try if (timestamp is not null)
{ {
var lastModified = response File.SetCreationTimeUtc(filePath, timestamp.Value.UtcDateTime);
.Content.Headers.TryGetValue("Last-Modified") File.SetLastWriteTimeUtc(filePath, timestamp.Value.UtcDateTime);
?.Pipe(s => File.SetLastAccessTimeUtc(filePath, timestamp.Value.UtcDateTime);
DateTimeOffset.TryParse(
s,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out var instant
)
? instant
: (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
{
// This can apparently fail for some reason.
// Updating the file date is not a critical task, so we'll just ignore exceptions thrown here.
// https://github.com/Tyrrrz/DiscordChatExporter/issues/585
} }
}, },
cancellationToken cancellationToken

View file

@ -103,7 +103,8 @@ internal class ExportContext(DiscordClient discord, ExportRequest request)
public async ValueTask<string> ResolveAssetUrlAsync( public async ValueTask<string> ResolveAssetUrlAsync(
string url, string url,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default,
DateTimeOffset? timestamp = null
) )
{ {
if (!Request.ShouldDownloadAssets) if (!Request.ShouldDownloadAssets)
@ -111,7 +112,7 @@ internal class ExportContext(DiscordClient discord, ExportRequest request)
try try
{ {
var filePath = await _assetDownloader.DownloadAsync(url, cancellationToken); var filePath = await _assetDownloader.DownloadAsync(url, cancellationToken, timestamp);
var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath); var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath);
// Prefer the relative path so that the export package can be copied around without breaking references. // Prefer the relative path so that the export package can be copied around without breaking references.

View file

@ -436,7 +436,7 @@ internal class JsonMessageWriter(Stream stream, ExportContext context)
_writer.WriteString("id", attachment.Id.ToString()); _writer.WriteString("id", attachment.Id.ToString());
_writer.WriteString( _writer.WriteString(
"url", "url",
await Context.ResolveAssetUrlAsync(attachment.Url, cancellationToken) await Context.ResolveAssetUrlAsync(attachment.Url, cancellationToken, message.Timestamp)
); );
_writer.WriteString("fileName", attachment.FileName); _writer.WriteString("fileName", attachment.FileName);
_writer.WriteNumber("fileSizeBytes", attachment.FileSize.TotalBytes); _writer.WriteNumber("fileSizeBytes", attachment.FileSize.TotalBytes);
@ -466,7 +466,7 @@ internal class JsonMessageWriter(Stream stream, ExportContext context)
_writer.WriteString("format", sticker.Format.ToString()); _writer.WriteString("format", sticker.Format.ToString());
_writer.WriteString( _writer.WriteString(
"sourceUrl", "sourceUrl",
await Context.ResolveAssetUrlAsync(sticker.SourceUrl, cancellationToken) await Context.ResolveAssetUrlAsync(sticker.SourceUrl, cancellationToken, message.Timestamp)
); );
_writer.WriteEndObject(); _writer.WriteEndObject();