Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a result operation to represent the file contents #59

Closed
MrDave1999 opened this issue Feb 26, 2024 · 0 comments · Fixed by #61
Closed

Add a result operation to represent the file contents #59

MrDave1999 opened this issue Feb 26, 2024 · 0 comments · Fixed by #61
Labels
feature New feature or request

Comments

@MrDave1999
Copy link
Owner

MrDave1999 commented Feb 26, 2024

New types to be added:

public class StreamFileContent
{
    public StreamFileContent(Stream content) => Content = content;
    public Stream Content { get; }
    public string ContentType { get; init; } = string.Empty;
    public string FileName { get; init; } = string.Empty;
}

public class ByteArrayFileContent
{
    public ByteArrayFileContent(byte[] content) => Content = content;
    public byte[] Content { get; }
    public string ContentType { get; init; } = string.Empty;
    public string FileName { get; init; } = string.Empty;
}

These types represent the content of a file.

Two methods are added to the Result type:

public static Result<StreamFileContent> File(StreamFileContent fileContent);
public static Result<ByteArrayFileContent> File(ByteArrayFileContent fileContent);

Usage

public class DownloadProformaInvoiceUseCase(
    IHtmlTemplateLoader htmlTemplateLoader,
    IHtmlConverter htmlConverter,
    DownloadProformaInvoiceValidator validator)
{
    public async Task<Result<ByteArrayFileContent>> DownloadAsPdfAsync(DownloadProformaInvoiceRequest request)
    {
        ValidationResult result = validator.Validate(request);
        if (result.IsFailed())
            return Result.Invalid(result.AsErrors());

        var html = await htmlTemplateLoader
            .LoadAsync("./Templates/ProformaInvoice.html", request.MapToObject());

        byte[] fileContents = htmlConverter.ConvertToPdf(html, new MemoryStream());
        var byteArrayFileContent = new ByteArrayFileContent(fileContents)
        {
            ContentType = "application/pdf",
            FileName = "Report.pdf"
        };
        return Result.File(byteArrayFileContent);
    }
}

If the result is successful, Result<ByteArrayFileContent> should be translated to an object like FileContentResult or FileStreamResult (these are types used in MVC controllers).

Support should also be added for Minimal APIs. See, FileContentHttpResult and FileStreamHttpResult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant