Files
itc.pidi-3-docs/figures/code/presigned-url.cs
T
0qln bff34658b8 appendix: 23 Code-Ausschnitte aus dem Houston-Repository
Pin nixpkgs to the revision used by itc.componentware: the channel version
ships a latexminted that crashes on Python 3.14, which made every listing
fail and sent latexmk into an endless error prompt. Also force
nonstopmode/halt-on-error so a broken listing can never loop again.
2026-08-25 21:38:22 +02:00

40 lines
1.4 KiB
C#

private async Task<string?> GetPreSignedUrlAsync(DocumentKey key, bool inline, CancellationToken cancellationToken)
{
if (!await ObjectExistsAsync(key, cancellationToken))
return null;
var request = new GetPreSignedUrlRequest
{
BucketName = _settings.S3.Bucket,
Key = key.ToString(),
Verb = HttpVerb.GET,
Expires = DateTime.UtcNow.Add(DownloadUrlLifetime),
ResponseHeaderOverrides =
{
ContentDisposition = inline
? InlineContentDisposition(key.Name.ToString())
: AttachmentContentDisposition(key.Name.ToString()),
},
};
return await s3.GetPreSignedURLAsync(request);
}
private static string AttachmentContentDisposition(string fileName)
{
var header = new ContentDispositionHeaderValue("attachment");
header.SetHttpFileName(fileName);
return header.ToString();
}
/// <summary>
/// Builds a <c>Content-Disposition</c> header that lets the browser render the document inline
/// while preserving the file name, using RFC 5987 encoding so non ASCII file names survive.
/// </summary>
private static string InlineContentDisposition(string fileName)
{
var asciiFallback = new string(fileName.Select(c => c is >= ' ' and < (char)127 and not '"' ? c : '_').ToArray());
var encoded = Uri.EscapeDataString(fileName);
return $"inline; filename=\"{asciiFallback}\"; filename*=UTF-8''{encoded}";
}