Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## [3.2.0] - 2026-08-20
### Changes
- StreamCopyingResultHandler now handles ForbiddenResult<StreamResponse> (403). Previously a forbidden result fell through to the default case and returned 500.
- StreamResponse gained a Disposition property (default "inline") so callers can force "attachment" for downloads (e.g. zips) while PDFs and similar keep previewing inline.
## [3.0.0] - 2026-05-12
### Changes
- Updated to .NET 10
Expand Down
7 changes: 5 additions & 2 deletions Linn.Common.Service.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35506.116
# Visual Studio Version 18
VisualStudioVersion = 18.4.11620.152 stable
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Linn.Common.Service", "src\Linn.Common.Service.csproj", "{C58B1BE7-9DAD-4F3A-AF6E-B9515DE0612B}"
EndProject
Expand Down Expand Up @@ -35,4 +35,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B39241F2-FBE3-462C-8F75-F37B6990D3C2}
EndGlobalSection
EndGlobal
15 changes: 13 additions & 2 deletions src/Handlers/StreamCopyingResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ public async Task Handle(

if (!string.IsNullOrEmpty(success.Data.FileName))
{
var disposition = string.IsNullOrEmpty(success.Data.Disposition)
? "inline"
: success.Data.Disposition;
res.Headers["Content-Disposition"] =
$"inline; filename=\"{success.Data.FileName}\"";
}
$"{disposition}; filename=\"{success.Data.FileName}\"";
Comment on lines +43 to +47
}

res.StatusCode = (int)HttpStatusCode.OK;

Expand Down Expand Up @@ -73,6 +76,14 @@ public async Task Handle(
}
break;

case ForbiddenResult<StreamResponse> forbidden:
res.StatusCode = 403;
if (!string.IsNullOrEmpty(forbidden.Message))
{
await res.WriteAsync(forbidden.Message, cancellationToken);
}
break;

case NotFoundResult<StreamResponse> _:
res.StatusCode = 404;
break;
Expand Down
3 changes: 3 additions & 0 deletions src/Handlers/StreamResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
{
public class StreamResponse
{
public Stream Stream { get; set; }

Check warning on line 5 in src/Handlers/StreamResponse.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Non-nullable property 'Stream' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public string ContentType { get; set; }

Check warning on line 7 in src/Handlers/StreamResponse.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Non-nullable property 'ContentType' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public string FileName { get; set; }

Check warning on line 9 in src/Handlers/StreamResponse.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Non-nullable property 'FileName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

// inline (preview, e.g. PDFs) or attachment (force download, e.g. zips)
public string Disposition { get; set; } = "inline";
}
}
2 changes: 1 addition & 1 deletion src/Linn.Common.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>Linn.Common.Service</AssemblyName>
<PackageId>Linn.Common.Service</PackageId>
<Version>3.1.0</Version>
<Version>3.2.0</Version>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
Expand Down
98 changes: 98 additions & 0 deletions tests/WhenCopyingStreamResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace Linn.Common.Service.Tests
{
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using FluentAssertions;

using Linn.Common.Facade;
using Linn.Common.Service.Handlers;

using Microsoft.AspNetCore.Http;

using NUnit.Framework;

public class WhenCopyingStreamResult
{
private StreamCopyingResultHandler handler;

private DefaultHttpContext context;

[SetUp]
public void SetUp()
{
this.handler = new StreamCopyingResultHandler();
this.context = new DefaultHttpContext();
this.context.Response.Body = new MemoryStream();
}

[Test]
public async Task ShouldCopySuccessStreamAndSetAttachmentDisposition()
{
var payload = Encoding.UTF8.GetBytes("zip-bytes");
var result = new SuccessResult<StreamResponse>(
new StreamResponse
{
Stream = new MemoryStream(payload),
ContentType = "application/zip",
FileName = "linn-resources.zip",
Disposition = "attachment"
});

await this.handler.Handle(this.context.Request, this.context.Response, result, CancellationToken.None);

this.context.Response.StatusCode.Should().Be((int)HttpStatusCode.OK);
this.context.Response.ContentType.Should().Be("application/zip");
this.context.Response.Headers["Content-Disposition"].ToString()
.Should().Be("attachment; filename=\"linn-resources.zip\"");

this.context.Response.Body.Position = 0;
using var reader = new StreamReader(this.context.Response.Body);
(await reader.ReadToEndAsync()).Should().Be("zip-bytes");
}

[Test]
public async Task ShouldDefaultToInlineDisposition()
{
var result = new SuccessResult<StreamResponse>(
new StreamResponse
{
Stream = new MemoryStream(Encoding.UTF8.GetBytes("pdf")),
ContentType = "application/pdf",
FileName = "invoice.pdf"
});

await this.handler.Handle(this.context.Request, this.context.Response, result, CancellationToken.None);

this.context.Response.Headers["Content-Disposition"].ToString()
.Should().Be("inline; filename=\"invoice.pdf\"");
}

[Test]
public async Task ShouldReturn403ForForbiddenResult()
{
var result = new ForbiddenResult<StreamResponse>("Access denied");

await this.handler.Handle(this.context.Request, this.context.Response, result, CancellationToken.None);

this.context.Response.StatusCode.Should().Be((int)HttpStatusCode.Forbidden);

this.context.Response.Body.Position = 0;
using var reader = new StreamReader(this.context.Response.Body);
(await reader.ReadToEndAsync()).Should().Be("Access denied");
}

[Test]
public async Task ShouldReturn404ForNotFoundResult()
{
var result = new NotFoundResult<StreamResponse>("nope");

await this.handler.Handle(this.context.Request, this.context.Response, result, CancellationToken.None);

this.context.Response.StatusCode.Should().Be((int)HttpStatusCode.NotFound);
}
}
}
Loading