Skip to content

Commit

Permalink
Add new endpoint to send file as DataResponse with string content
Browse files Browse the repository at this point in the history
  • Loading branch information
Gramli committed Jun 13, 2024
1 parent ebdf1ae commit a340e8e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/File.API/EndpointBuilders/FileEndpointsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ await handler.GetFileAsync(new DownloadFileQuery(id), cancellationToken))
.Produces<FileContentHttpResult>()
.WithName("DownloadFile")
.WithTags("Get");

endpointRouteBuilder.MapGet("v1/downloadAsJson",
async (int id, [FromServices] IDownloadFileQueryHandler handler, CancellationToken cancellationToken) =>
await handler.GetJsonFileAsync(new DownloadFileQuery(id), cancellationToken))
.DisableAntiforgery()
.Produces<DataResponse<StringContentFileDto>>()
.WithName("DownloadFileAsJson")
.WithTags("Get");

return endpointRouteBuilder;
}

Expand Down
22 changes: 22 additions & 0 deletions src/File.API/Extensions/IHandlerExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using File.Core.Abstractions;
using File.Domain.Dtos;
using File.Domain.Http;
using System.Buffers.Text;

namespace File.API.Extensions
{
Expand All @@ -20,5 +22,25 @@ internal static class IHandlerExtension
}
return Results.NotFound();
}

internal static async Task<IResult> GetJsonFileAsync<TResponse, TRequest>(this IRequestHandler<TResponse, TRequest> requestHandler, TRequest request, CancellationToken cancellationToken) where TResponse : FileDto
{
var response = await requestHandler.HandleAsync(request, cancellationToken);
if (response.Data is not null)
{
return Results.Json(new DataResponse<StringContentFileDto>
{
Errors = response.Errors,
Data = new StringContentFileDto
{
Data = Convert.ToBase64String(response.Data.Data),
ContentType = response.Data.ContentType,
FileName = response.Data.FileName,
Length = response.Data.Length
},
}, statusCode: (int)response.StatusCode);
}
return Results.NotFound();
}
}
}
11 changes: 11 additions & 0 deletions src/File.Domain/Dtos/BaseFileDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace File.Domain.Dtos
{
public abstract class BaseFileDto
{
public string FileName { get; init; } = string.Empty;

public string ContentType { get; init; } = string.Empty;

public long Length { get; init; }
}
}
8 changes: 1 addition & 7 deletions src/File.Domain/Dtos/FileDto.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
namespace File.Domain.Dtos
{
public class FileDto
public class FileDto : BaseFileDto
{
public string FileName { get; init; } = string.Empty;

public string ContentType { get; init; } = string.Empty;

public long Length { get; init; }

public byte[] Data { get; init; } = [];
}
}
7 changes: 7 additions & 0 deletions src/File.Domain/Dtos/StringContentFileDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace File.Domain.Dtos
{
public sealed class StringContentFileDto : BaseFileDto
{
public string Data { get; init; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ internal sealed class FileEntity
public int Id { get; set; }
public string FileName { get; init; } = string.Empty;
public string ContentType { get; init; } = string.Empty;
public byte[] Data { get; set; } = new byte[0];
public byte[] Data { get; set; } = [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ public async Task<Result<FileDto>> GetFile(DownloadFileQuery downloadFileQuery,
return Result.Fail(string.Format(ErrorMessages.FileNotExists, downloadFileQuery.Id));
}

return Result.Ok(file.Adapt<FileDto>());
return Result.Ok(new FileDto
{
ContentType = file.ContentType,
Data = file.Data,
FileName = file.FileName,
Length = file.Data.Length,
});
}

public async Task<IEnumerable<FileInfoDto>> GetFilesInfo(CancellationToken cancellationToken)
Expand Down

0 comments on commit a340e8e

Please sign in to comment.