Skip to content

Commit

Permalink
Updated backend
Browse files Browse the repository at this point in the history
Added csv export
  • Loading branch information
Aragas committed Feb 16, 2024
1 parent 8154b1c commit d903cd9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageReference Include="Vogen" Version="3.0.23" />
<PackageReference Include="Vogen" Version="3.0.24" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 18 additions & 17 deletions src/BUTR.Site.NexusMods.Server/BUTR.Site.NexusMods.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,43 @@
<PackageReference Include="AsmResolver.DotNet" Version="5.5.0" />
<PackageReference Include="Bannerlord.ModuleManager" Version="5.0.221" />
<PackageReference Include="BUTR.Authentication.NexusMods" Version="1.0.0.6" />
<PackageReference Include="BUTR.CrashReport.Models" Version="13.0.0.47" />
<PackageReference Include="BUTR.CrashReport.Bannerlord.Parser" Version="13.0.0.47" />
<PackageReference Include="BUTR.CrashReport.Models" Version="13.0.0.48" />
<PackageReference Include="BUTR.CrashReport.Bannerlord.Parser" Version="13.0.0.48" />
<PackageReference Include="Community.Microsoft.Extensions.Caching.PostgreSql" Version="4.0.4" />
<PackageReference Include="CsvHelper" Version="31.0.0" />
<PackageReference Include="ICSharpCode.Decompiler" Version="8.2.0.7535" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.2" />
<PackageReference Include="Mono.Posix" Version="7.1.0-final.1.21458.1" />
<PackageReference Include="Nerdbank.Streams" Version="2.10.72" />
<PackageReference Include="Npgsql" Version="8.0.1" />
<PackageReference Include="Npgsql" Version="8.0.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.1" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.2" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.8" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.6.0-beta.3" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.3" />
<PackageReference Include="OpenTelemetry.Instrumentation.Quartz" Version="1.0.0-alpha.3" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.5.1" />
<PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.4" />
<PackageReference Include="Polly" Version="8.2.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.10" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.7.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.4" />
<PackageReference Include="OpenTelemetry.Instrumentation.Quartz" Version="1.0.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.6" />
<PackageReference Include="Polly" Version="8.3.0" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
<PackageReference Include="Quartz" Version="3.8.0" />
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.8.0" />
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.8.0" />
<PackageReference Include="RateLimiter" Version="2.2.0" />
<PackageReference Include="SharpCompress" Version="0.35.0" />
<PackageReference Include="SharpCompress" Version="0.36.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="8.101.1.3" />
<PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="8.102.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using BUTR.Site.NexusMods.Server.Utils.Csv.Utils;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace BUTR.Site.NexusMods.Server.Utils.Csv.Extensions;

[ExcludeFromCodeCoverage]
public static class MvcOptionsExtensions
{
public static void AddCsvOutputFormatters(this MvcOptions options)
{
var types = typeof(Program).Assembly.GetTypes().Where(t => t.IsAssignableTo(typeof(ICsvFile)) && !t.IsAbstract);
foreach (var type in types)
options.OutputFormatters.Insert(0, (IOutputFormatter) Activator.CreateInstance(typeof(ExportOutputFormatter<>).MakeGenericType(type))!);
}
}
6 changes: 6 additions & 0 deletions src/BUTR.Site.NexusMods.Server/Utils/Csv/ICsvFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BUTR.Site.NexusMods.Server.Utils.Csv;

public interface ICsvFile
{
static abstract string GenerateFilename();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using CsvHelper;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers;

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace BUTR.Site.NexusMods.Server.Utils.Csv.Utils;

public class ExportOutputFormatter<TCsvFile> : TextOutputFormatter where TCsvFile: ICsvFile
{
private static string ContentType => "text/csv";

public ExportOutputFormatter()
{
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(ContentType));

SupportedEncodings.Add(Encoding.UTF8);
SupportedEncodings.Add(Encoding.Unicode);
}

protected override bool CanWriteType(Type? type)
{
if (typeof(TCsvFile).IsAssignableFrom(type) || typeof(IEnumerable<TCsvFile>).IsAssignableFrom(type))
return base.CanWriteType(type);
return false;
}

public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
{
var (data, type) = context.Object switch
{
IEnumerable<TCsvFile> enumerable => (enumerable, enumerable.GetType().GetGenericArguments()[0]),
TCsvFile entry => (new List<TCsvFile> { entry }, entry.GetType()),
_ => default
};
if (data is null || type is null)
throw new InvalidOperationException("Invalid csv data type!");

await WriteCsvCollection(context.HttpContext, data, selectedEncoding);
}

private static async Task WriteCsvCollection(HttpContext context, IEnumerable<TCsvFile> entries, Encoding selectedEncoding)
{
var response = context.Response;

response.Headers.ContentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = TCsvFile.GenerateFilename(),
Inline = false,
}.ToString();

await using var csv = new CsvWriter(new StreamWriter(response.Body, selectedEncoding), CultureInfo.InvariantCulture);

csv.WriteHeader<TCsvFile>();
await csv.NextRecordAsync();
await csv.WriteRecordsAsync(entries);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.127">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.141">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit d903cd9

Please sign in to comment.